20 de out. de 2006

Daylight problems in Java 1.4

In Brazil the daylight time is always changed in somehow by the government. What looks a simple change, make us, T.I workers having troubles.
As Java has a platform independent concept, in Java 1.4 daylight problems, even solved in Windows. Problem is that, Java doesn't get system's information about daylight, but it's own. So I found some solutions for this.

- The first solution, you can make a code change before getting system date. (original code by Benicio Silva Gontijo)

import java.util.*

public class DaylightTest { public static void main(String[] args){ // Brasilia(BR) Timezone SimpleTimeZone
brt= new SimpleTimeZone(-3 * 60 * 60 * 1000,"GMT-3:00"); // Set rules for Brazilian Summer Time brt.setStartRule(Calendar.NOVEMBER, 1, Calendar.SUNDAY,0); brt.setEndRule( Calendar.FEBRUARY, -1, Calendar.SUNDAY,0); Calendar todaysDay= new GregorianCalendar( brt); System.out.println(todaysDay.get(Calendar.HOUR_OF_DAY) + ":" + todaysDay.get(Calendar.MINUTE) + ":" + todaysDay.get(Calendar.SECOND); } }


- As second solution, you can just add arguments to your vm initialization: -Duser.timezone=$the_time_zone
- And finally the problem is solved in Java 5 so, if you can - migrate to it ;-)

Sources:
http://www.mundooo.com.br/php/modules.php?name=News&file=article&sid=486
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5033603
http://www.petefreitag.com/item/171.cfm


Nenhum comentário: