Java : Get the current time

There are several APIs in Java SE for getting the current time, e.g. Instant.now, ZonedDateTime.now, System.currentTimeMillis etc. This article provides the recommended methods for getting the current time.

Note:


Quick Guide

The code examples on this page are running with the pacific time zone.

final var zoneId = ZoneId.systemDefault();
System.out.println(zoneId); // America/Los_Angeles
System.out.println(zoneId.getDisplayName(TextStyle.FULL, Locale.US)); // Pacific Time

Java 8 or later (Recommendation)

Class Method Code Example
Instant Instant now()

Obtains a current instant from the system UTC clock.
final var now = Instant.now();

// 2023-10-11T05:29:57.515360Z
System.out.println(now);
ZonedDateTime ZonedDateTime now()

Obtains a current date-time from the system clock in the default time-zone.
final var now = ZonedDateTime.now();

// 2023-10-10T22:29:57.522355400-07:00[America/Los_Angeles]
System.out.println(now);
LocalDateTime LocalDateTime now()

Obtains a current date-time from the system clock in the default time-zone. The LocalDateTime object itself does not have the time-zone.
final var now = LocalDateTime.now();

// 2023-10-10T22:29:57.524354900
System.out.println(now);
Clock This class is a clock with a time zone.

It is useful when you want to obtain a dummy current time in unit tests. Please see here for more details.
final var clock = Clock.systemDefaultZone();
final var now1 = clock.instant();

// 2023-10-11T05:29:57.524354900Z
System.out.println(now1);

final var now2 = ZonedDateTime.now(clock);

// 2023-10-10T22:29:57.524354900-07:00[America/Los_Angeles]
System.out.println(now2);

Java 7 or earlier

Class Method Code Example
System long currentTimeMillis()

The difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
final var now = System.currentTimeMillis();

// 1672826617074
System.out.println(now);
Calendar Calendar getInstance()

Obtains a calendar using the default time zone and locale.
final var now = Calendar.getInstance();

// America/Los_Angeles
System.out.println(now.getTimeZone().getID());

// 2023-01-04 02:03:37
System.out.printf("%tF %tT", now, now);
Date Date()

Obtains a date from the system UTC clock.
final var now = new Date();

// 1672826617085
System.out.println(now.getTime());

The Calendar class and the Date class are legacy APIs. If you use Java 8 or later, there are new alternative APIs in the java.time package. You should use new APIs. This article omits the explanation of the Calendar class and the Date class.

Please see also : Don't use the legacy Date and Calendar classes, use new APIs instead


Instant.now

public static Instant now()
Obtains the current instant from the system clock.

The Instant class models a single instantaneous point on the time-line. The now method obtains the current instant from the system UTC clock with nanosecond precision.

final var now = Instant.now();

// UTC
System.out.println(now); // 2023-01-04T10:14:35.271737700Z

// Epoch time
System.out.println(now.getEpochSecond()); // 1672827275
System.out.println(now.getNano()); // 271737700

// Converts the now object to a ZonedDateTime object with a default time-zone.
final var zoneId = ZoneId.systemDefault();
System.out.println(zoneId); // America/Los_Angeles

final var dateTime = ZonedDateTime.ofInstant(now, zoneId);
System.out.println(dateTime); // 2023-01-04T02:14:35.271737700-08:00[America/Los_Angeles]

ZonedDateTime.now

public static ZonedDateTime now()
Obtains the current date-time from the system clock in the default time-zone.

The ZonedDateTime class is a date-time with a time-zone.

System.out.println(ZoneId.systemDefault()); // America/Los_Angeles

// Obtains a current date-time with a default time-zone.
final var now = ZonedDateTime.now();
System.out.println(now); // 2023-01-04T02:14:01.885252900-08:00[America/Los_Angeles]

// UTC
System.out.println(now.toInstant()); // 2023-01-04T10:14:01.885252900Z

LocalDateTime.now

public static LocalDateTime now()
Obtains the current date-time from the system clock in the default time-zone.

The LocalDateTime class is a date-time without a time-zone.

System.out.println(ZoneId.systemDefault()); // America/Los_Angeles

// Obtains a current date-time with a default time-zone.
final var now = LocalDateTime.now();

// The now object itself does not have the time zone.
System.out.println(now); // 2023-01-04T02:11:45.077924900

You can also use the now method to get the current time from the LocalDate class and the LocalTime.

// Obtains a current date with a default time-zone.
final var now = LocalDate.now();
System.out.println(now); // 2023-01-04
// Obtains a current time with a default time-zone.
final var now = LocalTime.now();
System.out.println(now); // 02:11:45.084920500

Clock

A clock providing access to the current instant, date and time using a time-zone.

The Clock class can also:

  • Always returns the same time.
  • Returns the time with the specified duration added.
final var clock = Clock.systemDefaultZone();

final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2023-10-10T23:01:47.900954200-07:00[America/Los_Angeles]

final var instant = clock.instant();
System.out.println(instant); // 2023-10-11T06:01:47.901953200Z
final var clock = Clock.systemUTC();

final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2023-10-11T06:01:47.907950300Z

final var instant = clock.instant();
System.out.println(instant); // 2023-10-11T06:01:47.907950300Z
final var dateTime = ZonedDateTime.of(1999, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
final var fixedClock = Clock.fixed(dateTime.toInstant(), dateTime.getZone());

{
    final var now = ZonedDateTime.now(fixedClock);
    System.out.println(now); // 1999-12-31T00:00Z

    final var instant = fixedClock.instant();
    System.out.println(instant); // 1999-12-31T00:00:00Z
}
{
    final var offsetClock = Clock.offset(fixedClock, Duration.ofDays(1));

    final var now = ZonedDateTime.now(offsetClock);
    System.out.println(now); // 2000-01-01T00:00Z

    final var instant = offsetClock.instant();
    System.out.println(instant); // 2000-01-01T00:00:00Z
}

System.currentTimeMillis

public static long currentTimeMillis()
Returns the current time in milliseconds.

The currentTimeMillis method obtains the difference, measured in milliseconds, between the current time and 1970-01-01T00:00:00Z.

final var millis = System.currentTimeMillis();
System.out.println(millis); // 1672819545517

final var now = Instant.ofEpochMilli(millis);
System.out.println(now); // 2023-01-04T08:05:45.517Z

// Converts the now object to a ZonedDateTime object with a default time-zone.
final var zoneId = ZoneId.systemDefault();
System.out.println(zoneId); // America/Los_Angeles

final var dateTime = ZonedDateTime.ofInstant(now, zoneId);
System.out.println(dateTime); // 2023-01-04T00:05:45.517-08:00[America/Los_Angeles]

Warning :

  • There is a similar API that is System.nanoTime. However, the nanoTime method cannot be used to obtain the current time. The nanoTime method can only be used to measure elapsed time.

Conclusion

  • You can get the current time, e.g the Instant.now method, the ZonedDateTime.now method etc. on Java 8 or later.
  • It is not recommended to use the legacy APIs the Calendar class and the Date class.

Related posts

To top of page