Hey, Tea lovers! This short post is about how you can easily convert the milliseconds to 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.
You can follow me on social media via @coderstea on Twitter, Linkedin, Facebook, or Instagram. We also share high-quality videos about programming on our Youtube channel. You can also publish your own post on CodersTea.com, just share your thought on Contact Us or let us know in the comments.
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 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);
The milliseconds refer to
Fri Mar 05 2021 06:43:14 in UTC time. 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);
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
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);
Convert LocalDate to Milliseconds in Java
This conversion is a little tricky. You don’t have
toInstant method or such in 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 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);
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 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!!!