Home  >  Core Java

Example of SimpleTimeZone in Java

By Arvind Rai, June 09, 2013
java.util.SimpleTimeZone represents TimeZone for GregorianCalendar. The object of SimpleTimeZone is created as
new SimpleTimeZone(int rawOffset, String ID)
ID is available TimeZone id. We need to set setStartRule and setEndRule for daylight saving. Find the example

SimpleTimeZoneDemo.java
package com.concretepage.util;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
public class SimpleTimeZoneDemo {
    public static void main(String[] args) {
         String[] ids = TimeZone.getAvailableIDs(-6 * 60 * 60 * 1000);
         SimpleTimeZone stz = new SimpleTimeZone(-6 * 60 * 60 * 1000, ids[0]);
         stz.setStartRule(Calendar.MARCH, 1, Calendar.MONDAY, 3 * 60 * 60 * 1000);
         stz.setEndRule(Calendar.NOVEMBER, -1, Calendar.SUNDAY, 3 * 60 * 60 * 1000);
         System.out.println(stz.getDisplayName(Locale.ITALIAN));
         System.out.println(stz.getDisplayName(Locale.ENGLISH));
         System.out.println(stz.getDisplayName(Locale.FRANCE));
         //checks if given date is day light saving
         System.out.println(stz.inDaylightTime(new Date()));
         
         //use SimpleTimeZone with GregorianCalendar
         Calendar calendar = new GregorianCalendar(stz);
         Date trialTime = new Date();
         calendar.setTime(trialTime);
    }
}
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us