简体   繁体   English

确定指定日期的Java夏令时(DST)是否处于活动状态

[英]Determine Whether Daylight Savings Time (DST) is Active in Java for a Specified Date

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. 我有一个Java类,它接收位置的纬度/经度,并在夏令时开启和关闭时返回GMT偏移。 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. 我正在寻找一种简单的方法来确定Java中当前日期是否为夏令时,因此我可以应用正确的偏移量。 Currently I am only performing this calculation for US timezones although eventually I would like to expand this to global timezones as well. 目前我只对美国时区执行此计算,但最终我还希望将其扩展到全球时区。

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

TimeZone.getTimeZone( "US/Alaska").inDaylightTime( new Date() );

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 java.time

Here is the modern java.time (see Tutorial ) version of the correct Answer by mamboking . 这是现代java.time (参见教程 )版本的正确答案 mamboking

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 . 注意在最后一行中我们如何通过简单调用toInstantZonedDateTime对象中提取Instant对象。


About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧遗留日期时间类,如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 . 规范是JSR 310

You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用符合JDBC 4.2或更高版本的JDBC驱动程序 No need for strings, no need for java.sql.* classes. 不需要字符串,不需要java.sql.*类。

Where to obtain the java.time classes? 从哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

TimeZone tz = TimeZone.getTimeZone("EST");
boolean inDs = tz.inDaylightTime(new Date());

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. 你将不得不使用这些坐标做更多的工作,并找出它们所在的时区。一旦你知道了哪个TimeZone,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. 其中一些可能支持也可能不支持夏令时。

Joda Time contains handling methods which will calculate the offsets for you. Joda Time包含处理方法,可以为您计算偏移量。 See DateTimeZone.convertLocalToUTC(...) 请参见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 (ie 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM