-->
Skip to main content

Convert LocalDateTime from Milliseconds in Java

Convert LocalDateTime from Milliseconds in Java 

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

Convert LocalDateTime from Milliseconds

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

LocalDateTime

  • 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 from milliseconds

package com.javacodegeek.post4.localdatetimefrommilliseconds;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ConvertLocalDateTimeFromMilliSeconds {
  public static void main(String[] args) {
   long current_timestamp = ZonedDateTime.now().toInstant().toEpochMilli();
   //This will print localdatetime in indian timezone format using default zone.
   LocalDateTime time1 = fromMilliesToLocalDateTimeWithDefaultTimeZone(current_timestamp);
   System.out.println("Indian Timezone timing : " + time1);

   //This will print localdatetime in Us timezone format using US/Eastern zone.
   String timezone = "US/Eastern";
   LocalDateTime time2 = fromMillsToLocalDateTimeWithSpecificTimezone(current_timestamp, timezone);
   System.out.println("US Timezone timing : " + time2);
 }

 private static LocalDateTime fromMilliesToLocalDateTimeWithDefaultTimeZone(long millis) {
  Instant instant = Instant.ofEpochMilli(millis);
  LocalDateTime date = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
  return date;
 }

 private static LocalDateTime fromMillsToLocalDateTimeWithSpecificTimezone(long millis, String timezone) {
  Instant instant = Instant.ofEpochMilli(millis);
  LocalDateTime date = instant.atZone(ZoneId.of(timezone)).toLocalDateTime();
  return date;
 }
}

Output: 

Indian Timezone timing : 2020-06-25T19:23:20.789
US Timezone timing : 2020-06-25T09:53:20.789
As we can see above output for two different timezones one with default timezone and one with user-specific timezone which in this case is US/Eastern.

 Instant instant = Instant.ofEpochMilli(millis);

This line is converting milliseconds to Instant Class Instance which is used further to convert the Instant class instance to LocalDateTime instance.

 LocalDateTime date = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();

This above code is used to convert the Instant class instance to localdatetime instance using default timezone specified by ZoneId.systemDefault() method.

LocalDateTime date = instant.atZone(ZoneId.of(timezone)).toLocalDateTime();

This code is now using timezone defined by the user thus convert localdatetime from milliseconds in user-defined timezone format.

Conclusion

In this quick tutorial, we have covered how we can convert LocalDateTime from milliseconds in java with System default timezone and user-defined timezone.

Happy Learning!!!

More Conversion Examples you may like

Comments