The Universal Time Coordinated(UTC) is the primary time standard by which the world regulates time. Java 8 java.time
API provides multiple options to handle UTC value.
You can use java.time.Instant.now()
to obtain the current UTC time.
In this tutorial, you will learn about the different ways to get UTC Date values using Java 8 in detail.
Using Instant
The Java 8 Instant class provides the current instant time represents a precise point in time specific to nanosecond and microsecond.
The Instant.now()
method will always return the current time aligned with the UTC time zone, even if the application runs in a different time zone.
To get time in UTC, you can always use the Instant.now()
method, as other methods will also use this method to get the time based on the UTC time zone. Also, Instant can be used to get the event-based time.
Code
import java.time.Instant;
//main
Instant instant = Instant.now();
System.out.println(instant); //2023-02-18T19:17:53.361322400Z
You can check this tutorial to convert an Instant to ZonedDateTime object, How to convert an Instant to Java 8 ZonedDateTime
Using ZonedDateTime
The Java 8 ZonedDateTime is an immutable representation of date-time with time-zone value.
The ZonedDateTime.now()
method returns the current time based on the system’s default time zone.
Use this method for applications that require Zone specific time.
To get UTC time using the ZonedDateTime,
- Use the ZonedDateTime.now() to get the current time.
- Pass the ZoneOffset value(
ZoneOffset.UTC
), and convert the current system default time to UTC time. - An UTC ZonedDateTime value will be returned.
Code
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
//main
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime); //2023-02-19T06:17:53.355325100+11:00[Australia/Sydney]
ZonedDateTime utcTime = ZonedDateTime.now(ZoneOffset.UTC);
System.out.println(utcTime); //2023-02-18T19:17:53.360329600Z
The first example is returned with the system default time zone value, and after the ZoneOffset.UTC
is applied, it returns the UTC time value.
Using OffsetDateTime
The Java 8 OffsetDateTime is an immutable representation of date-time with an offset.
The OffsetDateTime.now()
method returns the current time based on the system’s default time zone.
To get a UTC time-based OffsetDateTime value,
- Use the OffsetDateTime .now() to obtain the current time.
- Pass the ZoneId value(
ZoneId.of("Z")
), and convert the current system default time to UTC time. - An UTC OffsetDateTime value will be returned.
Use this method when the application requires handling time based on different time-zone offsets specific to other regions.
Code
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
//main
OffsetDateTime offsetDateTime = OffsetDateTime.now();
System.out.println(offsetDateTime); //2023-02-19T06:17:53.370319600+11:00
OffsetDateTime utcTime= OffsetDateTime.now(ZoneId.of("Z"));
System.out.println(utcTime); //2023-02-18T19:17:53.370319600Z
OffsetDateTime utcTime1 = OffsetDateTime.of(1991,1,1,0,0,0,0, ZoneOffset.UTC);
System.out.println(utcTime1); //1991-01-01T00:00Z
You can check this tutorial to convert an Instant to OffsetDateTime object, How to convert an Instant to Java 8 OffsetDateTime
Using LocalDateTime
The Java 8 LocalDateTime represents date-time without time-zone value.
The LocalDateTime.now()
method returns the current date-time value with the system default time-zone value.
To get the UTC time-based LocalDateTime value,
- Use the
LocalDateTime.now()
to get the current date-time - Pass the ZoneOffset
ZoneOffset.UTC
/ ZoneId(ZoneId(ZoneId.of("Z")
) to convert the system default time zone to UTC. - An UTC-specific LocalDateTime value will be returned.
Use this method for scenarios with date and time values without time zone expectations.
Code
import java.time.LocalDateTime;
import java.time.ZoneOffset;
//main
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime); //2023-02-19T06:17:53.370319600
LocalDateTime utcTime = LocalDateTime.now(ZoneOffset.UTC);
System.out.println(utcTime); //2023-02-18T19:17:53.371319800