Java 8

Java 8 – Convert java.util.Date to java.time.LocalDate

Here I am going to show you how to convert java.util.Date to java.time.LocalDate object.

Convert java.util.Date to java.time.LocalDate:

We can do this in different solutions.

Solution -1 :

Converting using JSR-310 Instant class

Date date = new Date();
LocalDate localeDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

Solution -2 :

Converting using JSR-310 Instant’s  ofEpochMilli()

Date date = new Date();
LocaleDate localeDate = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate()

Solution -3:

Using LocaleDate’s parse()

Date date = new Date();
LocalDate localDate = LocalDate.parse( new SimpleDateFormat("yyyy-MM-dd").format(date));

Solution -4:

Using GregorianCalendar class

Date date = new Date();
GregorianCalendar gregCalendar = (GregorianCalendar) Calendar.getInstance();
gregCalendar .setTime(date);
ZonedDateTime zonedDateTime = gregCalendar .toZonedDateTime();
LocaleDate localeDate = zonedDateTime.toLocalDate();

References:

Date Time Docs

Joda Time

Happy Learning :)

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

Ask a question or leave a comment