问题描述
我有一个Java类,它接收位置的纬度/经度,并在夏令时开启和关闭时返回GMT偏移。我正在寻找一种简单的方法来确定Java中当前日期是否为夏令时,因此我可以应用正确的偏移量。目前我只对美国时区执行此计算,但最终我还想将其扩展到全球时区。
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.
推荐答案
这是询问问题的机器的答案:
This is the answer for the machine on which the question is being asked:
TimeZone.getDefault().inDaylightTime( new Date() );
试图为客户解决这个问题的服务器需要客户端的时区。请参阅@Powerlord答案。
A server trying to figure this out for a client will need the client's time zone. See @Powerlord answer for the reason why.
对于任何特定的TimeZone
For any particular TimeZone
TimeZone.getTimeZone( "US/Alaska").inDaylightTime( new Date() );
这篇关于确定指定日期的Java夏令时(DST)是否处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!