What is the difference between Java 8 ZoneOffset.UTC and ZoneId.of(“UTC”)

Java 8 provides various options to convert current time to time-zone specific value.

The ZoneOffset.UTC and ZoneId.of(“UTC”) both are representations of UTC time zone but they have different purposes. ZoneOffset.UTC represents fixed offset from UTC of zero seconds whereas ZoneId.of(“UTC”) represents the UTC time zone itself, including its historical rules and transitions. The main difference between them is that the ZoneId class contains more information about the time zone, such as its abbreviation and the rules for daylight saving time.

This tutorial will help you learn about the differences between Java 8 ZoneOffset.UTC and ZoneId.of(“UTC”) in detail.

ZoneId.of(“UTC”)

The Java 8 ZoneId is a time-zone ID used to identify the rules to convert between Instant and LocalDateTime values.
There are two distinct types of ID,

  • Fixed Offset – An fully resolved offset value from UTC. Fixed offset is represented by ZoneOffset. Example: offset value: +03:00 from UTC.
  • Geographical regions/ – A specific set of rules for finding an offset from UTC. Example: zoneId: America/Costa_Rica

ZoneId.of(‘UTC’) should be used at most of the places instead of ZoneOffset.UTC becasue it provides more information about the time zone.

Syntax

    static ZoneId of(String zoneId)

The ZoneId.of("UTC") is equivalent to ZoneOffset.UTC, which identifies a set of rules based on the ID. Rules are definitions of how offsets vary from place and time every year. For Instance, Paris’s time difference from UTC is +01:00 in winter, but in summer, it will be +02:00.
ZoneId captures all the rules for every region around the world.

Code

    import java.time.ZoneId;   
    import java.time.ZonedDateTime;

    //main
    ZonedDateTime currentTime = ZonedDateTime.now();
    System.out.println(currentTime);  //2023-01-31T19:27:17.105737700Z[Europe/London]

    ZonedDateTime zonedDateTime = currentTime.withZoneSameInstant(ZoneId.of("UTC"));
    System.out.println(zonedDateTime);  //2023-01-31T19:27:17.105737700Z[UTC]

This returns a current time value converting to UTC, applying the rules specific to the UTC region.

Using the code below, you can also get a normalized version of the current time value, which will proceed to a time value equivalent to ZoneOffset.UTC.
Code

    //main
    ZonedDateTime zonedDateTime = currentTime.withZoneSameInstant(ZoneId.of("UTC").normalized());  
    System.out.println(zonedDateTime);  //2023-01-31T19:27:17.105737700Z

    ZonedDateTime zonedDateTime1 = currentTime.withZoneSameInstant(ZoneOffset.UTC);  
    System.out.println(zonedDateTime1);  //2023-01-31T19:27:17.105737700Z

    System.out.println(zonedDateTime1.equals(zonedDateTime));  //true

ZoneOffset.UTC

The Java ZoneOffset class extends from ZoneId and is a time-zone offset from UTC. The time-zone offset is used to denote the time difference from UTC/Greenwich.

Example: offset value +03:00 is used to add 3 hours from UTC.

ZoneOffset.UTC can be used in places to have fixed offset.

Code

    import java.time.ZoneOffset;  
    import java.time.ZonedDateTime;

    //main
    ZonedDateTime currentTime = ZonedDateTime.now();  
    System.out.println(currentTime);  //2023-01-31T19:27:17.105737700Z[Europe/London]

    ZonedDateTime zonedDateTime = currentTime.withZoneSameInstant(ZoneOffset.UTC);
    System.out.println(zonedDateTime);  //2023-01-31T19:27:17.105737700Z

The above code also converts the current time value to UTC-specific time.

Related Topics

Leave a Comment