Convert OffsetDateTime to LocalDateTime

Convert OffsetDateTime to LocalDateTime

1 Comment

Last Updated on August 1, 2020 by jt

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, LocalDateTime, 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, LocalDateTime represents a date and time without time zone information. An example is 2020-07-12T17:23:10.205580.

In addition to LocalDateTime, you can also use the LocalDate and LocalTime classes to work with date and time without time-zone separately.

An example of LocalDate is 2018-10-05. An example of LocalTime is 15:19:47.459.

In my previous post, I explained how to convert OffsetDateTime to ZonedDateTime.

In this post, I’ll discuss how to convert OffsetDateTime to LocalDateTime, LocalDate, and LocalTime.

Converting OffsetDateTime To LocalDateTime

The code for converting OffsetDateTime to LocalDateTime is this.

public LocalDateTime convertToLocalDateTime() {
   OffsetDateTime offsetDateTime = OffsetDateTime.now();
   System.out.println(offsetDateTime);
   LocalDateTime localDateTime = offsetDateTime.toLocalDateTime();
   System.out.println("localDateTime = " + localDateTime);
   return  localDateTime;
}

In the preceding code, OffsetDateTime.now() gets the current date-time in the default time-zone with an offset. Then offsetDateTime.toLocalDateTime() converts the current date-time LocalDateTime.

Let’s call this method from our main class.

DateConverter dateConvertor = new DateConverterImpl();

dateConvertor.convertToLocalDateTime();

On running the application, the output is this.

2020-07-12T18:51:36.240918

Converting OffsetDateTime To LocalDate

At times, you might want only the local date instead of a full date and time with offset. For that, you need to convert the OffsetDateTime to LocalDate.

The code is this.

public LocalDate convertToLocalDate() {
   OffsetDateTime offsetDateTime = OffsetDateTime.now();
   System.out.println(offsetDateTime);
   LocalDate localDate=offsetDateTime.toLocalDate();
   System.out.println(localDate);
   return  localDate;
}

The output on calling the preceding method is this.

2020-07-12

Converting OffsetDateTime To LocalTime

The OffsetDateTime class also allows conversion to local time.

The code for converting OffsetDateTime to LocalTime is this.

public LocalTime convertToLocalTime() {
   OffsetDateTime offsetDateTime = OffsetDateTime.now();
   System.out.println(offsetDateTime);
   LocalTime localTime=offsetDateTime.toLocalTime();
   System.out.println(localTime);
   return  localTime;
}

The output on calling the preceding method is this.

18:51:36.240918

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

    One comment

    1. August 25, 2021 at 5:42 am

      This totally ignores time zones as toLocalDateTime of OffsetDateTime just strips time zone information.
      If the OffsetTimeZone is GMT and local time zone is GMT+2, 15:19:47 will still be 15:19:47 in LocalDateTime, not 17:19:47, as would be what you’d want most of the time.
      It’s more of a cast than a convert.

      Instead of offsetDateTime.toLocalDateTime();, use offsetDateTime..atZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();

      Reply

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.