Java – How to get current date time

In this tutorial, we will show you how to get the current date time from the new Java 8 java.time.* like Localdate, LocalTime, LocalDateTime, ZonedDateTime, Instant and also the legacy date time APIs like Date and Calendar.

Table of contents

Summary

  • For new Java 8 java.time.* APIs , we can use .now() to get the current date-time and format it with DateTimeFormatter.
  • For legacy date-time APIs, we can use new Date() and Calendar.getInstance() to get the current date-time and format it with SimpleDateFormat.

1. Get current date time in Java

The below are some code snippets to display the current date-time in Java.

For java.time.LocalDate, uses LocalDate.now()


  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd");
  LocalDate localDate = LocalDate.now();
  System.out.println(dtf.format(localDate));            // 2021/03/22

For java.time.localTime, uses LocalTime.now()


  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
  LocalTime localTime = LocalTime.now();
  System.out.println(dtf.format(localTime));            // 16:37:15

For java.time.LocalDateTime, uses LocalDateTime.now()


  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
  LocalDateTime now = LocalDateTime.now();
  System.out.println(dtf.format(now));                  //  2021/03/22 16:37:15

For java.time.ZonedDateTime, uses ZonedDateTime.now()


  // get current date-time, with system default time zone
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
  ZonedDateTime now = ZonedDateTime.now();
  System.out.println(dtf.format(now));                  // 2021/03/22 16:37:15
  System.out.println(now.getOffset());                  // +08:00

  // get current date-time, with a specified time zone
  ZonedDateTime japanDateTime = now.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
  System.out.println(dtf.format(japanDateTime));        // 2021/03/22 17:37:15
  System.out.println(japanDateTime.getOffset());        // +09:00

For java.time.Instant, uses Instant.now()


  Instant now = Instant.now();

  // convert Instant to ZonedDateTime
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
  ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, ZoneId.systemDefault());
  System.out.println(dtfDateTime.format(zonedDateTime));

For java.util.Date, uses new Date()


  DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  Date date = new Date();
  System.out.println(dateFormat.format(date));           // 2021/03/22 16:37:15

For java.util.Calendar, uses Calendar.getInstance()


  DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  Calendar cal = Calendar.getInstance();
  System.out.println(dateFormat.format(cal.getTime()));  // 2021/03/22 16:37:15

2. java.time.LocalDate

For the java.time.LocalDate, uses LocalDate.now() to get the current date without a time-zone, and format it with the DateTimeFormatter.

LocalDateExample.java

package com.mkyong.app;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class LocalDateExample {

  public static void main(String[] args) {

      DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd");
      LocalDate localDate = LocalDate.now();
      System.out.println(dtf.format(localDate));    // 2021/03/22

  }

}

Output

Terminal

  2021/03/22

3. java.time.LocalTime

For the java.time.LocalTime, uses LocalDate.now() to get the current time without a time-zone, and format it with the DateTimeFormatter.

LocalTimeExample.java

package com.mkyong.app;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class LocalTimeExample {

    public static void main(String[] args) {

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalTime localTime = LocalTime.now();
        System.out.println(dtf.format(localTime));    // 16:37:15

    }

}

Output

Terminal

  16:37:15

4. java.time.LocalDateTime

For java.time.LocalDateTime, uses LocalDateTime.now() to get the current date time without a time-zone, and format it with the DateTimeFormatter.

LocalDateTimeExample.java

package com.mkyong.app;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeExample {

    public static void main(String[] args) {

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        System.out.println(dtf.format(now));        //  2021/03/22 16:37:15

    }

}

Output

Terminal

  2021/03/22 16:37:15

5. java.time.ZonedDateTime

For java.time.ZonedDateTime, uses ZonedDateTime.now() to get the current date time with the system default time zone, or a specified time zone.

ZonedDateTimeExample.java

package com.mkyong.app;

import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ZonedDateTimeExample {

  public static void main(String[] args) {

      DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");

      // Get default time zone
      System.out.println(ZoneOffset.systemDefault());         // Asia/Kuala_Lumpur
      System.out.println(OffsetDateTime.now().getOffset());   // +08:00

      // get current date time, with +08:00
      ZonedDateTime now = ZonedDateTime.now();
      System.out.println(dtf.format(now));                    // 2021/03/22 16:37:15
      System.out.println(now.getOffset());                    // +08:00

      // get get current date time, with +09:00
      ZonedDateTime japanDateTime = now.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
      System.out.println(dtf.format(japanDateTime));          // 2021/03/22 17:37:15
      System.out.println(japanDateTime.getOffset());          // +09:00

  }

}

