奇怪的问题与时区,日历和SimpleDateFormat [英] Strange problem with timezone, calendar and SimpleDateFormat
本文介绍了奇怪的问题与时区,日历和SimpleDateFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们考虑下面的代码:
SimpleDateFormat sdf = new SimpleDateFormat(HH:mm:ss dd / MM / yyyy ,Locale.US);
long start = sdf.parse(10:30:00 30/09/2009)。getTime();
long end = sdf.parse(10:30:00 30/10/2009)。getTime();
日历c = Calendar.getInstance(Locale.US);
c.setTimeInMillis(start);
System.out.println(Start =+ c.getTime());
c.setTimeInMillis(end);
System.out.println(End =+ c.getTime());
当运行此代码片段时,我有以下输出:
开始= 9月30日10:30:00 CEST 2009
结束= 10月30日10:30:00 CET 2009
为什么我得到不同的时区?
请注意,如果我设置第一个日期在8月和第二个在9月,输出将显示相同的时区在这两种情况下:
long start = sdf .parse(10:30:00 30/08/2009)。getTime();
long end = sdf.parse(10:30:00 30/09/2009)getTime();
将显示:
开始= Sun 8月30日10:30:00 CEST 2009
结束= 9月30日10:30:00 CEST 2009
我使用的是Java 1.6.0_14
解决方案夏时。它与CET相同,日光节约生效。
Let's consider the following code:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy", Locale.US); long start = sdf.parse("10:30:00 30/09/2009").getTime(); long end = sdf.parse("10:30:00 30/10/2009").getTime(); Calendar c = Calendar.getInstance(Locale.US); c.setTimeInMillis(start); System.out.println("Start = " + c.getTime()); c.setTimeInMillis(end); System.out.println(" End = " + c.getTime());
When running this code snippet, I have the following output:
Start = Wed Sep 30 10:30:00 CEST 2009 End = Fri Oct 30 10:30:00 CET 2009
Why do I get different timezone ?
Note that if I set the first date in august and the second one in september, the output will display the same timezone in both cases:
long start = sdf.parse("10:30:00 30/08/2009").getTime(); long end = sdf.parse("10:30:00 30/09/2009").getTime();
will display:
Start = Sun Aug 30 10:30:00 CEST 2009 End = Wed Sep 30 10:30:00 CEST 2009
I'm using Java 1.6.0_14
解决方案CEST is Central European Summer Time. It is the same as CET with daylight savings into effect.
这篇关于奇怪的问题与时区,日历和SimpleDateFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文