NOTE: This is answered already excellently in the JDK world here, but the accepted answer doesn't apply to the Android port of JSR-310 which doesn't have that extended API for Date.
So, what is the best way to convert a java.util.Date
to org.threeten.bp.LocalDate
?
Date input = new Date();
LocalDate date = ???
Date date = new Date(); LocalDateTime localDateTime = date. toInstant(). atZone(ZoneId. systemDefault()).
Convert LocalDate To Date in Java Step 1: First Create the LocalDate object using either now() or of() method. now() method gets the current date where as of() creates the LocalDate object with the given year, month and day. Step 2: Next, Get the timezone from operating system using ZoneId. systemDefault() method.
LocalDate is the date the calendar on the wall says. java. util. Date is not a date, it's an instant, and actually represents a millisecond offset from Unix epoch.
This should do it (inspired by https://stackoverflow.com/a/27378709/286419).
Date dateJavaFormat = new Date();
LocalDate dateThreeTenFormat = Instant.ofEpochMilli(dateJavaFormat.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With