Output

Terminal

Asia/Kuala_Lumpur
+08:00

2021/03/22 16:37:15
+08:00

2021/03/22 17:37:15
+09:00

6. java.time.Instant

For java.time.Instant, uses Instant.now() to get the seconds passed since the Unix epoch time (midnight of January 1, 1970 UTC), and later convert to other java.time.* date time classes like LocalDate, LocalDateTime and ZonedDateTime.

InstantExample.java

package com.mkyong.app;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class InstantExample {

  private static final DateTimeFormatter dtfDate = DateTimeFormatter.ofPattern("uuuu/MM/dd");
  private static final DateTimeFormatter dtfTime = DateTimeFormatter.ofPattern("HH:mm:ss");
  private static final DateTimeFormatter dtfDateTime = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");

  public static void main(String[] args) {

      // seconds passed since the Unix epoch time (midnight of January 1, 1970 UTC)
      Instant now = Instant.now();

      // convert Instant to LocalDate
      LocalDate localDate = LocalDate.ofInstant(now, ZoneId.systemDefault());
      System.out.println(dtfDate.format(localDate));

      // convert Instant to localTime
      LocalTime localTime = LocalTime.ofInstant(now, ZoneId.systemDefault());
      System.out.println(dtfTime.format(localTime));

      // convert Instant to LocalDateTime
      LocalDateTime localDateTime = LocalDateTime.ofInstant(now, ZoneId.systemDefault());
      System.out.println(dtfDateTime.format(localDateTime));

      // convert Instant to ZonedDateTime
      ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, ZoneId.systemDefault());
      System.out.println(dtfDateTime.format(zonedDateTime));

  }

}

Output

Terminal

2021/03/22
16:37:15
2021/03/22 16:37:15
2021/03/22 16:37:15

7. java.util.Date (Legacy)

For the legacy java.util.Date, uses new Date() or new Date(System.currentTimeMillis() to get the current date time, and format it with the SimpleDateFormat.

DateExample.java

package com.mkyong.app;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateExample {

  public static void main(String[] args) {

      DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

      Date date = new Date();
      System.out.println(dateFormat.format(date));    // 2021/03/22 16:37:15

      // new Date() actually calls this new Date(long date)
      Date date2 = new Date(System.currentTimeMillis());
      System.out.println(dateFormat.format(date));    // 2021/03/22 16:37:15

  }

}

Output

Terminal

  2021/03/22 16:37:15
  2021/03/22 16:37:15

8. java.util.Calendar (Legacy)

For the legacy java.util.Calendar, uses Calendar.getInstance() to get the current date time, and format it with the SimpleDateFormat.

CalendarExample.java

package com.mkyong.app;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarExample {

  public static void main(String[] args) {

      DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      Calendar cal = Calendar.getInstance();
      System.out.println(dateFormat.format(cal.getTime()));   // 2021/03/22 16:37:15

  }

}

Output

Terminal

  2021/03/22 16:37:15

9. References

14 comments on “Java – How to get current date time

  1. The reason I don’t like this post is for a simple timestamp, now I have to go look for the import statements needed for any of the API’s, I wish I could just come to a page and then get whatever I need from that page and not go looking for other salient parts of the complete code, like lots of other places.

    Reply
  2. want to convert
    From:
    22-JUL-22 10.00.00.000000000 AM
    To:
    2022-07-22T00:00:00Z
    Please help

    Reply
  3. Hi!
    You have typo in that example:
    // new Date() actually calls this new Date(long date)
    Date date2 = new Date(System.currentTimeMillis());
    System.out.println(dateFormat.format(date));
    in second line it should be date2, not date.

    Reply
  4. how to get the date object in dd/MM/yyyy without timestamp but not as string.

    Reply
  5. Hi friends how to display the hour AM/ PM? thanks

    Reply
  6. NOTE: make sure Date.util is imported not the sql lib

    Reply
  7. You blog is really really helpful! I have found serveral articles that directly solve my problems, Thank you!

    Reply
  8. Thank you for the clarification.
    Simple and precise.

    Reply
  9. Omg, you have tutorials for everything its amazing; you are really saving me a lot of time; thank you for you very good how to and examples

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *