Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert from GMT to local zone using Daylight savings 1

Status
Not open for further replies.

EwS

Programmer
Dec 30, 2002
398
US
I'm looking for code that converts date/time in GMT format to the local time zone format. I found some examples on the web, but they don't account for daylight savings. Is there any java function that would do that for me?
 
Will getTimeZone() do that? Can anyone post some code?
 
Do you mean that you want to convert a UTC Date/Time in a Local Date/Time? Assuming that your Java program runs in local time (you can enforce that with the java runtime parameter -Duser.timezone).

long utcMiliseconds = utcDate.getTime();
GregorianCalendar cal = new GregorianCalendar();
cal.setTimeInMillis(utcMiliseconds);
Date localTime = new Date(utcMiliseconds + cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET));
 
tom62,
Yes, I want to convert from UTC to local date/time.
I tried your code, but I'm getting ParseException on the last line:
Code:
Date localTime = new Date(utcMiliseconds + cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET));
 
tom62,
Your code works correctly. Thank you very much. I appreciate fast and accurate response!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top