When I set the month on a date representing 1/1/1970, and then immediately get the month back, it's off by one.
import java.util.Date;
@Test
public void monthShouldBeExpectedValue() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(0));
int expectedMonth = Calendar.JUNE;
calendar.set(Calendar.MONTH, expectedMonth);
int actualMonth = calendar.get(Calendar.MONTH);
assertThat(actualMonth, equalTo(expectedMonth)); // test fails: expected 5 got 6
}
If I change this line
calendar.setTime(new Date(0));
to
calendar.setTime(new Date()); // use 'today' instead of 1/1/1970
then the test passes. Anyone know why?
Edit
The printed version of the dates are:
new Date(0): Wed Dec 31 19:00:00 EST 1969
date from calendar: Tue Jul 01 19:00:00 EDT 1969
I'm running an old JDK: 1.6.0_30-b12 (64 bit)
I'm in Eastern Standard Time.