1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.util.HashMap;
18  import java.util.Map;
19  import java.util.TimeZone;
20  
21  /**
22   * <a href="TimeZoneUtil.java.html"><b><i>View Source</i></b></a>
23   *
24   * @author Brian Wing Shun Chan
25   */
26  public class TimeZoneUtil {
27  
28      public static TimeZone getDefault() {
29          return _instance._getDefault();
30      }
31  
32      public static TimeZone getTimeZone(String timeZoneId) {
33          return _instance._getTimeZone(timeZoneId);
34      }
35  
36      public static void setDefault(String timeZoneId) {
37          _instance._setDefault(timeZoneId);
38      }
39  
40      private TimeZoneUtil() {
41          _timeZone = _getTimeZone(StringPool.UTC);
42      }
43  
44      private TimeZone _getDefault() {
45          TimeZone timeZone = TimeZoneThreadLocal.getTimeZone();
46  
47          if (timeZone != null) {
48              return timeZone;
49          }
50  
51          return _timeZone;
52      }
53  
54      private TimeZone _getTimeZone(String timeZoneId) {
55          TimeZone timeZone = _timeZones.get(timeZoneId);
56  
57          if (timeZone == null) {
58              timeZone = TimeZone.getTimeZone(timeZoneId);
59  
60              _timeZones.put(timeZoneId, timeZone);
61          }
62  
63          return timeZone;
64      }
65  
66      private void _setDefault(String timeZoneId) {
67          if (Validator.isNotNull(timeZoneId)) {
68              _timeZone = TimeZone.getTimeZone(timeZoneId);
69          }
70      }
71  
72      private static TimeZoneUtil _instance = new TimeZoneUtil();
73  
74      private TimeZone _timeZone;
75      private Map<String, TimeZone> _timeZones = new HashMap<String, TimeZone>();
76  
77  }