-
Notifications
You must be signed in to change notification settings - Fork 4
Getting current date and time with Calendar
Ramesh Fadatare edited this page Jul 11, 2019
·
1 revision
java.util.Calendar represents a specific instant in time, with millisecond precision.
The example uses java.util.Calendar to get the current date-time and formats it with java.text.SimpleDateFormat.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class JavaCurrentDateTimeCalendar {
public static void main(String[] args) {
Date now = Calendar.getInstance().getTime();
DateFormat df = new SimpleDateFormat("yyyy-MM-d HH:mm:ss");
System.out.println(df.format(now));
}
}
Output:
2019-07-11 17:30:59