Languages
[Edit]
EN

Java - convert LocalDateTime to Date (java.util.Date)

6 points
Created by:
Yusef-Ewing
479

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
    }
}

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java conversion

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join