Convert LocalDateTime to Milliseconds in Java

  • Last updated Apr 25, 2024

In Java, we can convert a LocalDateTime value to milliseconds since the epoch (January 1, 1970, 00:00:00 GMT) by first converting it to a ZonedDateTime object and then calling the toInstant() method to get an Instant object, which represents the same point on the timeline in UTC.

The following example code shows how to convert LocalDateTime value to milliseconds in Java:

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Example {

   public static void main(String[] args) {
      // Creating LocalDateTime object
      LocalDateTime currrentLocalDateTime =;

      ZonedDateTime zonedDateTime = currrentLocalDateTime.atZone(ZoneOffset.systemDefault());
      long milliseconds = zonedDateTime.toInstant().toEpochMilli();

      System.out.println("LocalDateTime in Milliseconds : " + milliseconds);



The output of the above code is as follows:

LocalDateTime in Milliseconds : 1680500036423

In this example, we create a LocalDateTime object representing the current datetime using the now() method. Next, we convert the LocalDateTime object to a ZonedDateTime object using the atZone() method, passing in the system default time zone. Finally, we call the toInstant() method to get an Instant object, and then call the toEpochMilli() method to get the number of milliseconds since the epoch.