wood burning stoves 2.0*
The moose likes Java in General and the fly likes Display time in other timezones Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Display time in other timezones" Watch "Display time in other timezones" New topic
Author

Display time in other timezones

Bryan McElhenney
Greenhorn

Joined: Apr 08, 2005
Posts: 2
I'm trying to output a list of the current time in a few different cities but I'm having a hard time doing this. So far I've managed to create a Calendar object for a different timezone. I can extract the hours, minutes, etc from this successfully and manage to parse them all together with large mangle of code. When I try to use the getTime() method on this object, however, it returns the current local time. Unfortunately, the Calendar object doesn't seem to have some handy feature like Calender.toDate(). The only other crazy way I thought of doing this was to get the timezone offset and do some subtraction. Can anyone suggest a simple solution? So far I have this:

Date now = new Date();
Calendar c = Calendar.getInstance (TimeZone.getTimeZone ("JST"),new Locale ("ja", "JP"));

I want this to display something like:

Time in Austin: Friday 4:38 PM
Time in Japan: Saturday 6:38 AM

All I need is something to get a Date object adjusted for timezone and I'd be set.
Bryan McElhenney
Greenhorn

Joined: Apr 08, 2005
Posts: 2
Well, I looked a bit harder through the mountain of search returns and found this post:

http://www.coderanch.com/t/371134/java/java/Calendar-Time-Zones

Now its working. I'll copy it here in case someone hits this on a search.

Date time = new Date();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time.getTime());
SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
formatter.setTimeZone( TimeZone.getTimeZone( "JST" ) );
formatter.format( cal.getTime() );

Using formatter seems to be the solution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display time in other timezones
 
Similar Threads
Problem with Calendar().getTime()
Convert Date from one timezone to another
TimeZone, calendar
Problem with converting TimeZone as PST
Adding date and time to a class?