Java Check Daylight Saving Time Utility

Aim
This simple example is to create a small java application that you can use to view timezones and see when the JDK/JRE changes to daylight savings time or changes back from daylight savings time. It's important to know when DST occurs, especially for systems that have time sensitive information.

You can use the java/oracle tzupdater.jar utility to update your jre/jdk for all the latest timezones and daylight saving time configurations.

Application
This application enumerates the timezones available on a system. You can then select a timezone and set a date and then increment that date to see when DST occurs. This utility uses the 'inDaylightTime()' function to test if the time is in daylight savings time or not.

 1. import java.util.*;
 2. import java.text.*;
 3. 
 4. public class tzutility{
 5.   public static void main(String[] args){
 6.     Date date = new Date();
 7.     String TimeZoneIds[] = TimeZone.getAvailableIDs();
 8.     for(int i = 0; i < TimeZoneIds.length; i++){
 9.       TimeZone tz = TimeZone.getTimeZone(TimeZoneIds[i]);
10.       String tzName = tz.getDisplayName(tz.inDaylightTime(date), TimeZone.LONG);
11.       System.out.println(TimeZoneIds[i] + ":   ");
12.     }
13.     try{
14.       DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
15.       TimeZone nswtz = TimeZone.getTimeZone("Australia/Queensland");
16.       Date d = df.parse("2010-09-01");
17.       Calendar cal = Calendar.getInstance();
18.       cal.setTime(d);
19.       for(int i = 0 ; i < 100 ; i++ ){
20.         System.out.println(cal.getTime()+" "+nswtz.inDaylightTime(cal.getTime()));
21.         cal.add(Calendar.DATE, 1);
22.       }
23.     }catch(Exception e){
24.       e.printStackTrace();
25.     }
26.   }
27. }
Hide line numbers

No comments: