• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

timezone conversion using DST

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic