时区、日历和 SimpleDateFormat 的奇怪问题 [英] Strange problem with timezone, calendar and SimpleDateFormat

查看:28
本文介绍了时区、日历和 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();

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 ?

请注意,如果我将第一个日期设置为 8 月,第二个日期设置为 9 月,则两种情况下的输出将显示相同的时区:

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();

将显示:

Start = Sun Aug 30 10:30:00 CEST 2009
  End = Wed Sep 30 10:30:00 CEST 2009

我使用的是 Java 1.6.0_14

I'm using Java 1.6.0_14

推荐答案

CEST 是中欧夏令时.与夏令时生效的 CET 相同.

CEST is Central European Summer Time. It is the same as CET with daylight savings into effect.

这篇关于时区、日历和 SimpleDateFormat 的奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