I want to convert java.time.LocalDate
into java.util.Date
type. Because I want to set the date into JDateChooser
. Or is there any date chooser that supports java.time
dates?

- 18,660
- 9
- 81
- 112

- 4,668
- 2
- 17
- 20
-
1Have you read http://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate ? – Daksh Shah Apr 08 '14 at 06:36
-
2**LGoodDatePicker** natively uses the java.time package (aka Java 8 time, or [JSR-310](http://www.threeten.org/). Specifically, LGoodDatePicker uses a "java.time.LocalDate" to store the date values. Screenshots and a demo are at the [Project Homepage](https://github.com/LGoodDatePicker/LGoodDatePicker). – BlakeTNC Dec 05 '16 at 20:18
-
More answers can be found on: [*LocalDate to java.util.Date and vice versa simplest conversion?*](https://stackoverflow.com/q/33066904/642706) – Basil Bourque Oct 01 '19 at 21:07
13 Answers
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
That assumes your date chooser uses the system default timezone to transform dates into strings.

- 678,734
- 91
- 1,224
- 1,255
-
I do not know why but using this conversion I get wrong results for dates before April 1893 – cornz Jun 27 '17 at 18:36
-
5Is it possible to avoid `atStartOfDay()`, since it changes the value of the date, as I understand it. – yegor256 Oct 18 '17 at 11:08
-
1@yegor256 your comment doesn't make much sense to me. Why don't you ask your own question instead of commenting on a very old one? – JB Nizet Oct 18 '17 at 11:16
-
5@JBNizet your answer doesn't make much sense to me that's why I decided to clarify. Why don't you clarify it instead of making useless comments? – yegor256 Oct 18 '17 at 11:25
-
14Because I don't see how and why it would need any clarification. 232 people upvoted this answer, thus finding it clear. You say atStartOfDay changes the value of the date. That doesn't make sense. atStartOfDay does what the javadoc says it does: it transforms a LocalDate into a LocalDateTime, on the same date, and at the start of the day. – JB Nizet Oct 18 '17 at 12:13
-
3@cornz Are you sure your results are incorrect? If you're in Germany, then it might be something to do with the 6 minute 32 second glitch in the time in Germany, in April 1893. See https://www.timeanddate.com/time/zone/germany/berlin?syear=1850 for some details. – Dawood ibn Kareem Nov 04 '18 at 20:19
-
This jsut works on android "O" and above. Iwant this method to get rid of LocalDate that can not support in my app, so this method is bring me same issue. – Mahdi Mar 12 '20 at 03:07
Here's a utility class I use to convert the newer java.time
classes to java.util.Date
objects and vice versa:
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class DateUtils {
public static Date asDate(LocalDate localDate) {
return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
}
public static Date asDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
public static LocalDate asLocalDate(Date date) {
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
}
public static LocalDateTime asLocalDateTime(Date date) {
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
}
}
Edited based on @Oliv comment.

- 10,029
- 11
- 83
- 152

- 10,110
- 9
- 60
- 69
-
Isn't using `ZoneId.systemDefault()` problematic because timezones change over the corse of the year. So if on 01-Jan I'm in timezone -05:00 (central), but then on 01-July I'm in the timezone -06:00 (central daylight) won't that cause inaccurate results because of daylight savings time? – jhilden Sep 24 '18 at 13:18
Disclaimer: For illustrating existing java apis only. Should not be used in production code.
You can use java.sql.Date.valueOf()
method as:
Date date = java.sql.Date.valueOf(localDate);
No need to add time and time zone info here because they are taken implicitly.
See LocalDate to java.util.Date and vice versa simplest conversion?

- 7,206
- 8
- 33
- 42
-
This was perfect for me, since what I needed was to pass LocalDate into SQL. However, if your Date formatter is going to do something about time zone shift before formatting the date, you might get one day too early depending on the local time zone, in which case you'll want one of the above options instead. – Tom Dibble Oct 07 '15 at 21:54
-
12`java.sql.Date` is meant for the database layer, JDBC, JPA. The web layer (or any client application) should absolutely be free of any dependency from `java.sql.*`. – Tiny Jan 31 '16 at 07:49
-
2@Tiny java.sql.Date resides in rt.jar. There are no any external dependencies. You just use language features. – George Feb 01 '16 at 09:40
-
2`java.sql.Date` is just `java.util.Date` with its time set to `00:00:00` but the point in design perspective is that `java.sql.*` is not meant for a front layer which clients interact with like Servlets / JSP. [`java.util.Date` in Java side and `java.sql.Timestamp` or whatever applicable from `java.sql.*` in JDBC side](http://stackoverflow.com/a/3323870/1391249). – Tiny Feb 01 '16 at 10:29
-
5This is a "horrible hack" according with the java.time.* author: https://stackoverflow.com/questions/33066904/localdate-to-java-util-date-and-vice-versa-simplest-conversion#comment72769769_33066935. In Java 9 `java.sql.*` classes will be a separate dependency. – Dherik Dec 15 '17 at 17:25
-
1
-
7If it is horrible hack why does not java include this simple stuff in java.util.. Day by day java is getting crazier than b4 – Aadam Sep 16 '19 at 04:05
-
I do actually like this hack, and e.g. baudelang is mentioning there as well, without any issues or comments, that it may be problematic (hack). Check yourself: https://www.baeldung.com/java-date-to-localdate-and-localdatetime#localdate-date – DelphyM May 05 '23 at 11:56
java.time has the Temporal interface which you can use to create Instant objects from most of the the time classes. Instant represents milliseconds on the timeline in the Epoch - the base reference for all other dates and times.
We need to convert the Date into a ZonedDateTime, with a Time and a Zone, to do the conversion:
LocalDate ldate = ...;
Instant instant = Instant.from(ldate.atStartOfDay(ZoneId.of("GMT")));
Date date = Date.from(instant);

- 2,306
- 25
- 33
In order to create a java.util.Date from a java.time.LocalDate, you have to
- add a time to the LocalDate
- interpret the date and time within a time zone
- get the number of seconds / milliseconds since epoch
- create a java.util.Date
The code might look as follows:
LocalDate localDate = LocalDate.now();
Date date = new Date(localDate.atStartOfDay(ZoneId.of("America/New_York")).toEpochSecond() * 1000);
-
1
-
localDate.atStartOfDay() creates a ZonedDateTime, but there is no toEpochSecond() method for ZonedDateTime. – Kevin Sadler Dec 02 '14 at 10:30
-
@KevinSadler: The method `toEpochSecond` is inherited from `java.time.chrono.ChronoZonedDateTime`. See http://docs.oracle.com/javase/8/docs/api/java/time/chrono/ChronoZonedDateTime.html#toEpochSecond-- – nosid Dec 02 '14 at 17:57
-
@nosid Thank you for your correction. When I use code completion in Eclipse it (and toInstance) isn't present as an option. But if I type it in full it seems to be accepted. I had wrongly concluded it wasn't a method because of this fact and that I didn't see it on the Javadoc for ZonedDateTime, as it is listed as an inherited method, as you say. Sorry, please accept an upclick :) – Kevin Sadler Dec 02 '14 at 21:31
This works for me:
java.util.Date d = new SimpleDateFormat("yyyy-MM-dd").parse(localDate.toString());
https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#toString--

- 6,143
- 10
- 56
- 78
-
8
-
2Converting to Instant might be verbose, but building and parsing a String is like going from New York to Mexico City via Tokyo... – ehecatl Apr 18 '18 at 18:19
-
2@ehecatl Beware... in the future NY -> Tokyo -> Mexico City may be done within hour(s) ;) – Stephan May 15 '18 at 09:57
-
1this works better for my use cases when I no longer have the timezone info at the time of conversion. E.g. work with Freemaker to print the date. – Weishi Z Dec 23 '21 at 19:59
-
this is wrong solution because SimpleDateFormat is not thread safe – Oleg Ushakov Feb 20 '23 at 17:22
-
@OlegUshakov if it's inlined like this, then thread-safety will not fail – EpicPandaForce Apr 12 '23 at 03:19
Kotlin Solution:
1) Paste this extension function somewhere.
fun LocalDate.toDate(): Date = Date.from(this.atStartOfDay(ZoneId.systemDefault()).toInstant())
2) Use it, and never google this again.
val myDate = myLocalDate.toDate()

- 4,530
- 5
- 29
- 37
-
20
-
8Converting the Java LocalDate to Date is a common, annoying problem for any JVM developer. This is a solution for Kotlin developers. – gyoder Apr 19 '18 at 20:05
-
1
public static Date convertToTimeZone(Date date, String tzFrom, String tzTo) {
return Date.from(LocalDateTime.ofInstant(date.toInstant(), ZoneId.of(tzTo)).atZone(ZoneId.of(tzFrom)).toInstant());
}

- 1
- 2
LocalDate date = LocalDate.now();
DateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
try {
Date utilDate= formatter.parse(date.toString());
} catch (ParseException e) {
// handle exception
}

- 307
- 5
- 11
Try this:
public Date convertFrom(LocalDate date) {
return Date.valueOf(date);
}

- 7
- 3
-
1The valueOf method is a member of java.sql.Date, and the question specifies java.util.Date. – nasch Jul 19 '22 at 19:28
Simple
public Date convertFrom(LocalDate date) {
return java.sql.Timestamp.valueOf(date.atStartOfDay());
}

- 107
- 5
localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));

- 13
- 2
-
Your answer could be improved by adding additional information on what the code does and how it helps the OP. – Tyler2P Feb 13 '22 at 12:13
-
java.util.Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());

- 411
- 7
- 14
-
how your answer is different from accepted one after 4 years? Copy-paste to achieve reputation increase? – stinger Mar 04 '19 at 06:24