How to convert Joda LocalDate to java.util.Date?

JavaDateJodatime

Java Problem Overview


What is the simplest way to convert a JodaTime LocalDate to java.util.Date object?

Java Solutions


Solution 1 - Java

JodaTime

To convert JodaTime's org.joda.time.LocalDate to java.util.Date, do

Date date = localDate.toDateTimeAtStartOfDay().toDate();

To convert JodaTime's org.joda.time.LocalDateTime to java.util.Date, do

Date date = localDateTime.toDate();

JavaTime

To convert Java8's java.time.LocalDate to java.util.Date, do

Date date = Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());

To convert Java8's java.time.LocalDateTime to java.util.Date, do

Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

You might be tempted to shorten it with LocalDateTime#toInstant(ZoneOffset), but there isn't a direct API to obtain the system default zone offset.

To convert Java8's java.time.ZonedDateTime to java.util.Date, do

Date date = Date.from(zonedDateTime.toInstant());

Solution 2 - Java

Since 2.0 version LocalDate has a toDate() method

Date date = localDate.toDate();

If using version 1.5 - 2.0 use:

Date date = localDate.toDateTimeAtStartOfDay().toDate();

On older versions you are left with:

Date date = localDate.toDateMidnight().toDate();

Solution 3 - Java

You will need a timezone.

LocalDate date = ...

Date utilDate = date.toDateTimeAtStartOfDay( timeZone ).toDate( );

Solution 4 - Java

Maybe this?

localDate.toDateTimeAtCurrentTime().toDate();

Solution 5 - Java

Try this.

new Date(localDate.toEpochDay())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEric WilsonView Question on Stackoverflow
Solution 1 - JavaBalusCView Answer on Stackoverflow
Solution 2 - JavamaraswronaView Answer on Stackoverflow
Solution 3 - JavaAlexander PogrebnyakView Answer on Stackoverflow
Solution 4 - JavaSean Patrick FloydView Answer on Stackoverflow
Solution 5 - JavaAlex SalesView Answer on Stackoverflow