سؤال

After a lot of debugging I narrowed my problem to the following snippet:

public static void calendarBug() {
    for (int i=0 ; i<6 ; i++) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(2015, 2, 27, i, 0);
        System.out.println(c.getTime());
    }
}

Running this gives the following output:

Fri Mar 27 00:00:00 IST 2015
Fri Mar 27 01:00:00 IST 2015
Fri Mar 27 03:00:00 IDT 2015
Fri Mar 27 03:00:00 IDT 2015
Fri Mar 27 04:00:00 IDT 2015
Fri Mar 27 05:00:00 IDT 2015

Does anyone know why does c.set(2015,2,27,2,0) returns 3AM instead of 2AM?

هل كانت مفيدة؟

المحلول

Think about that your are at time just at DST time, It moves forward one hour or moves back one hour without living this time portion.

So It is not bug, it is feature.

When you change the timezone which does not use DST (lets say Canada/East-Saskatchewan), you will see what you have expected.

Here is an example.

public static void calendarBug() {
    for (int i=0 ; i<6 ; i++) {
        Calendar c = Calendar.getInstance();

        c.setTimeZone(TimeZone.getTimeZone("Canada/East-Saskatchewan"));
        c.clear();
        c.set(2015, 2, 27, i, 0);
        System.out.println(c.getTime());
    }
}

Fri Mar 27 08:00:00 EET 2015
Fri Mar 27 09:00:00 EET 2015
Fri Mar 27 10:00:00 EET 2015
Fri Mar 27 11:00:00 EET 2015
Fri Mar 27 12:00:00 EET 2015
Fri Mar 27 13:00:00 EET 2015

نصائح أخرى

That is most likely a DST switchover for your timezone.

Mar 27 is the last Friday of March in 2015. This is the day when the DST switchover takes place in the Israel, Jordan, Syria, the West Bank etc.

See

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top