Time Zone

From Documentation
Revision as of 07:34, 8 July 2022 by Hawk (talk | contribs) ((via JWB))


The time zone used to process requests and events is, by default, determined by the JVM's default (i.e. java.util.TimeZone.getDefault()).

In this section, we will discuss how to configure ZK to use a time zone other than JVM's default. For example, you might configure ZK to use the preferred time zone that a user-specified in his or her profile.

The Decision Sequence of Time Zone

The time zone is decided in the following sequence.

  1. It checks if an attribute called org.zkoss.web.preferred.timeZone defined in the HTTP session (aka., Session). If so, use it.
  2. It checks if an attribute called org.zkoss.web.preferred.timeZone defined in the Servlet context (aka., Application). If so, use it.
  3. It checks if a property called org.zkoss.web.preferred.timeZone defined in the library property (i.e., Library). If so, use it.
  4. If none of them is found, JVM's default will be used.

With this sequence in mind, you could configure ZK to use the correct time zone based on the application requirements.

Application-level Time Zone

If you want to use the same time zone for all users of the same application, you can specify the time zone in the library property. For example, you could specify the following in WEB-INF/zk.xml:

  • Line 3: the value can be anything accepted by java.util.TimeZone.getTimeZone()

Alternatively, if you prefer to specify it in Java, you can invoke Library.setProperty(String, String). Furthermore, to avoid typo, you could use Attributes.PREFERRED_TIME_ZONE as follows.

Library.setProperty(Attributes.PREFERRED_TIME_ZONE, "PST");

Per-user Time Zone

Because ZK will check if a session attribute for the default time zone, you could configure ZK to have per-user time zone by specifying the attribute in a session.

For example, you can do this when a user logins.

 void login(String username, String password) {
     //check password
     TimeZone preferredTimeZone = ...; //decide the time zone (from, say, database)
     session.setAttribute(Attributes.PREFERRED_TIME_ZONE, preferredTimeZone);

Current Time Zone

TimeZones.getCurrent() returns the current (server) time zone determined by the sequence mentioned above.

The Request Interceptor

Like configuring a locale, you can prepare the time zone for the given session by the use of the request interceptor. Please refer to the Locale section for more information.

Version History

Version Date Content

Last Update : 2022/07/08

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.