Your code is basically correct and would have worked with java.time.LocalDate
, only not with the implementation of the same class in org.threeten.bp.LocalDate
. So your options are two:
- Change all of your imports to use
java.time
instead of org.threeten.bp
and stop using the backport.
- Use
org.threeten.bp.DateTimeUtils
for conversions between legacy date-time classes and the classes in ThreeTen Backport.
Example of option 2.:
LocalDate currentDate = LocalDate.now(ZoneId.of("America/Whitehorse"));
Date d = DateTimeUtils.toDate(
currentDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
System.out.println("" + currentDate + " was converted to " + d);
When running on my computer just now this snippet printed:
2019-06-25 was converted to Tue Jun 25 00:00:00 CEST 2019
DateTimeUtils
also has a toInstant(Date)
method for the opposite conversion.
Link: DateTimeUtils
documentation