Why does the date 1986/05/04 start from 1:00 but not 0:00 in java ( strange behaviours of calendar with the time zone - Shanghai )?

StackOverflow https://stackoverflow.com/questions/20755695

Question

Hi~ I met a strange problem when I tried to get the interval between two dates. The problem is that when the date 1986/05/04 is within the two dates used in my calculation, the result is less than what I want. That's all because 1986/05/04 starts from 1:00 but not 0:00 and here is the test code:

public static void main( String[] args )
{
    Calendar bc = new GregorianCalendar( 1986, 4, 4, 0, 0, 0 );
    Calendar ec = new GregorianCalendar( 1986, 4, 5, 0, 0, 0 );
    System.out.println( "bch: " + bc.get( Calendar.HOUR_OF_DAY ) + " bcm: " + bc.get( Calendar.MINUTE )
            + " bcs: " + bc.get( Calendar.SECOND ) + " bcms: " + bc.get( Calendar.MILLISECOND ) );
    System.out.println( "ech: " + ec.get( Calendar.HOUR_OF_DAY ) + " ecm: " + ec.get( Calendar.MINUTE )
            + " ecs: " + ec.get( Calendar.SECOND ) + " ecms: " + ec.get( Calendar.MILLISECOND ) );
    System.out.println( "Interval: " + ((ec.getTimeInMillis() - bc.getTimeInMillis())) );
}

and the output is:

bch: 1 bcm: 0 bcs: 0 bcms: 0
ech: 0 ecm: 0 ecs: 0 ecms: 0
Interval: 82800000

I want 86400000. You can see that though I set the hour to 0 for 1986/05/04, it's still no use. I really can't understand the result and wonder whether there is some special rules in the gregorian calendar I don't know or it's just a bug of the gregorian calendar. I will appreciate it if you could help me.

Environment:

JRE 1.6
ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]
Was it helpful?

Solution

This is normal there is no 0:00 the 4th May 1986 in China. There was a daylight time saving change at this date (1986/05/04) in China. A quick google about this confirms it.

Just for information, China observed daylight time saving between 1986 and 1991 according to this page. They do not observe it anymore.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top