Ada*_*tan 10 java timezone dst
如何在Java中获取特定日期和时区的GMT时差?
确定特定时区是否在DST中非常简单:
boolean isIsraelInDST = TimeZone.getTimeZone("Israel").inDaylightTime(new Date());
Run Code Online (Sandbox Code Playgroud)
我如何得到实际的时差?
Evg*_*eev 22
使用TimeZone.getRawOffset():返回添加到UTC以获取此时区的标准时间的时间量(以毫秒为单位).由于此值不受夏令时影响,因此称为原始偏移.
如果您想要包含DST的偏移量,则使用TimeZone.getOffset(长日期).您应该为方法提供具体日期,例如now - System.currentTimeMillis()
And*_*kov 11
我建议在getRawOffset中添加夏季/冬季时间偏移量:
TimeZone tz1 = TimeZone.getTimeZone("GMT");
TimeZone tz2 = TimeZone.getTimeZone("America/New_York");
long timeDifference = tz1.getRawOffset() - tz2.getRawOffset() + tz1.getDSTSavings() - tz2.getDSTSavings();
Run Code Online (Sandbox Code Playgroud)
ZoneId.of( "Pacific/Auckland" ) // Specify a time zone.
.getRules() // Get the object representing the rules for all the past, present, and future changes in offset used by the people in the region of that zone.
.getOffset( Instant.now() ) // Get a `ZoneOffset` object representing the number of hours, minutes, and seconds displaced from UTC. Here we ask for the offset in effect right now.
.toString() // Generate a String in standard ISO 8601 format.
Run Code Online (Sandbox Code Playgroud)
+13:00
在特定日期的第一时刻.
ZoneId.of( "Pacific/Auckland" )
.getRules()
.getOffset(
LocalDate.of( 2018 , Month.AUGUST , 23 ) // Specify a certain date. Has no concept of time zone or offset.
.atStartOfDay( ZoneId.of( "Pacific/Auckland" ) ) // Determine the first moment of the day on that date in that region. Not always `00:00:00` because of anomalies such as Daylight Saving Time.
.toInstant() // Adjust to UTC by extracting an `Instant`.
)
.toString()
Run Code Online (Sandbox Code Playgroud)
+12:00
其他答案已经过时了,因为该TimeZone课程现已成为遗产.这个和其他麻烦的旧日期时间类被java.time时间类取代.
现在我们使用ZoneId,ZoneOffset而ZoneRules不是遗留TimeZone类.
指定适当的时区名称,格式continent/region,如America/Montreal,Africa/Casablanca或Pacific/Auckland.切勿使用3-4字母缩写,例如EST或IST因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!).
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
Run Code Online (Sandbox Code Playgroud)
获取该区域的规则.
ZoneRules rules = z.getRules() ;
Run Code Online (Sandbox Code Playgroud)
如果夏令时(DST)在某个时刻生效,请询问规则.将时刻指定为Instant.该Instant级表示时间轴上的时刻UTC,分辨率为纳秒(最多9个(9)小数的位数).
Instant instant = Instant.now() ; // Capture current moment in UTC.
boolean isDst = rules.isDaylightSavings( instant ) ;
Run Code Online (Sandbox Code Playgroud)
我如何得到实际的时差?
不确定你是什么意思,但我猜你要求在那个时刻比从区域开始生效的UTC偏移量.偏移量是UTC的小时,分钟和秒位移数.我们使用ZoneOffset类表示偏移量.时区是特定地区人民使用的偏移的过去,现在和将来变化的历史.我们使用ZoneId类表示时区.
由于某个地区的偏移量可能会随时间而变化,因此我们必须在要求抵消时通过.
ZoneOffset offset = rules.getOffset( instant ) ;
Run Code Online (Sandbox Code Playgroud)
生成表示ISO 8601标准格式的偏移量的字符串.
String output output = offset.toString() ;
Run Code Online (Sandbox Code Playgroud)
您可以要求偏移量作为总秒数.
int offsetInSeconds = offset.getTotalSeconds() ;
Run Code Online (Sandbox Code Playgroud)
该java.time框架是建立在Java 8和更高版本.这些类取代麻烦的老传统日期时间类,如java.util.Date,Calendar,和SimpleDateFormat.
现在处于维护模式的Joda-Time项目建议迁移到java.time类.
要了解更多信息,请参阅Oracle教程.并搜索Stack Overflow以获取许多示例和解释.规范是JSR 310.
使用符合JDBC 4.2或更高版本的JDBC驱动程序,您可以直接与数据库交换java.time对象.不需要字符串也不需要java.sql.*类.
从哪里获取java.time类?
该ThreeTen-额外项目与其他类扩展java.time.该项目是未来可能添加到java.time的试验场.您可以在此比如找到一些有用的类Interval,YearWeek,YearQuarter,和更多.
小智 8
我看到这是一个旧线程 - 但是由于我最近有类似的要求而添加了这个 - 这给出了基于CURRENT时间(来自epoch的毫秒)的毫秒的实际差异.
TimeZone.getTimeZone("America/New_York").getOffset(Calendar.getInstance().getTimeInMillis())
Run Code Online (Sandbox Code Playgroud)