Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Display time in other timezones

 
Bryan McElhenney
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic