Converting between java time LocalDateTime and java util Date

0 votes
Java 8 has a completely new API for date and time. One of the most useful classes in this API is LocalDateTime, for holding a timezone-independent date-with-time value.

There are probably millions of lines of code using the legacy class java.util.Date for this purpose. As such, when interfacing old and new code there will be a need for converting between the two. As there seems to be no direct methods for accomplishing this, how can it be done?
Feb 6, 2019 in Java by Sushmita
• 6,920 points
2,919 views

2 answers to this question.

0 votes

Here is what I came up with ( and like all Date Time conundrums it is probably going to be disproved based on some weird timezone-leapyear-daylight adjustment :D )

Round-tripping: Date <<->> LocalDateTime

Given: Date date = [some date]

(1) LocalDateTime << Instant<< Date

    Instant instant = Instant.ofEpochMilli(date.getTime());
    LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);

(2) Date << Instant << LocalDateTime

    Instant instant = ldt.toInstant(ZoneOffset.UTC);
    Date date = Date.from(instant);

Example:

Given:

Date date = new Date();
System.out.println(date + " long: " + date.getTime());

(1) LocalDateTime << Instant<< Date:

Create Instant from Date:

Instant instant = Instant.ofEpochMilli(date.getTime());
System.out.println("Instant from Date:\n" + instant);

Create Date from Instant (not necessary,but for illustration):

date = Date.from(instant);
System.out.println("Date from Instant:\n" + date + " long: " + date.getTime());

Create LocalDateTime from Instant

LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
System.out.println("LocalDateTime from Instant:\n" + ldt);

(2) Date << Instant << LocalDateTime

Create Instant from LocalDateTime:

instant = ldt.toInstant(ZoneOffset.UTC);
System.out.println("Instant from LocalDateTime:\n" + instant);

Create Date from Instant:

date = Date.from(instant);
System.out.println("Date from Instant:\n" + date + " long: " + date.getTime());

The output is:

Fri Nov 01 07:13:04 PDT 2013 long: 1383315184574

Instant from Date:
2013-11-01T14:13:04.574Z

Date from Instant:
Fri Nov 01 07:13:04 PDT 2013 long: 1383315184574

LocalDateTime from Instant:
2013-11-01T14:13:04.574

Instant from LocalDateTime:
2013-11-01T14:13:04.574Z

Date from Instant:
Fri Nov 01 07:13:04 PDT 2013 long: 1383315184574
answered Feb 6, 2019 by developer_1
• 3,350 points
0 votes

Try this:

Date in = new Date();
LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault());
Date out = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
answered Aug 27, 2019 by Sirajul
• 59,230 points

Related Questions In Java

0 votes
2 answers

How do I get the current date and time using Java?

If you require a time stamp in ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 59,230 points
2,985 views
0 votes
3 answers

Setting time zone of a java.util.Date

Using Java 8 Java 8 introduced a new Date-Time ...READ MORE

answered Dec 15, 2020 in Java by Gitika
• 65,770 points
56,905 views
0 votes
1 answer

Converting ISO 8601-compliant String to java.util.Date

Unfortunately, the time zone formats available to SimpleDateFormat (Java ...READ MORE

answered Nov 27, 2018 in Java by Daisy
• 8,140 points

edited Oct 7, 2021 by Sarfaraz 9,809 views
+2 votes
2 answers

Can you give me the proper difference between java.util.Date vs java.sql.Date ?

 java.sql.Date is a thin wrapper around millisecond value ...READ MORE

answered Aug 2, 2019 in Java by sampriti
• 1,120 points
15,519 views
0 votes
2 answers

Date format conversion in Java

Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 1); Date date = ...READ MORE

answered Aug 13, 2018 in Java by Sushmita
• 6,920 points
1,103 views
0 votes
3 answers

Change date format in a Java string

The reason for the inaccuracy is because ...READ MORE

answered Feb 9, 2022 in Java by Soham
• 9,710 points
3,433 views
0 votes
1 answer

Unexpected result on Subtracting Times

ou've encountered a local time discontinuity: When local standard ...READ MORE

answered May 29, 2018 in Java by Rishabh
• 3,620 points
612 views
0 votes
1 answer

How can I get the current date and time in UTC or GMT in Java?

This definitely returns UTC time: as String ...READ MORE

answered Jun 7, 2018 in Java by Rishabh
• 3,620 points
27,652 views
0 votes
1 answer

difference between wait() and sleep() in java

We can find a big difference between ...READ MORE

answered Apr 26, 2018 in Java by developer_1
• 3,350 points
1,255 views
0 votes
1 answer

Converting String and Float Data-types

Using Java’s Float class. float f = Float.parseFloat("25"); String s = ...READ MORE

answered May 9, 2018 in Java by developer_1
• 3,350 points
850 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP