• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Display time in other timezones

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I looked a bit harder through the mountain of search returns and found this post:

https://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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic