EN
Java - convert LocalDateTime to Date (java.util.Date)
6
points
Hi, today I would like to show you how to convert LocalDateTime to Date in Java.
Quick solution:
LocalDateTime localDate = LocalDateTime
.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
Date date = Date.from(localDate.atZone(ZoneId.systemDefault()).toInstant());
In this example we use:
- java.util.Date
- java.time.LocalDateTime
Full example with imports:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class Example {
public static void main(String[] args) {
LocalDateTime localDate = LocalDateTime
.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
Date date = Date.from(localDate.atZone(ZoneId.systemDefault()).toInstant());
System.out.println(date); // Sat Dec 19 16:50:17 CET 2020
}
}