Steps to get current date time in java
- Creating the SimpleDateFormat object with the desire pattern.
- Create an object of Date class.
- Call the format() method of DateFormat class and pass the date object as an argument to the method.
Java get current date time example
package com.w3schools;
import java.text.SimpleDateFormat;
import java.util.Date;
public class GetDateTime {
public static void main(String args[]){
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String date = sdf.format(new Date());
System.out.println(date);
}
} |
package com.w3schools;
import java.text.SimpleDateFormat;
import java.util.Date;
public class GetDateTime {
public static void main(String args[]){
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String date = sdf.format(new Date());
System.out.println(date);
}
}
Output: