Does any body know how to … get values in UTC+0?
I’m taking the easy part first:
OffsetDateTime dateTime = Instant.ofEpochMilli(1_525_694_035_615L)
.atOffset(ZoneOffset.UTC);
System.out.println(dateTime);
This prints:
2018-05-07T11:53:55.615Z
This agrees with what you said in the question. The Z
in the end means UTC.
…how to identify time zone…
We can’t exactly. If you know the time to which the timestamp corresponds in the timezone where it was produced, we can calculate the offset. Since this is rather boring when the answer is UTC, I am taking a different example:
long timestamp = 1_525_708_128_067L;
LocalDateTime dateTimeInUnknownTimezone = LocalDateTime.of(2018, Month.MAY, 7, 18, 48, 48);
LocalDateTime utcDateTime = Instant.ofEpochMilli(timestamp)
.atOffset(ZoneOffset.UTC)
.toLocalDateTime()
.truncatedTo(ChronoUnit.SECONDS);
int offsetInSeconds = (int) ChronoUnit.SECONDS.between(utcDateTime, dateTimeInUnknownTimezone);
ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetInSeconds);
System.out.println(offset);
+03:00
Since the date-time has precision of seconds only, I am also truncating the UTC time to seconds to get the offset precise. From the offset of +03:00 we cannot unambiguously determine a time zone, though, since many time zones use this offset.