AdBlock Detected

It looks like you're using an ad-blocker!

Our team work realy hard to produce quality content on this website and we noticed you have ad-blocking enabled. Advertisements and advertising enable us to continue working and provide high-quality content.

Convert OffsetDateTime to LocalDate

In this post we are going to see how we can convert OffsetDateTime to LocalDate and LocalDateTime, as well as to 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 the Zone information.

On the other hand, the difference between OffsetDateTime and LocalDate is that LocalDate only shows the year, day, and month date.

Additionally, 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 value:

  • OffsetDateTime: 2017-11-02T10: 15: 30 + 01: 00
  • LocaDateTime: 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, 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 an assertAll() comparing the hour, minute, and second.

Conclusion

In this post on how to convert OffsetDateTime to LocalDate, LocalDateTime, and LocalTime in Java, we have seen how easy it is to perform a type conversion to three objects of the java.time package. The java.time API makes conversions easy and simple through a method to perform the conversion.

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!!

Leave a Reply

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