I work on an application that uses a java.util.Date to hold a time. Every time get 1900-01-01 as date. The application has a utility class for adding minutes to a time. On Linux I get 1900-01-01 08:51:32 as result when adding 9*60 + 32 = 572 minutes to 1900-01-01 00:00:00 where 1900-01-01 09:32:00 is expected. How is this difference possible?
This difference does not occur on Windows 7 or on Linux for 1901 or 2013.
Compiled/Tested with Java: 1.6.0_23-b05 and 1.7.0_11-b21
Linux version: 2.6.18-308.el5 #1 SMP Fri Jan 27 17:17:51 EST 2012 x86_64 x86_64 x86_64 GNU/Linux
Timezone: CEST
package nl.brandweer.test.calendar;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class CalendarTest {
public static void main(String[] args) {
System.out.println("Hello Mars");
CalendarTest test = new CalendarTest();
test.test1();
test.test2();
test.test3();
}
GregorianCalendar myCalendar;
private void test1() {
System.out.println("Test 1");
this.myCalendar = new GregorianCalendar(1900, 0, 1, 0, 0, 0);
System.out.println("myCalendar: " + myCalendar);
System.out.println("myCalendar.getTime(): " + myCalendar.getTime());
addMinutes(572);
System.out.println("myCalendar.getTime(): " + myCalendar.getTime());
}
private void test2() {
System.out.println("Test 2");
this.myCalendar = new GregorianCalendar(1901, 0, 1, 0, 0, 0);
System.out.println("myCalendar: " + myCalendar);
System.out.println("myCalendar.getTime(): " + myCalendar.getTime());
addMinutes(572);
System.out.println("myCalendar.getTime(): " + myCalendar.getTime());
}
private void test3() {
System.out.println("Test 3");
this.myCalendar = new GregorianCalendar(2013, 0, 1, 0, 0, 0);
System.out.println("myCalendar: " + myCalendar);
System.out.println("myCalendar.getTime(): " + myCalendar.getTime());
addMinutes(572);
System.out.println("myCalendar.getTime(): " + myCalendar.getTime());
}
/**
* Add minutes to Time.<p>
* The date stays te same!
* @param value Minutes
*/
protected void addMinutes(int value) {
int year = getYear();
int month = getMonth() - 1;
int day = getDay();
this.myCalendar.add(Calendar.MINUTE, value);
this.myCalendar.set(Calendar.YEAR, year);
this.myCalendar.set(Calendar.MONTH, month);
this.myCalendar.set(Calendar.DAY_OF_MONTH, day);
}
/**
* @return The day of month
*/
public int getDay() {
return get(Calendar.DAY_OF_MONTH);
}
/**
* @return Month [1..12]
*/
public int getMonth() {
return get(Calendar.MONTH) + 1;
}
public int getYear() {
return get(Calendar.YEAR);
}
protected int get(int field) {
return myCalendar.get(field);
}
}
A work-a-round is to change the year to 1901 or 2013 while doing pure time calculations.
UPDATE: The same error occurs for 1900 with Linux in a Joda-time implementation.