In this post, we are going to see how we can convert OffsetDateTime to LocalDateTime and LocalDate, as well as LocalTime in Java. These date types were introduced in Java 8 within the java.time package.
OffsetDateTime represents a date time with an offset. The OffsetDateTime object type stores the complete date and time with nanosecond precision.
The difference between OffsetDateTime and LocalDateTime is that LocalDateTime does not carry Zone information.
On the other hand, the difference between OffsetDateTime and LocalDate is that LocalDate only shows the year, day, and month.
In addition, we will also see how we can convert from an OffsetDateTime to LocalTime. The difference between LocalTime and OffsetDateTime is that LocalTime only returns the time of the OffsetDateTime.
Let’s see different dates with their values:
- OffsetDateTime: 2017-11-02T10: 15: 30 + 01: 00
- LocalDateTime: 2023-06-12T13:30:10.106680
- LocalDate: 2018-10-05
- LocalTime: 20:19:27.109
Convert OffsetDateTime to LocalDateTime in Java
To convert an OffsetDateTime type to LocalDateTime in Java, we will use the toLocalDateTime method. Let’s see it better with an example:
@Test public void given_offsetDateTime_when_convert_to_localDateTime_then_return_localDateTime() { OffsetDateTime offsetDateTime = OffsetDateTime.now(); LocalDateTime localDateTime = offsetDateTime.toLocalDateTime(); assertAll(() -> { assertEquals(offsetDateTime.getYear(), localDateTime.getYear()), assertEquals(offsetDateTime.getMonth(), localDateTime.getMonth()), assertEquals(offsetDateTime.getDayOfMonth(), localDateTime.getDayOfMonth()) }); }
In the previous example, we have used assertAll to compare the month, year, and day of the month.
Convert OffsetDateTime to LocalDate in Java
OffsetDateTime allows us to convert to LocalDate through the toLocalDate method. Let’s see an example with a test:
@Test public void given_offsetDateTime_when_convert_to_localDate_then_return_localDate() { OffsetDateTime offsetDateTime = OffsetDateTime.now(); LocalDate localDate = offsetDateTime.toLocalDate(); assertAll(() -> { assertEquals(offsetDateTime.getMonth(), localDate.getMonth()), assertEquals(offsetDateTime.getMonth(), localDate.getMonth()), assertEquals(offsetDateTime.getDayOfMonth(), localDate.getDayOfMonth()) }); }
Convert OffsetDateTime to LocalTime in Java
OffsetDateTime also allows us to convert to LocalTime, that is, to extract the time from the OffsetDateTime object. To obtain a LocalTime object, we will use the toLocalTime() method..
@Test public void given_offsetDateTime_when_convert_to_localTime_then_return_localTime() { OffsetDateTime offsetDateTime = OffsetDateTime.now(); LocalTime localTime = offsetDateTime.toLocalTime(); assertAll(() -> { assertEquals(offsetDateTime.getHour(), localTime.getHour()), assertEquals(offsetDateTime.getMinute(), localTime.getMinute()), assertEquals(offsetDateTime.getSecond(), localTime.getSecond()) }); }
In the previous test, we have converted an OffsetDateTime object to LocalTime with the toLocalTime() method. To verify that it has been converted correctly, we have used assertAll() to compare the hour, minute, and second.
Conclusion
In this post about how to convert OffsetDateTime to LocalDateTime, LocalDate, and LocalTime in Java, we have seen how easy it is to perform a conversion of type to three objects from the java.time package. The java.time API facilitates conversions of a variety of date types, making date handling much easier and more efficient.
If you need more information, you can leave us a comment or send an email to refactorizando.web@gmail.com You can also contact us through our social media channels on Facebook or twitter and we will be happy to assist you!!