Convert OffsetDateTime to ZonedDateTime

Convert OffsetDateTime to ZonedDateTime

0 Comments

Java 8 introduced a new date and time API defined inside java.time package. The key date and time classes defined in this package are LocalDateTime, ZonedDateTime, and OffsetDateTime.

OffsetDateTime represents a date-time with an offset. This class stores all date and time fields, to a precision of nanoseconds, as well as the offset from UTC/Greenwich. For example, the value 2nd December 2018 at 15:35.40.123456789 +03:00 can be stored in an OffsetDateTime.

On the other hand, ZonedDateTime represents a date-time with a time-zone. An example is 2007-12-03T10:15:30+01:00 Europe/Paris.

Thus the difference between OffsetDateTime and ZonedDateTime is that the latter includes the rules that cover daylight saving time adjustments and various other anomalies.

In this post, I’ll discuss how to convert OffsetDateTime to ZonedDateTime.

Approach 1: Converting OffsetDateTime To ZonedDateTime

The code for converting OffsetDateTime to ZonedDateTime is this.

public ZonedDateTime convertToZonedDateTime() {

  OffsetDateTime offsetDateTime = OffsetDateTime.now();

  ZonedDateTime zonedDateTime1 = offsetDateTime.toZonedDateTime();

  System.out.println(zonedDateTime1); 

  return zonedDateTime1;

}

In the preceding code, OffsetDateTime.now() gets the current date-time in the default time-zone with an offset. Then offsetDateTime.toZonedDateTime() converts the current date-time with offset to a ZonedDateTime using the offset as the zone ID.

Now, let’s call this method from our main class.

DateConverter dateConvertor = new DateConverterImpl();

dateConvertor.convertToZonedDateTime();

On running the application, the output is this.

2020-06-21T16:40:20.348877+05:30

Approach 2: Converting OffsetDateTime To ZonedDateTime

Next, I’ll convert OffsetDateTime to atZone Same Instant.

The code is this.

ZoneId zoneId = ZoneId.of("Asia/Kolkata");

public ZonedDateTime convertToAtZoneSameInstant() {

  OffsetDateTime offsetDateTime = OffsetDateTime.now();

  ZonedDateTime zonedDateTime2 = offsetDateTime.atZoneSameInstant(zoneId);

  System.out.println(zonedDateTime2);

  return zonedDateTime2;

}

The code obtains the zone id that represents the time-zone of Asia/Kolkata.

Next, the method atZoneSameInstant(zoneId) combines the current date and time with the zone ID to create a new instance of ZonedDateTime. This method ensures that the result has the same instant.

I’ll call this method from the main method.

dateConvertor.convertToAtZoneSameInstant();

On running the application, the output is this.

2020-06-21T16:44:24.165456+05:30[Asia/Kolkata]

Approach 3: Converting OffsetDateTime To ZonedDateTime

In this approach, we will call the atZoneSimilarLocal(zoneId) method of OffsetDateTime to perform the conversion.

The code is this.

public ZonedDateTime convertToAtZoneSimilarLocal() {

  OffsetDateTime offsetDateTime = OffsetDateTime.now();

  ZonedDateTime zonedDateTime3 = offsetDateTime.atZoneSimilarLocal(zoneId);

  System.out.println(zonedDateTime3);

  return zonedDateTime3;

}

 

The atZoneSimilarLocal(zoneId) method is similar to the atZoneSameInstant(zoneId) method we used above. The difference is that instead of adjusting the time/date to the target zone., his method keeps the same local date and time.

This code calls this method from the main method.

dateConvertor.convertToAtZoneSimilarLocal();

The output on running the application is this.

2020-06-21T16:48:27.929353+05:30[Asia/Kolkata]

Summary

As ZonedDateTime and an OffsetDateTime refer to the same instant, you might end up using them interchangeably. However, they are not interchangeable. One important thing to remember is that writing a ZonedDateTimeto an ANSI SQL database will lose information because ANSI SQL only supports OffsetDateTimes.

Also, ZonedDateTime is now more usable as an alternative to OffsetDateTime if you want to keep it simple.

So if you need to store data and time, use OffsetDateTime and to present date and time to users, use the ZonedDataTime.

The source code for this post can be found here on GitHub.

About SFG Contributor

Staff writer account for Spring Framework Guru

    You May Also Like