Showing posts with label epoch. Show all posts
Showing posts with label epoch. Show all posts

Monday, 13 April 2020

Get LocalDateTime from long epoch time

Following snippet is used to convert epoch to LocalDateTime.

LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(epoch), ZoneId.systemDefault());

App.java
package com.sample.app;

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

public class App {

 public static LocalDateTime getLocalDate(long epoch) {
  LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(epoch), ZoneId.systemDefault());
  return localDateTime;
 }

 public static void main(String args[]) throws ClassNotFoundException {
  LocalDateTime localDateTime = getLocalDate(1234567890l);

  System.out.println(localDateTime);
 }
}

Output
1970-01-15T12:26:07.890

You may like
Previous                                                    Next                                                    Home

Get LocalDate from long epoch time

'ZonedDateTime' class provides 'toLocalDate' method to convert ZonedDateTime to LocalDate.

Following snippet is used to get LocalDate from epoch.
LocalDate localDate = Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()).toLocalDate();

App.java
package com.sample.app;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;

public class App {

 public static LocalDate getLocalDate(long epoch) {
  LocalDate localDate = Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()).toLocalDate();
  return localDate;
 }

 public static void main(String args[]) throws ClassNotFoundException {
  LocalDate localDate = getLocalDate(1234567890l);
  
  System.out.println(localDate);
 }
}

Output
1970-01-15

You may like
Previous                                                    Next                                                    Home