You can use java.time
to meet your requirement(s):
First option (preferred): OffsetDateTime
final long timestamp = 1568124300000L;
// create an Instant from the timestamp
Instant instant = Instant.ofEpochMilli(timestamp);
// create a datetime object from the Instant
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, ZoneId.of("UTC"));
// print it in your desired formatting
System.out.println("The day it belongs to is: "
+ offsetDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
Second option (valid, but less appropriate): ZonedDateTime
final long timestamp = 1568124300000L;
// create an Instant from the timestamp
Instant instant = Instant.ofEpochMilli(timestamp);
// create a datetime object from the Instant
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
// print it in your desired formatting
System.out.println("The day it belongs to is: "
+ zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
Please note that you can have different ZoneId
s. You have clearly written that it is UTC in your case, so I took that one for the example. There is ZoneId.systemDefault();
to get a local timezone, the output for that is the same (date) on my system.
For using this, you will have to be using Java 8 or the ThreeTen-Backport…