Calendar.add for Central African Timezone (GMT +2)
Hi all,
I am trying to add x days to 1900-01-01 06:00 at the Central African Timezone (GMT +2). However, the result from simple Calendar.add(Calender.DATE, numDays) is giving me 4 min and 12 seconds less. This only happens when the final date pass March 1, 1903 (which is the only DST day of Central African).
ie:
origDate = 1900-01-01 06:00 CAT
numDaysToAdd = 1154
resultDate = 1903-03-01 05:55:48 CAT (instead of 06:00)
I searched around the bug database and someone suggests to always use GMT to do calendar calculation. However, with that, I will get 2 extra hours.
ie:
origDate = 1900-01-01 06:00 CAT
numDaysToAdd = 1154
resultDate = 1903-03-01 08:00:00 CAT (instead of 06:00)
Any idea on how to work this out?
Here is the test code:
import java.util.*;
import java.math.*;
public class Test2
{
public static void main(String[] args) {
TimeZone.setDefault(TimeZone.getTimeZone("Africa/Harare"));
// Create a date set the Time to 1900/01/01 06:00 AM
Calendar now = Calendar.getInstance();
now.set(1900, 0, 1, 6, 0, 0);
Date d = new Date(now.getTimeInMillis());
System.out.println(d.toString());
System.out.println();
// try to add 1154 days to d with GMT timezone
Date d2 = addDaysWithGMT(d, 1154);
System.out.println("Extra two hours: " + d2.toString());
System.out.println();
// try to add 1154 days to d with original timezone
d2 = addDays(d, 1154);
System.out.println("Missing 4 minutes and 12 seconds: " + d2.toString());
}
public static Date addDaysWithGMT(Date d, int days) {
Calendar now=Calendar.getInstance();
now.setTimeZone(TimeZone.getTimeZone("GMT"));
now.set(d.getYear() + 1900, d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds());
System.out.println("after set: " + now.getTime());
now.add(Calendar.DATE,days);
System.out.println("after add: " + now.getTime());
now.setTimeZone(TimeZone.getDefault());
return new Date(now.getTimeInMillis());
}
public static Date addDays(Date d, int days) {
Calendar now=Calendar.getInstance();
now.set(d.getYear() + 1900, d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds());
System.out.println("after set: " + now.getTime());
now.add(Calendar.DATE,days);
System.out.println("after add: " + now.getTime());
return new Date(now.getTimeInMillis());
}
}
Any help would be appreciated.
Thanks,
Winnie

