Wednesday, April 27, 2011

Timezone Conversion with Daylight saving

Timezone conversion from one timezone to other timezone for the given time or hour string input.

While converting the timezone we have to consider DST standard also. in jdk 1.3. and 1.4 DST was not taken care internally by jdk, But from jdk 1.5 it is taken care.

What is daylight saving or DST?

Daylight Saving Time (or summertime as it is called in many countries) is a way of getting more light out of the day by advancing clocks by one hour during the summer. During Daylight Saving Time, the sun appears to rise one hour later in the morning, when people are usually asleep anyway, and sets one hour later in the evening, seeming to stretch the day longer.
About Daylight Saving Time.

for brief history on daylight visit the below link
http://en.wikipedia.org/wiki/Daylight_saving_time

In jDK we have a mehod inDaylightTime() in TimeZone class to check is given date is in DST for the timezone

ex : timeZone.inDaylightTime(new Date());

sample code :

public static boolean IsTimeZoneInDST(TimeZone timeZone) throws ParseException{
return timeZone.inDaylightTime(new Date());
}

now we have to convert the time from one timezone to another by using the timezone rawOffset.

sample :

public static Timestamp convert(Timestamp timeStamp, TimeZone fromTimeZone, TimeZone toTimeZone) {

// if null, convert from UTC
long offFrom = fromTimeZone == null ? 0 : fromTimeZone.getOffset(timeStamp.getTime());

// if null, convert to UTC
long offTo = toTimeZone == null ? 0 : toTimeZone.getOffset(timeStamp.getTime());

return new Timestamp(timeStamp.getTime()/1000*1000 + timeStamp.getNanos()/1000000 + offTo - offFrom);
}
now in above we have some thing fishy i.e

timeStamp.getTime()/1000*1000 + timeStamp.getNanos()/1000000

the above code was for the jdk 1.3 and 1.4, Because its was a bug in the jdk see the below link
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4679060

sample TimeZoneConverter.java class download

import java.sql.Timestamp;
import java.util.TimeZone;
import java.util.logging.Logger;
import java.util.*;
public class TimeZoneConvertor {
private static final TimeZone defaultTimeZone = TimeZone.getDefault();
private static final Logger log = Logger.getLogger("TimeZone Convertor");
/**
* Convert input time stamp to UTC/GMT
* @param timestamp
* @return
*/
public static Timestamp convertToUTC(Timestamp timestamp){
return convert(timestamp,defaultTimeZone,null);
}
/**
* Convert from UTC/GMT
* @param timestamp
* @return
*/
public static Timestamp convertFromUTC(Timestamp timestamp){
return convert(timestamp,null,defaultTimeZone);
}
/**
* Convert the Timestamp From one timezone to another
*
* @param timestamp
* @param fromTimeZone
* @param toTimeZone
* @return
*/
private static Timestamp convert(Timestamp timestamp,TimeZone fromTimeZone, TimeZone toTimeZone) {
long offFrom = fromTimeZone == null ? 0 : fromTimeZone.getOffset(timestamp.getTime());
long offTo = toTimeZone == null ? 0 : toTimeZone.getOffset(timestamp.getTime());
return new Timestamp(timestamp.getTime()/1000*1000 + timestamp.getNanos()/1000000 + offTo - offFrom);
}
public static void DisplayOffset(String text,int rawOffset,TimeZone timeZone){
int hour = rawOffset/(60*60*1000);
int minutes = Math.abs(rawOffset/(60*1000)) % 60;
System.out.println(text+timeZone.getDisplayName()+ " "+hour+ ":"+minutes+" hours");
}
public static void main(String[] args) {
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
// To Get Available TimeZones
/*String []TimezoneIDs = TimeZone.getAvailableIDs();
for(String Id : TimezoneIDs)
System.out.println("Timzone ID : \t"+Id);*/
TimeZone fromTimeZone = TimeZone.getTimeZone("Asia/Kolkata");
TimeZone toTimeZone = TimeZone.getTimeZone("America/Chicago");
/*
* Now we have to convert from current timezone to target time
*
* First we have to convert the fromTimezone Time to GMT i.e difference with GMT
*
* Get the converted timestamp i.e in GMT time now
*
* now convert timestamp from GMT to target time zone
*/
System.out.println("******************** Off set from GMT ************");
DisplayOffset("From : ", fromTimeZone.getRawOffset(), fromTimeZone);
DisplayOffset("To : ", toTimeZone.getRawOffset(), toTimeZone);
System.out.println("Current Time \t"+timestamp);
timestamp = convert(timestamp, fromTimeZone, TimeZone.getTimeZone("GMT"));
System.out.println("Timestamp to GMT\t"+timestamp);
timestamp = convert(timestamp, TimeZone.getTimeZone("GMT"), toTimeZone);
System.out.println("GMT to target Timezone \t"+timestamp);
System.out.println("**************************************************");
}
}

Related Posts:

  • 6 Steps for contribute your jar/project to open source maven repository Prerequisites: 1. Sign Up at Sonatype. 2. Create your project in JIRA. 3. Create a Key Pair. 4. Create and Sign your Artifacts. 5. Deploy your Artifacts to your Staging Repository. 6. Promote your Repository Step #1: Sign Up… Read More
  • Command execution by Java Processorsample code to execute any command on operating system by using java processor. for example : To find jdk installed in system, We can run the command "where javac" for window and "where is javac" for linux the above command… Read More
  • Whois domain detail by javaApache commmon org.apache.commons.net.whois.WhoisClient class provides the domain details. Following details: Domain Name: UTTESH.COM Registrar: GODADDY.COM, LLC Sponsoring Registrar IANA ID: 146 Whois Server: whois.godaddy… Read More
  • Spring + JPA + Hibernate Sample IntroductionStand-alone Spring application sample, Spring framework supports the persistence layer to be fully implemented through JPA. Simple example of configure the Spring needed components to perform persistence over sta… Read More
  • Java get system environment variable detailsGet System environment variable details. import java.util.Map; /** * * @author Uttesh Kumar T.H. */ public class FindJdk { public static void main(String[] args) { Map<String, String> env = System.gete… Read More

0 comments:

Post a Comment