Languages
[Edit]
EN

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

6 points
Created by:
Yusef-Ewing
239

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

 

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