我有以下测试:

import static org.junit.Assert.assertEquals;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

import org.junit.Test;
public class CalendarBug {
    private static final TimeZone UTC_ZONE = TimeZone.getTimeZone("UTC");// +0 hours
    private static final TimeZone IST_ZONE = TimeZone.getTimeZone("IST");// +5 hours 30 minutes

    @Test
    public void calendarBug() {
        Calendar utcCalendar = Calendar.getInstance(UTC_ZONE);
        utcCalendar.set(Calendar.YEAR, 2015);
        utcCalendar.set(Calendar.MONTH, 3);
        utcCalendar.set(Calendar.DAY_OF_MONTH, 12);
        utcCalendar.set(Calendar.HOUR_OF_DAY, 10);
        utcCalendar.set(Calendar.MINUTE, 0);
        utcCalendar.set(Calendar.SECOND, 0);
        SimpleDateFormat utcFormatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss Z" );
        utcFormatter.setTimeZone(UTC_ZONE);
        System.out.println( "If I have this line commented out, the test fails: " + utcFormatter.format(utcCalendar.getTime()));
        Calendar istCalendar = (Calendar) utcCalendar.clone();
        assertEquals(UTC_ZONE, istCalendar.getTimeZone());
        istCalendar.setTimeZone(IST_ZONE);
        assertEquals(istCalendar.getTimeInMillis(), utcCalendar.getTimeInMillis());
    }
}

如果您运行测试,它将起作用。但是,如果您用里面的格式化程序注释掉了System.out.println行,它将失败:
java.lang.AssertionError: expected:<1428813000979> but was:<1428832800979>
    at org.junit.Assert.fail(Assert.java:93)
    at org.junit.Assert.failNotEquals(Assert.java:647)
    at org.junit.Assert.assertEquals(Assert.java:128)
    at org.junit.Assert.assertEquals(Assert.java:472)
    at org.junit.Assert.assertEquals(Assert.java:456)
    at xxx.yyy.CalendarBug.calendarBug(CalendarBug.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

似乎格式化程序更改了utcCalendar的内部状态,并导致测试通过。

我在这里滥用了什么吗? JDK中是否有针对此的打开错误?

Java版本:
liptak@XXXXXX:~$ java -version
java version "1.7.0_60"
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

最佳答案

编辑:由于只读取部分代码,我匆忙发布了错误的答案。
这是一个全新的答案:
首先,Calendar的Javadoc指出了实际计算Calendar的时间:

您遇到的行为不是错误。
您创建一个Calendar实例,然后创建该实例的副本。然后,您更改克隆实例的时区。
但是,有两种情况:

  • 由于在打印语句中调用utcCalendar.getTime(),因此在创建克隆之前已计算了原始Calendar对象的时间。克隆的Calendar包含此时间,因此更改时区不会更改该时间(因为getTimeInMillis()返回UTC milliseconds from the epoch,所以它不取决于使用哪个时区)。调用assertEquals(istCalendar.getTimeInMillis(), utcCalendar.getTimeInMillis())时不会重新计算时间,因此测试通过。
  • 在创建克隆之前,不会计算原始Calendar对象的时间。然后,您可以更改克隆实例的时区。现在,当您调用assertEquals(istCalendar.getTimeInMillis(), utcCalendar.getTimeInMillis())时,两个Calendar实例中的时间都是计算出来的,并且它们是不同的,因为10AM IST和10AM UTC不是同一时间(它们相差5.5小时,这正是14288130009791428832800979之间的区别)。
  • 09-27 22:02