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.
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);
}
Warnings about truncating to hours are well taken, but for the purposes of the application this is for we don't need to go that far with it. However, it'd be easy enough I imagine to truncate using ChronoUnit.MINUTES
or even seconds. An interesting exercise that Ole V.V. helpfully pointed out.
Not sure why this was being downvoted -- maybe the confusion around the meaning of a timestamp. I think it was pretty clear from the question that I was referring to a generic timestamp (as Ole V.V. and others noted). I hope the downvoters reread the question and adjust their assessment.
While I don't really consider it relevant to the question, 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.