Skip to main content

Convert LocalDateTime to Milliseconds in Java

Convert LocalDateTime to Milliseconds in Java 

In this tutorial, we'll see how we can convert LocalDateTime to Milliseconds in java.

Convert LocalDateTime to Milliseconds

Here is the brief intro about what is LocalDateTime class in java.


  • LocalDateTime is an instance of date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.
  • LocalDateTime is an immutable date-time object that represents a date-time, often viewed as a year-month-day-hour-minute-second.
Let's see now how we can convert LocalDateTime to milliseconds
package com.javacodegeek.post5.localdatetimetomilliseconds;

import java.time.LocalDateTime;
import java.time.ZoneId;

public class LocalDateTimetoMilliseconds {
 public static void main(String[] args) {
  LocalDateTime now = LocalDateTime.now();
  long milli = fromLocalDateTimeToMillies(now);
  System.out.println("LocalDateTime : " + now + "  to milli : " + milli);

 private static long fromLocalDateTimeToMillies(LocalDateTime now) {
  long milli = now.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
  return milli;

Output : 

LocalDateTime : 2020-06-25T20:32:43.506  to milli : 1593097363506
As we can see above LocalDateTime is converted into milliseconds easily using the below code


This code first converts localdatetime instance to ZonedDateTime instance using 
now.atZone(ZoneId.systemDefault()) with system default timezone and then convert ZonedDateTime instance to Instant instance using toInstant() method and then finally Instant instance to milliseconds using toEpochMilli() method of Instant class.


In this quick tutorial, we have covered how we can convert LocalDateTime to milliseconds in java with System default timezone.

Happy Learning!!!

More Conversion Examples you may like