确定夏令时 (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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:57:25  来源:igfitidea点击:

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

javadstdatetimeoffset

提问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

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 Instantobject from our ZonedDateTimeobject with a simple call to toInstant.

请注意,在最后一行中,我们必须通过简单的调用InstantZonedDateTime对象中提取一个对象toInstant

Table of date-time types in Java, both modern and legacy.

Java 中的日期时间类型表,包括现代的和传统的。



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 类?

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 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多