0

I have two timestamps as milliseconds, which were created in different time zones and cannot convert to the same time zone.

Date_1 = 1525694035615 - Mon May 07 2018 11:53:55

Date_2 = 1525686835730 - Mon May 07 2018 09:53:55

Does any body know how to identify time zone and get values in UTC+0?

When I transform these timestamps always get values that these timestamps have timezone +0.

Anton Konikov
  • 49
  • 1
  • 1
  • 7
  • 5
    Timestamps are time-zone independent. – Compass May 07 '18 at 13:28
  • 1
    Use TimeZone (java.util.TimeZone), see if it heps – Nephilim May 07 '18 at 13:32
  • 1
    @Nephilim Don’t use the outdated `TimeZone` class. Use [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 07 '18 at 14:54
  • 1
    The values you give are already in UTC+0. For example 1525694035615 is the same point in time as Mon May 07 2018 11:53:55 UTC. In another time zone the time would probably (but not for sure) be something else. – Ole V.V. May 07 '18 at 14:59
  • @Compass More precisely, such timestamps are in UTC (likely). – Basil Bourque May 07 '18 at 20:04

3 Answers3

3

If your milliseconds timestamps created in Java they are always milliseconds from

midnight, January 1, 1970 UTC

And have nothing about time zone.

what you see:

Mon May 07 2018 11:53:55

Mon May 07 2018 09:53:55

Just string representations of those moments in time in the current JVM/Computer Time Zone

If you need to have a String representation in UTC time zone there are number of classes to do that. In all JDK there are Calendar implementations (e.g. GregorianCalendar). From Java 8 there is new API inspired by Joda time.

It should work better with timezones handling.

Community
  • 1
  • 1
Vadim
  • 4,027
  • 2
  • 10
  • 26
2

If you are getting timezone+0 that is because that is the default timezone given any fixed time in any amount (milliseconds in your example). You must tell it what timezone you are in if you want a different one that UTC+0.

Clint L
  • 1,093
  • 5
  • 12
  • 29
  • I'm looking for more independence solution, because one of this timestamp is getting from one backend and can be changed according to landscape. Which time-zone for other backends it will be harder to identify. – Anton Konikov May 07 '18 at 13:35
  • @AntonKonikov well you can't create information from nowhere. A number of milliseconds contain no information about the time zone that was used by whoever generated it, so it's not possible to get an accurate time zone out of it. If you need to know the time zones of your multiple sources, you need them to tell what is their time zone, in addition to numbers of milliseconds. – kumesana May 07 '18 at 13:51
  • @AntonKonikov your independence is reliant on the "other backends" you mention. They must tell you what timezone they are in. You can't figure it out just by getting their time with milliseconds. You will either need a mapping for each system and what their corresponding timezone is, or have it retrieved. – Clint L Jun 12 '18 at 17:18
1

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161