【发布时间】:2016-11-02 14:23:15
【问题描述】:
上周日我们更改了中欧的时间 (-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();
}
}
这是输出
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
为什么 UTC 和马德里时间相差 2 小时?现在在马德里是 UTC+1。
谢谢。
【问题讨论】:
-
时钟是哪一天改的?如果你这样做
String dateUtc = "2016-11-02 12:50:00";会发生什么? -
谢谢,你是对的。我对时间变化视而不见,我没有意识到字符串日期更早:(
-
别担心,它的经典,需要另一双眼睛,东西。叫别人看东西我就解决了这么多。
-
仅供参考,那些日期时间类现在是遗留的,被 java.time 类所取代。迁移到 java.time 将使您的生活更轻松。