Using a system that stores timezone information for historical object as follows:
o1: dt= 2022-07-15 13:32:00 offset=5 dst=false
o2: dt= 2022-01-15 13:32:00 offset=5 dst=true
o3: dt= 2022-07-15 13:32:00 offset=7 dst=false
Life would be much easier if it stored IANA timezones like "America/New York" but this is what I have to deal with. My end goal is to compare these historical dates to the current time and get a time difference between them.
Creating current time is easy. I can get it in UTC
ZonedDateTime.now(ZoneId.of("UTC"))
2022-07-15T15:33:04.177Z[UTC]
Or my local timezone
ZonedDateTime.now()
2022-07-15T11:33:28.648-04:00[America/New_York]
The question is, how can I read the table data into a ZonedDateTime
for an apples to apples comparison. For example some magical method like
ZonedDateTime.fromOffset(5).fromDst(false).asTz('UTC')
You can use ZoneOffset
with the specified offset, the DST true/false doesn't seem relevant here. Assuming the timestamps are shown are local (and not UTC), you can do something like:
public static void main(String[] args) {
record TimestampAndOffset(String timestamp, int offsetHours) { }
var timeData = List.of(
new TimestampAndOffset("2022-07-15T13:32:00", 5),
new TimestampAndOffset("2022-01-15T13:32:00", 5),
new TimestampAndOffset("2022-07-15T13:32:00", 7));
timeData.forEach(to -> {
LocalDateTime localTimestamp = LocalDateTime.parse(to.timestamp());
var zoneOffset = ZoneOffset.ofHours(to.offsetHours());
var zonedValue = ZonedDateTime.of(localTimestamp, zoneOffset);
System.out.println(zonedValue);
System.out.println(zonedValue.withZoneSameInstant(ZoneOffset.UTC));
});
}
To simplify parsing for this example, I use ISO-8601 format with T
as the separator.
Output:
2022-07-15T13:32+05:00
2022-07-15T08:32Z
2022-01-15T13:32+05:00
2022-01-15T08:32Z
2022-07-15T13:32+07:00
2022-07-15T06:32Z
Important caveat as pointed out by user16320675 in the comments, double check the meaning of your offsets, as based on the original version of your question, your data might be using an inverse offset (e.g. 5 instead of -5), in which case you need to use ZoneOffset.ofHours(-to.offsetHours())
instead.
As mentioned in the comments by Basil Bourque, consider if using OffsetDateTime
makes more sense for your use case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With