This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes TimeZone Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Java in General
Bookmark "TimeZone Conversion" Watch "TimeZone Conversion" New topic

TimeZone Conversion

Amit Ghilidyal

Joined: Mar 14, 2000
Posts: 2
i would appreciate if any one could help me.
I need to convert date generated from a any given timezone
to any other timezone.

Tom P
Ranch Hand

Joined: May 06, 2000
Posts: 88
You can use the TimeZone and Calendar objects to do all the work for you. You just have to know the time zone id for the time zones you want to convert.
Let's assume that you want to convert from eastern standard time to pacific standard time:
TimeZone tzPST = TimeZone.getTimeZone("PST");
TimeZone tzEST = TimeZone.getTimeZone("EST");
Calendar calPST = new GregorianCalendar(tzPST);
Calendar calEST = new GregorianCalendar(tzEST);
// now load calEST with the time you want to convert
//calPST now has the time in pacific standard time
System.out.println(calPST.get(Calendar.HOUR_OF_DAY) + ":" + calPST.get(Calendar.MINUTE));
It is sorta covered in the JavaRanch Style Guide.
subject: TimeZone Conversion
It's not a secret anymore!