开发者

Determine Timezone based on offset and DST offset

开发者 https://www.devze.com 2023-02-24 04:14 出处:网络
I was wondering if there was a way to build a java TimeZone object g开发者_开发百科iven the 2 gmt-hour-offsets as integers. For example, if I was given (-5, -6), then I would like to be able to interp

I was wondering if there was a way to build a java TimeZone object g开发者_开发百科iven the 2 gmt-hour-offsets as integers. For example, if I was given (-5, -6), then I would like to be able to interpret that as EST time. Additionally, once I know what location it corresponds to, I want to be able to find out if a given date in that time zone is in DST or not. So ideally,

public static TimeZone getTimeZone(int offsetHrs, int dstOffsetHrs);
...
TimeZone tz = getTimeZone(-5, -6);
if(tz.isDST(currentDate)) {
    //Do stuff...
}

And then if offsetHours and dstOffsetHrs are the same, then we ignore dst.... Don't ask why, it's a requirement that I either need to confirm can be done, or else I'll need to look at some major changes elsewhere.

Thanks in advance


No - there can be multiple time zones with the same offsets, but which transition on different dates. Of course if you're happy to accept that potential ambiguity, it shouldn't be impossible...

For example, using a Joda DateTimeZone you'd probably want to start off with some fixed date (e.g. January 1st 2010) and look through the next few transitions of each time zone - if the time zone has transitions - to work out what its standard/daylight offsets are. You can then use isStandardOffset to determine whether a particular instant is in DST or standard time.

0

精彩评论

暂无评论...
验证码 换一张
取 消