根据偏移量和 DST 偏移量确定时区
我想知道是否有一种方法可以构建一个 java TimeZone 对象,并将 2 个 gmt-hour-offsets 作为整数。例如,如果给我 (-5, -6)
,那么我希望能够将其解释为 EST 时间。此外,一旦我知道它对应的位置,我希望能够查明该时区的给定日期是否处于夏令时。所以理想情况
public static TimeZone getTimeZone(int offsetHrs, int dstOffsetHrs);
...
TimeZone tz = getTimeZone(-5, -6);
if(tz.isDST(currentDate)) {
//Do stuff...
}
下,如果 offsetHours 和 dstOffsetHrs 相同,那么我们忽略 dst ....不要问为什么,这是我需要确认可以完成的要求,否则我需要查看一些其他地方发生重大变化。
提前致谢
I was wondering if there was a way to build a java TimeZone object given 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
否 - 可以有多个时区具有相同的偏移量,但在不同的日期转换。当然,如果您愿意接受这种潜在的歧义,那么这应该不是不可能的......
例如,使用 Joda
DateTimeZone
您可能希望从某个固定日期开始(例如 2010 年 1 月 1 日),然后查看接下来的几个转换每个时区的信息 - 如果时区有转换 - 计算出其标准/夏令时偏移量是多少。然后,您可以使用isStandardOffset
来确定特定时刻是处于 DST 还是标准时间。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 useisStandardOffset
to determine whether a particular instant is in DST or standard time.