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.
posted 10 years ago
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(); cal.setTimeInMillis(time.getTime()); SimpleDateFormat formatter = new SimpleDateFormat("h:mm a"); formatter.setTimeZone( TimeZone.getTimeZone( "JST" ) ); formatter.format( cal.getTime() );