Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java timestamp, time conversion

807606Mar 29 2007 — edited Mar 30 2007
Hi,
I need to convert a java timestamp which is in EST/EDT (Eastern standard/daylight time) to a timestamp in MST (mountain standard time) that reflects the time in MST.
Here is the code i have written. I wanted to know if there was a better way of doing this:

private Timestamp convertToMST(Timestamp stamp){
TimeZone tzMST = TimeZone.getTimeZone("MST");
TimeZone tzLocal = TimeZone.getDefault();
int dayLightSavingsOffset = 0;
if(tzLocal.useDaylightTime()){
// check that the timestamp stamp is in DST.
if(tzLocal.inDaylightTime(new Date(stamp.getTime()))){
// the timestamps timezone is following daylight savings time
// so subtract the amount to time of the daylight savings from the timestamp
dayLightSavingsOffset = tzLocal.getDSTSavings();
}
}
int mstOffSet = tzMST.getRawOffset();
int localOffSet = tzLocal.getRawOffset();
if(mstOffSet<0) mstOffSet=Math.abs(mstOffSet);
if(localOffSet<0) localOffSet=Math.abs(localOffSet);
// subtract the 2
int diff = mstOffSet - localOffSet;
diff = Math.abs(diff);
long current = stamp.getTime() - dayLightSavingsOffset;
if(mstOffSet>localOffSet){
current = current - diff; // subtract diff from current
}else if(mstOffSet<localOffSet){
current = current + diff; // add diff to current
} // if not these --simply return current

return new Timestamp(current);
}

Thank you
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 27 2007
Added on Mar 29 2007
2 comments
711 views