javadatetimetimestamputcdatetimeoffset

Is it possible to determine the UTC offset from two timestamps?


Let's say I have two timestamps. One is in UTC, and the other's timezone is unknown, but has a date and a time. The format of the latter is YYYY:MM:DD HH:MM:SS.

Is it reasonable to get the offset from UTC by simply subtracting the second timestamp from the UTC one, then rounding the hours?

Is there a straightforward way to do this using Java? (e.g. Joda or JUT?)

Thanks!

A little background: These dates come from EXIF metadata of images. EXIF stores the CreateDate of an image in the format: YYYY:MM:DD HH:MM:SS with no timezone information. It also stores a UTC timestamp (sometimes) under GPSTimeStamp. Using these two values, I was hoping to derive the offset in order to store the create date with it. Although there is EXIF metadata for TZ offsets, cameras rarely record that.


Solution

  • This is what I ended up doing:

    public static ZoneOffset determineTimeZoneOffset(OffsetDateTime localTime, OffsetDateTime utcTime) {
      if(utcTime == null || localTime == null) {
        return null;
      }
    
      OffsetDateTime ceilingUtc = utcTime.truncatedTo(ChronoUnit.HOURS).plusHours(1);
      OffsetDateTime ceilingLocal = localTime.truncatedTo(ChronoUnit.HOURS).plusHours(1);
    
      int offsetHours = (int)ChronoUnit.HOURS.between(ceilingUtc, ceilingLocal);
    
      return ZoneOffset.ofHours(offsetHours);
    }