fork download
  1. import java.time.Instant;
  2. import java.time.LocalDate;
  3. import java.time.LocalDateTime;
  4. import java.time.OffsetDateTime;
  5. import java.time.ZoneId;
  6. import java.time.ZonedDateTime;
  7. import java.util.Date;
  8.  
  9. class Main {
  10. public static void main(String[] args) {
  11. long unixTime = 797000000L;
  12. Instant instant = Instant.ofEpochSecond(unixTime);
  13. System.out.println(instant);
  14.  
  15. // You can derive other date-time objects from the Instant object
  16. // ZoneId.systemDefault() returns the system default time-zone.
  17. // Replace it with the desired time-zone e.g. ZoneId.of("Asia/Kolkata")
  18. ZoneId zoneId = ZoneId.systemDefault();
  19. ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);
  20. OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, zoneId);
  21. LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
  22. LocalDate localDate = LocalDate.ofInstant(instant, zoneId);
  23. // etc.
  24. System.out.println(zonedDateTime);
  25. System.out.println(offsetDateTime);
  26. System.out.println(localDateTime);
  27. System.out.println(localDate);
  28.  
  29. // For some reason, if you need an instance of `java.util.Date`
  30. Date date = Date.from(instant);
  31. System.out.println(date);
  32. }
  33. }
Success #stdin #stdout 0.15s 59824KB
stdin
Standard input is empty
stdout
1995-04-04T12:53:20Z
1995-04-04T12:53:20Z[GMT]
1995-04-04T12:53:20Z
1995-04-04T12:53:20
1995-04-04
Tue Apr 04 12:53:20 GMT 1995