The following code appears to demonstrate a bug in java.util.Date whereby an hour gets added on if the local clock is set to GMT with DST adjustment on and the time is before Nov 1 1971. My first assumption is always that I've got it wrong. Can anyone see what's wrong (or is this really a Java bug)? What's significant about Nov 1 1971?
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
class JavaUtilDateBug
{
private static void demo() throws Exception
{
// UK developers usually have the clock on their development machines set
// to "Europe/London" (i.e. GMT with daylight saving). Set it explicitly
// here so readers in other countries can see the problem too.
TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
Locale.setDefault(Locale.ENGLISH);
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String strJan1st1970Expected = "Thu Jan 01 00:00:00 GMT 1970";
String strJan1st1970Actual = dateFormat.parse(strJan1st1970Expected).toString();
System.out.println("strJan1st1970Actual: " + strJan1st1970Actual); // -> "Thu Jan 01 01:00:00 GMT 1970"
boolean jvmHasDateBug = !strJan1st1970Expected.equals(strJan1st1970Actual);
System.out.println("jvmHasDateBug: " + jvmHasDateBug); // -> true
// The anomaly only seems to affect times before 1 Nov 1971.
final String strNov1st1971 = "Mon Nov 01 00:00:00 GMT 1971";
assert strNov1st1971.equals(dateFormat.parse(strNov1st1971).toString());
}
public static void main(String[] args)
{
try
{
demo();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
My Java environment:
java version "1.6.0_13"
Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
Java HotSpot(TM) Client VM (build 11.3-b02, mixed mode, sharing)