java convert java.util.Date to LocalDate

www.l‮a‬utturi.com
java convert java.util.Date to LocalDate

To convert a java.util.Date object to a java.time.LocalDate object in Java, you can use the toInstant method of the Date class to convert the Date object to an Instant object, and then use the atZone method to convert the Instant object to a ZonedDateTime object. Finally, you can use the toLocalDate method to convert the ZonedDateTime object to a LocalDate object.

Here is an example of how you can convert a java.util.Date object to a java.time.LocalDate object in Java:

Date date = new Date();
Instant instant = date.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
LocalDate localDate = zdt.toLocalDate();

In this example, we have a Date object called date that represents a specific date and time. We use the toInstant method to convert the Date object to an Instant object, which represents a point in time in the UTC time zone. We then use the atZone method to convert the Instant object to a ZonedDateTime object, which represents a specific date, time, and time zone. Finally, we use the toLocalDate method to convert the ZonedDateTime object to a LocalDate object, which represents just a date without a time or time zone.

Created Time:2017-11-03 00:14:52  Author:lautturi