Author Topic: Timezone support  (Read 53802 times)

Dev Team

  • Customer
  • Hero Member
  • *
  • Posts: 593
    • View Profile
Timezone support
« on: July 02, 2012, 10:13:09 AM »
AjaxSwing supports getting Timezone from user browser, timezone API:
Code: [Select]
int getTimeZoneCurrentOffset()  - returns the time difference between GMT and current local time, in minutes, includes daylight saving time offset
int getTimeZoneRawOffset()  - returns the time difference between GMT and local time, in minutes, not affected by daylight saving time
boolean getTimeZoneUseDaylight()  - returns if timezone use daylight saving time
TimeZone getTimeZone()  - returns timezone for client browser

All methods are inside ClientAgent.getCurrentInstance().

Notice: getTimeZone() would not always return exact client timezone, browser does not tell server which timezone it is using, only GMT offset, getTimeZone() method would return timezone that has same GMT offset as browser, same daylight offset and exact same dates when timezone switch to daylight time and from daylight time, so time in this timezone should always match to client local time.
If no information is available from browser, server timezone would be returned.

Example:
TimeZone timezone = null;
if(AjaxSwingManager.isAjaxSwingRunning()) {
    timezone = ClientAgent.getCurrentInstance().getTimeZone();
} else {
    timezone = TimeZone.getDefault();
}
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat();
format.setTimeZone(timezone);
format.format(date);