OK - I feel pretty dumb asking such a basic question, but hey.
I'm trying to get the current time in a different timezone in a Java webapp. I've tried the following obvious solution: in my servlet,
Calendar localCalendar = Calendar.getInstance(myBean.getTimeZone());
then I pass the calendar object through to a JSP as a request attribute 'localCalendar':
It is now: [${requestScope.localCalendar.time}]
in TimeZone ${requestScope.localCalendar.timeZone.ID}
but my output seems to ignore the timezone set, i.e.
It is now: [Thu Nov 26 10:01:03 GMT 2009] in TimeZone Indian/Mahe
I'm guessing it's something to do with Locale settings, is there any way to just get the time formatted for my Locale, in another timezone?
解决方案
The internal data of Calendar is basically:
An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).
So, it holds a long that will be the same no matter what operations you use on it and toString makes no promises.
You want to format the date as per locale and timezone. You can do this in the presentation layer (the JSP) using things like JSTL or you can format the date in your bean using a DateFormat and return the value as a string.