gel*_*mil 59 java datetimeoffset dst
我有一个Java类,它接收位置的纬度/经度,并在夏令时开启和关闭时返回GMT偏移.我正在寻找一种简单的方法来确定Java当前日期是否为夏令时,因此我可以应用正确的偏移量.目前我只对美国时区执行此计算,但最终我还希望将其扩展到全球时区.
Cli*_*int 69
这是问题所在机器的答案:
TimeZone.getDefault().inDaylightTime( new Date() );
Run Code Online (Sandbox Code Playgroud)
试图为客户端解决这个问题的服务器需要客户端的时区.请参阅@Powerlord的答案.
对于任何特定的TimeZone
TimeZone.getTimeZone( "US/Alaska").inDaylightTime( new Date() );
Run Code Online (Sandbox Code Playgroud)
Bas*_*que 38
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.
Run Code Online (Sandbox Code Playgroud)
这是现代java.time(参见教程)版本的正确答案 by mamboking.
ZoneId代表时区.该类知道判断DST是否适用于特定时区的规则.ZoneRules级车型全部为一个时区的历史和未来的过渡.Instant是UTC时间轴上的一个时刻.ZonedDateTime是施加的结果ZoneId到一个Instant.示例代码:
ZonedDateTime now = ZonedDateTime.now( ZoneId.of( "America/Montreal" ) );
…
ZoneId z = now.getZone();
ZoneRules zoneRules = z.getRules();
Boolean isDst = zoneRules.isDaylightSavings( now.toInstant() );
Run Code Online (Sandbox Code Playgroud)
注意在最后一行中我们如何通过简单的调用Instant从ZonedDateTime对象中提取对象toInstant.
该java.time框架是建立在Java 8和更高版本.这些类取代麻烦的老传统日期时间类,如java.util.Date,Calendar,和SimpleDateFormat.
现在处于维护模式的Joda-Time项目建议迁移到java.time类.
要了解更多信息,请参阅Oracle教程.并搜索Stack Overflow以获取许多示例和解释.规范是JSR 310.
您可以直接与数据库交换java.time对象.使用符合JDBC 4.2或更高版本的JDBC驱动程序.不需要字符串,不需要课程.java.sql.*
从哪里获取java.time类?
该ThreeTen-额外项目与其他类扩展java.time.该项目是未来可能添加到java.time的试验场.您可以在此比如找到一些有用的类Interval,YearWeek,YearQuarter,和更多.
mam*_*ing 10
TimeZone tz = TimeZone.getTimeZone("EST");
boolean inDs = tz.inDaylightTime(new Date());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
87085 次 |
| 最近记录: |