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:
- This article may use translation software for your convenience. Please also check the original Japanese version.
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. |
|
ZonedDateTime | ZonedDateTime now() Obtains a current date-time from the system clock in the default time-zone. |
|
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. |
|
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. |
|
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. |
|
Calendar | Calendar getInstance() Obtains a calendar using the default time zone and locale. |
|
Date | Date() Obtains a date from the system UTC clock. |
|
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
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
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
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
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
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
- API Examples
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset