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:
Happy Learning 🙂