69. Converting a Unix timestamp to date-time

For this solution, let's suppose the following Unix timestamp—1573768800. This timestamp is equivalent to the following:

  • 11/14/2019 @ 10:00pm (UTC)
  • 2019-11-14T22:00:00+00:00 in ISO-8601
  • Thu, 14 Nov 2019 22:00:00 +0000 in RFC 822, 1036, 1123, 2822
  • Thursday, 14-Nov-19 22:00:00 UTC in RFC 2822
  • 2019-11-14T22:00:00+00:00 in RFC 3339

In order to convert a Unix timestamp to a date-time, it is important to know that the Unix timestamps resolution is in seconds, while java.util.Date needs milliseconds. So, the solution to obtain a Date object from a Unix timestamp requires a conversion from seconds to milliseconds by multiplying the Unix timestamp by 1,000 as shown in the following two examples:

long unixTimestamp = 1573768800;

// Fri Nov 15 00:00:00 EET 2019 - in the default time zone
Date date = new Date(unixTimestamp * 1000L);

// Fri Nov 15 00:00:00 EET 2019 - in the default time zone
Date date = new Date(TimeUnit.MILLISECONDS
.convert(unixTimestamp, TimeUnit.SECONDS));

Starting with JDK 8, the Date class uses the from(Instant instant) method. Moreover, the Instant class comes with the ofEpochSecond(long epochSecond) method, which returns an instance of Instant, using the given seconds from the epoch, of 1970-01-01T00:00:00Z:

// 2019-11-14T22:00:00Z in UTC
Instant instant = Instant.ofEpochSecond(unixTimestamp);

// Fri Nov 15 00:00:00 EET 2019 - in the default time zone
Date date = Date.from(instant);

The instant that was obtained in the previous example can be used to create LocalDateTime or ZonedDateTime, as follows:

// 2019-11-15T06:00
LocalDateTime date = LocalDateTime
.ofInstant(instant, ZoneId.of("Australia/Perth"));

// 2019-Nov-15 00:00:00 +0200 Europe/Bucharest
ZonedDateTime date = ZonedDateTime
.ofInstant(instant, ZoneId.of("Europe/Bucharest"));
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset