确定 Java 中指定日期的夏令时 (DST) 是否有效
我有一个 Java 类,它接收某个位置的纬度/经度,并在夏令时开启和关闭时返回 GMT 偏移量。 我正在寻找一种简单的方法来确定 Java 中当前日期是否处于夏令时,以便我可以应用正确的偏移量。 目前我只针对美国时区执行此计算,尽管最终我也想将其扩展到全球时区。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是提出问题的机器的答案:
试图为客户端解决这个问题的服务器将需要客户端的时区。 原因请参见@Powerlord 的回答。
对于任何特定时区
This is the answer for the machine on which the question is being asked:
A server trying to figure this out for a client will need the client's time zone. See @Powerlord answer for the reason why.
For any particular TimeZone
tl;dr
java.time
这是现代的 java .time(请参阅教程)版本的正确答案,作者:mamboking 。
ZoneId
代表一个时区。 该类知道DST是否适用于特定时区。ZoneRules
类对时区的所有历史和未来转换进行建模。即时
是 UTC 时间线上的一个时刻。ZonedDateTime
是将ZoneId
应用于Instant
的结果。示例代码:
请注意,在最后一行中,我们如何通过简单调用 ZonedDateTime 对象中提取
Instant
对象。 oracle.com/javase/8/docs/api/java/time/chrono/ChronoZonedDateTime.html#toInstant--" rel="noreferrer">toInstant
。关于 java.time
java.time< /a> 框架内置于 Java 8 及更高版本中。 这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
,&SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
要了解更多信息,请参阅 Oracle 教程 。 并在 Stack Overflow 上搜索许多示例和解释。 规范为 JSR 310。
您可以直接与数据库交换java.time对象。 使用符合 JDBC 驱动程序 jeps/170" rel="noreferrer">JDBC 4.2 或更高版本。 不需要字符串,不需要 java.sql.* 类。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。 该项目是 java.time 未来可能添加的内容的试验场。 您可能会在这里找到一些有用的类,例如
Interval
,YearWeek
,YearQuarter
,以及更多。tl;dr
java.time
Here is the modern java.time (see Tutorial) version of the correct Answer by mamboking.
ZoneId
represents a time zone. The class knows the rules that tell if DST applies to a particular time zone.ZoneRules
class models all the historic and future transitions for a time-zone.Instant
is a moment on the timeline in UTC.ZonedDateTime
is the result of applying aZoneId
to anInstant
.Example code:
Note how in the last line we had to extract an
Instant
object from ourZonedDateTime
object with a simple call totoInstant
.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.您将不得不使用这些坐标做更多的工作并找出它们所在的时区。一旦您知道是哪个时区, isDayLight() 方法就会很有用。
例如,您无法判断 -0500 是否为 EST(美国/加拿大东部标准时间)、CDT(美国/加拿大中部夏令时间)、COT(哥伦比亚时间)、AST(巴西阿克里标准时间)、ECT(厄瓜多尔)时间)等...
其中一些可能支持也可能不支持夏令时。
You're going to have to do a bit more work using those coordinates and figure out which time zone they're in. Once you know which TimeZone that is, the isDayLight() method would be useful.
For example, you have no way of telling whether -0500 is EST (US/Canada Eastern Standard Time), CDT (US/Canada Central Daylight Time), COT (Colombia Time), AST (Brazil Acre Standard Time), ECT (Ecuador Time), etc...
Some of these may or may not support daylight saving time.
Joda Time 包含将为您计算偏移量的处理方法。 请参阅DateTimeZone.convertLocalToUTC(...)
作为补充,您需要使用纬度/经度信息查找当前时区。 GeoNames 为其 Web 服务提供了一个 Java 客户端,以及一个简单的 Web -请求框架(即http://ws.geonames.org/timezone?纬度=47.01&lng=10.2)
Joda Time contains handling methods which will calculate the offsets for you. See DateTimeZone.convertLocalToUTC(...)
To supplement this, you will need to look up the current time zone with your latitude/longitude info. GeoNames provides a java client for its web service, as well as a simple web-request framework (i.e. http://ws.geonames.org/timezone?lat=47.01&lng=10.2)
ZonedDateTime
类。getOffset()
method of theTimeZone
class will get the offset, without having to find out whether the time being processed is in daylight saving time or not - it does all that work for you. You may even be able to just use theZonedDateTime
class.