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

By | 2018-11-16T04:09:28+00:00 November 16th, 2018|java8|0 Comments

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 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment