my dog learned polymorphism
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

Display time in other timezones

Bryan McElhenney

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

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

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

Date time = new Date();
Calendar cal = Calendar.getInstance();
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:
subject: Display time in other timezones
It's not a secret anymore!