Java - How to get current date and time

By xngo on June 14, 2019

In Java, there are different ways to get the current date and time. You can get the current date and time through the following classes:

  • Date represents a specific instant in time, with millisecond precision.
  • Calendar is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields.
  • LocalDate represents date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.
  • LocalDateTime represents date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.
  • ZonedDateTime represents date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.

Here are some examples to get the current date time using the classes mentioned above.

For Java 8 and onward

For Java 8 and onward, it is recommended to use the new Date and Time APIs that are part of java.time package.

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class DateTime8Current {
    public static void main(String[] args) {
        LocalDate localDate =;
        LocalDateTime localDateTime =;
        ZonedDateTime zonedDateTime =;
        // Convert current date time to different formats than ISO-8601.
        // **************************************************************
                localDate.format(DateTimeFormatter.ofPattern("E, dd MMMM yyyy")));
                localDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss a")));
                zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss z")));


Sun, 17 November 2019
2019/11/17 09:45:34 PM
2019/11/17 09:45:34 EST

For Java 7 and below

For Java 7 and below, you can use Date and Calendar classes.

import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class DateTime7Current {
    public static void main(String[] args) {
        // Using Date class.
        Date date = new Date();
        // Using Calendar class.
        //        SimpleDateFormat is used because 
        //        Calendar.toString() outputs more data than needed.
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");


Sun Nov 17 09:47:55 EST 2019
2019/11/17 09:47:55


  • For Java 8 and onward, use
  • For Java 7 and below, use

About the author

Xuan Ngo is the founder of He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.