Java 时区问题

标签 java timezone

使用 Timezone.getTimeZone("Antarctica/Cas​​ey"),某些时区返回无效输出。例如,

SimpleDateFormat outputFormat = new SimpleDateFormat( "MM/dd/yyyy hh:mm aa zzz" );  
outputFormat.setTimeZone( TimeZone.getTimeZone( "Antarctica/Casey" ) );  
System.out.println(outputFormat.format( new Date() ));  

它应该返回“MM/dd/yyyy hh:mm aa CAST”,但它返回“MM/dd/yyyy hh:mm aa WST”。

像这样还有其他一些问题,

  1. 澳大利亚/阿德莱德 - ACST/ACDT
  2. 澳大利亚/尤克拉 - ACWST/ACWDT
  3. 澳大利亚/悉尼 - AEST/AEDT
  4. 南极洲/莫森 - AHMT
  5. 澳大利亚/珀斯 - AWST/AWDT
  6. 太平洋/阿皮亚 - SST。

最佳答案

如果你勾选the IANA Time Zone Database ,您会看到 Casey 当前处于 WST 时区,并且自 2012 年 2 月以来一直处于该时区。

# Zone  NAME        GMTOFF  RULES   FORMAT  [UNTIL]
Zone Antarctica/Casey   0   -   zzz 1969
            8:00    -   WST 2009 Oct 18 2:00
                        # Western (Aus) Standard Time
            11:00   -   CAST    2010 Mar 5 2:00
                        # Casey Time
            8:00    -   WST 2011 Oct 28 2:00
            11:00   -   CAST    2012 Feb 21 17:00u
            8:00    -   WST

这里是对更改的更详细引用:http://www.timeanddate.com/news/time/antarctica-2012.html .

但是 WST 仅在打印时使用:在内部正确处理从 GMT+8 到 GMT+11 的变化。参见示例:

public static void main(String[] args) {
  SimpleDateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm aa Z");
  TimeZone tz = TimeZone.getTimeZone("Antarctica/Casey");
  outputFormat.setTimeZone(tz);

  Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

  //May 2014 = GMT + 8
  System.out.println(outputFormat.format(c.getTime()));

  //january 2012 = GMT + 11
  c.set(Calendar.YEAR, 2012);
  c.set(Calendar.MONTH, 0);
  System.out.println(outputFormat.format(c.getTime()));
}

至于其他的区别,大概就是夏令时和冬令时的区别吧。

关于Java 时区问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23477891/

相关文章:

Java继承而不强制转换

php - 如何让 UNIX_TIMESTAMP 在不同时区时不偏移日期时间字段?

java - Backtrace 中缺少 gdb 符号?

java - 将字节缓冲区转换为字符串在 Java 中不起作用

python - 使用本地时区在 Python 中获取正确的时区偏移量

java - JPA+Hibernate日期显示错误数据

r - 如何更改数据框中的时区?

PostgreSQL - 以本地时间提取日期范围?

java - Java中的操作顺序是什么?

java - java 情况下的 HTTP 响应拆分