How to convert from Date to LocalDate when using ThreeTenABP?
Asked Answered
J

1

13

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 = ???
Jocular answered 16/7, 2015 at 11:52 Comment(0)
J
24

This should do it (inspired by https://mcmap.net/q/64120/-convert-java-util-date-to-java-time-localdate).

Date dateJavaFormat = new Date();
LocalDate dateThreeTenFormat = Instant.ofEpochMilli(dateJavaFormat.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
Jocular answered 16/7, 2015 at 11:52 Comment(1)
Thank you. Dates complexity on android is ever puzzling.Noh

© 2022 - 2024 — McMap. All rights reserved.