Time Zone"

From Documentation
m
 
(15 intermediate revisions by 4 users not shown)
Line 4: Line 4:
  
 
=Overview=
 
=Overview=
The time zone used to process requests and events is, by default, determined by the JVM's default (by use of the <tt>getDefault</tt> method of <tt>java.util.TimeZone</tt>)<ref>Unlike locale, there is no way to determine the time zone for each browser</ref>.
+
The time zone used to process requests and events is, by default, determined by the JVM's default (i.e. [https://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html#getDefault() java.util.TimeZone.getDefault()]).
  
In this section, we'd like to discuss how to configure ZK to use a time zone other than JVM's default. For example, you might want to use the preferred time zone that a user specified in his or her profile.
+
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.
  
<blockquote>
+
=The Decision Sequence of Server Time Zone=
----
 
<references/>
 
</blockquote>
 
 
 
=The decision sequence of time zone=
 
  
 
The time zone is decided in the following sequence.
 
The time zone is decided in the following sequence.
  
# It checks if an attribute called <code>org.zkoss.web.preferred.timeZone</code> defined in the HTTP session (or <javadoc type="interface">org.zkoss.zk.ui.Session</javadoc>). If so, use it.
+
# It checks if an attribute called <code>org.zkoss.web.preferred.timeZone</code> defined in the HTTP session (aka., <javadoc type="interface">org.zkoss.zk.ui.Session</javadoc>). If so, use it.
# It checks if an attribute called <code>org.zkoss.web.preferred.timeZone</code> defined in the Servlet context (or <javadoc type="interface">org.zkoss.zk.ui.Application</javadoc>). If so, use it.
+
# It checks if an attribute called <code>org.zkoss.web.preferred.timeZone</code> defined in the Servlet context (aka., <javadoc type="interface">org.zkoss.zk.ui.Application</javadoc>). If so, use it.
 
# It checks if a property called <code>org.zkoss.web.preferred.timeZone</code> defined in the library property (i.e., <javadoc>org.zkoss.lang.Library</javadoc>). If so, use it.
 
# It checks if a property called <code>org.zkoss.web.preferred.timeZone</code> defined in the library property (i.e., <javadoc>org.zkoss.lang.Library</javadoc>). If so, use it.
 
# If none of them is found, JVM's default will be used.
 
# If none of them is found, JVM's default will be used.
 +
#: You can enforce the time zone with JVM option: <code>-Duser.timezone="Asia/Taipei"</code>
  
 
With this sequence in mind, you could configure ZK to use the correct time zone based on the application requirements.
 
With this sequence in mind, you could configure ZK to use the correct time zone based on the application requirements.
  
==Application-level time zone==
+
==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 <tt>WEB-INF/zk.xml</tt>:
+
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 <code>WEB-INF/zk.xml</code>:
  
 
<source lang="xml">
 
<source lang="xml">
Line 35: Line 31:
 
</source>
 
</source>
  
where the value can be anything accepted by the <code>getTimeZone</code> method of <code>java.util.TimeZone</code>
+
* Line 3: the value can be anything accepted by <code>java.util.TimeZone.getTimeZone()</code>
  
Alternatively, if you prefer to specify it in Java, you could invoke <javadoc method="setProperty(java.lang.String, java.lang.String)">org.zkoss.lang.Library</javadoc>. Furthermore, to avoid typo, you could use <javadoc method="PREFERRED_TIME_ZONE">org.zkoss.web.Attributes</javadoc> as follows.
+
Alternatively, if you prefer to specify it in Java, you can invoke <javadoc method="setProperty(java.lang.String, java.lang.String)">org.zkoss.lang.Library</javadoc>. Furthermore, to avoid typos, you could use <javadoc method="PREFERRED_TIME_ZONE">org.zkoss.web.Attributes</javadoc> as follows.
  
 
<source lang="java">
 
<source lang="java">
Line 43: Line 39:
 
</source>
 
</source>
  
== Per-user time zone ==
+
== 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.
+
Because ZK will check if a session attribute is 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.
 
For example, you can do this when a user logins.
Line 58: Line 54:
 
  }
 
  }
 
</source>
 
</source>
 +
 +
= Current Time Zone=
 +
[https://www.zkoss.org/javadoc/latest/zk/org/zkoss/util/TimeZones.html#getCurrent-- TimeZones.getCurrent()] returns the current (server) time zone determined by the sequence mentioned above.
 +
 +
= Browser Time Zone =
 +
 +
Run the js code below in your browser's developer tool's Console, you will get a time zone like <code>America/New_York</code>:
 +
 +
<syntaxhighlight lang="javascript">
 +
Intl.DateTimeFormat().resolvedOptions().timeZone
 +
</syntaxhighlight>
 +
Or you can check a browser's time zone setting.
 +
 +
When there is a time zone difference between the server and the browser, you need to handle the difference. See [[ZK_Developer%27s_Reference/Internationalization/Time_Zone/Handling_Server_and_User_Time_Zones]]
  
 
= The Request Interceptor =
 
= The Request Interceptor =
  
Like configuring locale, you can prepare the time zone for the given session by use of the request interceptor. Please refer to [[http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/Internationalization/Locale#The_Request_Interceptor Locale]] for more information.
+
Like configuring a locale, you can prepare the time zone for the given session by the use of the request interceptor. Please refer to [[ZK_Developer's_Reference/Internationalization/Locale#The_Request_Interceptor|the Locale section]] for more information.
  
=Version History=
 
  
{| border='1px' | width="100%"
 
! Version !! Date !! Content
 
|-
 
| &nbsp;
 
| &nbsp;
 
| &nbsp;
 
|}
 
  
 
{{ZKDevelopersReferencePageFooter}}
 
{{ZKDevelopersReferencePageFooter}}

Latest revision as of 01:46, 14 August 2024

Overview

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 Server 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.
    You can enforce the time zone with JVM option: -Duser.timezone="Asia/Taipei"

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:

<library-property>
    <name>org.zkoss.web.preferred.timeZone</name>
    <value>GMT-8</value>
</library-property>
  • 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 typos, 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 is 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.

Browser Time Zone

Run the js code below in your browser's developer tool's Console, you will get a time zone like America/New_York:

Intl.DateTimeFormat().resolvedOptions().timeZone

Or you can check a browser's time zone setting.

When there is a time zone difference between the server and the browser, you need to handle the difference. See ZK_Developer's_Reference/Internationalization/Time_Zone/Handling_Server_and_User_Time_Zones

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.




Last Update : 2024/08/14

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