| Author |
timezone conversion using DST
|
sony rao
Greenhorn
Joined: Jun 18, 2007
Posts: 20
|
|
hi all iam working on timezone conversion. when source and destination timezones dont fall under DST my conversion function is working fine. But when any timezone is under DST, conversion is not appropriate.iam using jdk1.5.0_01 and jre1.5.0_01. i also have tzupdater.jar in my classpath Iam trying to convert 1/10/2007 in Pacific/Auckland to equivalent time in Africa/Casablanca. here goes my code... { TimeZone tz1=TimeZone.getTimeZone("Pacific/Auckland"); int fromDST=tz1.getDSTSavings(); TimeZone tz2=TimeZone.getTimeZone("Africa/Casablanca"); int toDST=tz2.getDSTSavings(); Calendar cal=Calendar.getInstance(); cal.set(Calendar.YEAR,2007); cal.set(Calendar.MONTH,9); cal.set(Calendar.DATE,1); cal.set(Calendar.HOUR_OF_DAY,HtmlHelper.getHourValueInt (Integer.parseInt("12"),"AM")); //getHourValueInt - method that gives hour value in terms of AM PM cal.set(Calendar.MINUTE,0); convertLocalToLocalWithDST(new Timestamp(cal.getTime().getTime()) ,tz1,fromDST,tz2,toDST)); } public static Timestamp convertLocalToLocalWithDST(Timestamp timestamp, TimeZone fromZone,int fromDST, TimeZone toZone,int toDST) { return convertGmtToLocalWithDST(convertLocalToGmtWithDST(timestamp, fromZone,fromDST), toZone,toDST); } public static Timestamp convertLocalToGmtWithDST(Timestamp timestamp, TimeZone localZone,int fromDST) { Timestamp newTimestamp = null; if (timestamp != null) { Calendar calendar = new GregorianCalendar(localZone); calendar.setTime(timestamp); calendar.set(Calendar.DST_OFFSET,fromDST); int zoneOffset = calendar.get(Calendar.ZONE_OFFSET); if(localZone.inDaylightTime(timestamp)){ newTimestamp = new Timestamp(calendar.getTime().getTime() - zoneOffset - fromDST); }else{ newTimestamp = new Timestamp(calendar.getTime().getTime() - zoneOffset + fromDST); } } return newTimestamp; } public static Timestamp convertGmtToLocalWithDST(Timestamp timestamp, TimeZone localZone,int toDST) { Timestamp newTimestamp = null; if (timestamp != null) { Calendar calendar = new GregorianCalendar(localZone); calendar.setTime(timestamp); calendar.set(Calendar.DST_OFFSET,toDST); int zoneOffset = calendar.get(Calendar.ZONE_OFFSET); if(localZone.inDaylightTime(timestamp)){ newTimestamp = new Timestamp(calendar.getTime().getTime() + zoneOffset + toDST ); } else{ newTimestamp = new Timestamp(calendar.getTime().getTime() + zoneOffset + toDST +toDST ); } } return newTimestamp; } converted time should be 30 Sep 2007 11am but iam getting 30 Sep 2007 12am can anybody please help me out [ November 08, 2007: Message edited by: sony rao ]
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12950
|
|
Originally posted by sony rao: i also have tzupdater.jar in my classpath
You are not supposed to put that JAR in your classpath. Read the following instructions for using the timezone updater tool: Timezone Updater Tool
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: timezone conversion using DST
|
|
|