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?
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
That assumes your date chooser uses the system default timezone to transform dates into strings.
atStartOfDay()
, since it changes the value of the date, as I understand it. –
Jointure 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.
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? –
Edo 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?
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.*
. –
Kalagher 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. –
Kalagher java.sql.*
classes will be a separate dependency. –
Mesentery 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);
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);
toEpochSecond
is inherited from java.time.chrono.ChronoZonedDateTime
. See docs.oracle.com/javase/8/docs/api/java/time/chrono/… –
Selfimprovement 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--
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()
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());
}
LocalDate date = LocalDate.now();
DateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
try {
Date utilDate= formatter.parse(date.toString());
} catch (ParseException e) {
// handle exception
}
Try this:
public Date convertFrom(LocalDate date) {
return Date.valueOf(date);
}
Simple
public Date convertFrom(LocalDate date) {
return java.sql.Timestamp.valueOf(date.atStartOfDay());
}
localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
java.util.Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
© 2022 - 2024 — McMap. All rights reserved.