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

[2623 byte] By [WinnieTama] at [2007-11-27 8:48:05]
# 1

Not going to read all that, especially with the unformatted code. However, a couple of things leap out at me.

* 1903

* 4 min., 12 sec.

As a rough guess, this sounds like an accumulation of leap seconds.

As a second out-of-my-butt guess--this TZ might be one of the less common/"popular"/well-known ones, so it's possible that it has various adjustments to its rules over the years. Note that I don't really know what I'm talking about here--just venting a vague gut feeling.

jverda at 2007-7-12 20:54:25 > top of Java-index,Java Essentials,Java Programming...