Jon*_*der 3 java timezone date
上周日,我们更改了中欧的时间 (-1h)。我正在做一些测试,但有些东西并没有让我用 java 时间解析器睡觉。这是代码
public static void main(String[] args) {
String dateFormatPattern = "yyyy-MM-dd HH:mm:ss";
String dateUtc = "2016-10-09 12:50:00";
SimpleDateFormat dateFormatUtc = new SimpleDateFormat(dateFormatPattern);
dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat dateFormatLisboa = new SimpleDateFormat(dateFormatPattern);
dateFormatLisboa.setTimeZone(TimeZone.getTimeZone("Europe/Lisboa"));
SimpleDateFormat dateFormatMadrid = new SimpleDateFormat(dateFormatPattern);
dateFormatMadrid.setTimeZone(TimeZone.getTimeZone("Europe/Madrid"));
SimpleDateFormat dateFormatParis = new SimpleDateFormat(dateFormatPattern);
dateFormatParis.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
System.out.println("UTC: "+dateUtc);
try {
Date d = dateFormatUtc.parse(dateUtc);
System.out.println("Lisboa: "+dateFormatLisboa.format(d));
System.out.println("Madrid: "+dateFormatMadrid.format(d));
System.out.println("Paris: "+dateFormatParis.format(d));
} catch (ParseException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出
UTC: 2016-10-09 12:50:00
Lisboa: 2016-10-09 12:50:00
Madrid: 2016-10-09 14:50:00
Paris: 2016-10-09 14:50:00
Run Code Online (Sandbox Code Playgroud)
为什么UTC和马德里时间相差2小时?现在马德里是UTC+1。
谢谢。
时间是正确的,因为时钟在 10 月 30 日凌晨 2 点更改
如果您将代码更改为此
String dateUtc = "2016-11-09 12:50:00";
Run Code Online (Sandbox Code Playgroud)
你得到这个输出,给出正确的 1 小时差异。
UTC: 2016-11-09 12:50:00
Lisboa: 2016-11-09 12:50:00
Madrid: 2016-11-09 13:50:00
Paris: 2016-11-09 13:50:00
Run Code Online (Sandbox Code Playgroud)
时区是由于日期对象实际引用的时间。所以那个时候是正确的
| 归档时间: |
|
| 查看次数: |
2837 次 |
| 最近记录: |