3

My server was setted with 'America/Sao_Paulo' TimeZone, and this year in october 21th the daylight saving started.

Why if I do this

public static void main(String[] args){

    Calendar d1 = new GregorianCalendar(2012, 9, 19, 0, 0, 0);

    Calendar d2 = new GregorianCalendar(2012, 9, 22, 0, 0, 0);

    while(d1.compareTo(d2) <= 0){
        System.out.println("\nBEFORE: " + d1.getTime());
        d1.add(Calendar.DAY_OF_MONTH, 1);
        System.out.println("AFTER: " + d1.getTime());
    }


}

I have this output

BEFORE: Fri Oct 19 00:00:00 BRT 2012

AFTER: Sat Oct 20 00:00:00 BRT 2012

BEFORE: Sat Oct 20 00:00:00 BRT 2012

AFTER: Sun Oct 21 01:00:00 BRST 2012

BEFORE: Sun Oct 21 01:00:00 BRST 2012

AFTER: Mon Oct 22 01:00:00 BRST 2012

when I do this d1.add(Calendar.DAY_OF_MONTH, 1) just the day should be increased not the hour too, right? How can I avoid this, and maintain the original hour, I do not want to use GMT-3, I need to now when is DST.

thanksss

sampert
  • 51
  • 4
  • Following post will help you.. if you wanna do it with JodaTime API http://stackoverflow.com/questions/1449500/get-daylight-saving-transition-dates-for-time-zones-in-java – Vishal Dec 06 '12 at 07:09

1 Answers1

0

Since Sao_paulo has no midnight on transition day 21st, then 21st will start from 1 AM. For the 22nd, you add one day, then it keeps 1 AM and so on. To make it right, you should add days from the 19th instead of calculating by the previous day.

Gutblender
  • 1,340
  • 1
  • 12
  • 25
angela
  • 1