时区、日历和 SimpleDateFormat 的奇怪问题

2022-01-11 00:00:00 date calendar simpledateformat java

让我们考虑以下代码:

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.

相关文章