Home Java Convert Milliseconds to & from LocalDateTime in Java8 & Above

Convert Milliseconds to & from LocalDateTime in Java8 & Above

Published: Last Updated on 1 comment

Hey, Tea lovers! This short post is about how you can easily convert the milliseconds to a LocalDateTime Object. This is a very short guide on how to create LocalDateTime via milliseconds. I will also show you the opposite, that is, converting LocalDateTime to milliseconds. Note that, if you want to convert it to LocalDate instead of LocalDateTime, the process is similar.

Requirement for the Conversions

Well, you will only require Java. Since LocalDateTime or LocalDate in java was introduced from Java 1.8, you will require Java 8 or higher.


I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.


Milliseconds to LocalDateTime Object

To convert the milliseconds to LocalDateTime you will need an Instant Object. Instant is a very useful class for any time-related functionality in Java, but let’s talk about it in another post. For our use case, we will use Instant.ofEpochMilli the function. Then we will attach a ZoneId, we will use the default but you can add any Zone preferable to you. At last call the toLocalDateTime method to get our desired object. Let’s see the example below.

long millis = 1614926594000L; // UTC Fri Mar 05 2021 06:43:14
LocalDateTime dateTime = Instant.ofEpochMilli(millis)
        .atZone(ZoneId.systemDefault()) // default zone
        .toLocalDateTime(); // returns actual LocalDateTime object

System.out.println("The Millisecond " + millis + " refers to Time " + dateTime);Code language: JavaScript (javascript)

The milliseconds refer to Fri Mar 05 2021 06:43:14 in UTC. But since we used the system’s timezone, the final result may differ according to your timezone.

Convert LocalDateTime to Milliseconds in Java

Now, let us reverse the requirement. We will convert the LocalDateTime object to milliseconds. LocalDateTime doesn’t have a direct function, you have to get the Instant from it. For that, use atZone to set the timezone, system default in our case, then we will use toInstant and call toEpochMilli to get the milliseconds. The example is shown below.

LocalDateTime dateTime1 = LocalDateTime.of(2021, 3, 5, 6, 43, 14);
long millis1 = dateTime1.atZone(ZoneId.systemDefault()) // timezone
        .toInstant() // Instant object
        .toEpochMilli(); // milliseconds

System.out.println("The milliseconds for date " + dateTime1 + " is " + millis1);Code language: JavaScript (javascript)

Convert Milliseconds to LocalDate

As I said earlier, the process is similar, we just need to call the LocalDate object instead of LocalDateTime. Here is the example, with the same code as shown in the earlier example, but this time with the LocalDate object.

long millis = 1614926594000L; // UTC Fri Mar 05 2021 06:43:14
LocalDate dateTime = Instant.ofEpochMilli(millis)
        .atZone(ZoneId.systemDefault()) // default zone
        .toLocalDate(); // returns actual LocalDate object

System.out.println("The Millisecond " + millis + " refers to date" + dateTime);Code language: JavaScript (javascript)

Convert LocalDate to Milliseconds in Java

This conversion is a little tricky. You don’t have toInstant a method or such in the LocalDate class. And the one I will show you returns seconds instead of milliseconds. So we have to convert it to milliseconds by multiplying it by a thousand (1000). Let’s see the example.

LocalDate dateTime1 = LocalDate.of(2021, 3, 5);
long seconds = dateTime1.atStartOfDay(ZoneId.systemDefault())
        .toEpochSecond(); // returns seconds
long millis1 = seconds * 1000; // seconds to milliseconds

System.out.println("The milliseconds for date " + dateTime1 + " is " + millis1);Code language: JavaScript (javascript)

Conclusion

Hope you liked the post and it was helpful to you. The full code can be found on GitHub.

If you want to write such a short tutorial yourself, please contact us via the Contact Us page, comment below or you can reach us out on our social media accounts @coderstea (all Social Media websites).

See you in the next post. HAKUNA MATATA!!!


I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.


Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ads
Ads
Ads

@2023 All Right Reserved. Designed and Developed by CodersTea

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More