确定夏令时 (DST) 在 Java 中是否在指定日期处于活动状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1060479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine Whether Daylight Savings Time (DST) is Active in Java for a Specified Date
提问by gelgamil
I have a Java class that takes in the latitude/longitude of a location and returns the GMT offset when daylight savings time is on and off. I am looking for an easy way to determine in Java if the current date is in daylight savings time so I can apply the correct offset. Currently I am only performing this calculation for U.S. timezones although eventually I would like to expand this to global timezones as well.
我有一个 Java 类,它接受一个位置的纬度/经度,并在夏令时打开和关闭时返回 GMT 偏移量。我正在寻找一种在 Java 中确定当前日期是否处于夏令时的简单方法,以便我可以应用正确的偏移量。目前我只对美国时区执行此计算,但最终我也想将其扩展到全球时区。
回答by Clint
This is the answer for the machine on which the question is being asked:
这是被问到问题的机器的答案:
TimeZone.getDefault().inDaylightTime( new Date() );
A server trying to figure this out for a client will need the client's time zone. See @Powerlord answer for the reason why.
试图为客户端解决这个问题的服务器将需要客户端的时区。请参阅@Powerlord 答案以了解原因。
For any particular TimeZone
对于任何特定时区
TimeZone.getTimeZone( "US/Alaska").inDaylightTime( new Date() );
回答by mamboking
TimeZone tz = TimeZone.getTimeZone("EST");
boolean inDs = tz.inDaylightTime(new Date());
回答by Powerlord
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.
您将不得不使用这些坐标做更多的工作,并找出它们所在的时区。一旦知道是哪个时区,isDayLight() 方法就会很有用。
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...
例如,您无法判断 -0500 是否为 EST(美国/加拿大东部标准时间)、CDT(美国/加拿大中部夏令时间)、COT(哥伦比亚时间)、AST(巴西英亩标准时间)、ECT(厄瓜多尔)时间)等...
Some of these may or may not support daylight saving time.
其中一些可能支持也可能不支持夏令时。
回答by James Van Huis
Joda Timecontains handling methods which will calculate the offsets for you. See DateTimeZone.convertLocalToUTC(...)
Joda Time包含可以为您计算偏移量的处理方法。请参阅DateTimeZone.convertLocalToUTC(...)
To supplement this, you will need to look up the current time zone with your latitude/longitude info. GeoNamesprovides 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)
为了补充这一点,您需要使用纬度/经度信息查找当前时区。 GeoNames为其 web 服务提供了一个 java 客户端,以及一个简单的 web 请求框架(即http://ws.geonames.org/timezone?lat=47.01&lng=10.2)
回答by Basil Bourque
tl;dr
tl;博士
ZoneId.of( "America/Montreal" ) // Represent a specific time zone, the history of past, present, and future changes to the offset-from-UTC used by the people of a certain region.
.getRules() // Obtain the list of those changes in offset.
.isDaylightSavings( // See if the people of this region are observing Daylight Saving Time at a specific moment.
Instant.now() // Specify the moment. Here we capture the current moment at runtime.
) // Returns a boolean.
java.time
时间
Here is the modern java.time(see Tutorial) version of the correct Answerby mamboking.
这里是现代java.time(参见教程)的版本正确答案的mamboking。
- A
ZoneId
represents a time zone. The class knows the rules that tell if DSTapplies to a particular time zone. - The
ZoneRules
class models all the historic and future transitions for a time-zone. - An
Instant
is a moment on the timeline in UTC. - A
ZonedDateTime
is the result of applying aZoneId
to anInstant
.
- A
ZoneId
表示时区。该类知道判断DST 是否适用于特定时区的规则。 - 该
ZoneRules
级车型全部为一个时区的历史和未来的过渡。 - An
Instant
是UTC时间线上的一个时刻。 - A
ZonedDateTime
是将 aZoneId
应用于 an的结果Instant
。
Example code:
示例代码:
ZonedDateTime now = ZonedDateTime.now( ZoneId.of( "America/Montreal" ) );
…
ZoneId z = now.getZone();
ZoneRules zoneRules = z.getRules();
Boolean isDst = zoneRules.isDaylightSavings( now.toInstant() );
Note how in the last line we had to extract an Instant
object from our ZonedDateTime
object with a simple call to toInstant
.
请注意,在最后一行中,我们必须通过简单的调用Instant
从ZonedDateTime
对象中提取一个对象toInstant
。
About java.time
关于java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date
, Calendar
, & SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*
classes.
您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Most of the java.timefunctionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.timeclasses.
- For earlier Android (<26), the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9、Java SE 10、Java SE 11及更高版本 - 标准 Java API 的一部分,具有捆绑实现。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 大部分java.time功能在ThreeTen-Backport中向后移植到 Java 6 & 7 。
- 安卓
- java.time类的更高版本的 Android 捆绑实现。
- 对于早期的 Android(<26),ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject 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.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。