001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    import java.util.TimeZone;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     * @author Raymond Augé
026     */
027    public class TimeZoneUtil {
028    
029            public static TimeZone getDefault() {
030                    return getInstance()._getDefault();
031            }
032    
033            public static TimeZoneUtil getInstance() {
034                    PortalRuntimePermission.checkGetBeanProperty(TimeZoneUtil.class);
035    
036                    return _instance;
037            }
038    
039            public static TimeZone getTimeZone(String timeZoneId) {
040                    return getInstance()._getTimeZone(timeZoneId);
041            }
042    
043            public static void setDefault(String timeZoneId) {
044                    getInstance()._setDefault(timeZoneId);
045            }
046    
047            private TimeZoneUtil() {
048                    _timeZone = _getTimeZone(StringPool.UTC);
049            }
050    
051            private TimeZone _getDefault() {
052                    TimeZone timeZone = TimeZoneThreadLocal.getDefaultTimeZone();
053    
054                    if (timeZone != null) {
055                            return timeZone;
056                    }
057    
058                    return _timeZone;
059            }
060    
061            private TimeZone _getTimeZone(String timeZoneId) {
062                    TimeZone timeZone = _timeZones.get(timeZoneId);
063    
064                    if (timeZone == null) {
065                            timeZone = TimeZone.getTimeZone(timeZoneId);
066    
067                            _timeZones.put(timeZoneId, timeZone);
068                    }
069    
070                    return timeZone;
071            }
072    
073            private void _setDefault(String timeZoneId) {
074                    PortalRuntimePermission.checkSetBeanProperty(getClass());
075    
076                    if (Validator.isNotNull(timeZoneId)) {
077                            _timeZone = TimeZone.getTimeZone(timeZoneId);
078                    }
079            }
080    
081            private static TimeZoneUtil _instance = new TimeZoneUtil();
082    
083            private TimeZone _timeZone;
084            private Map<String, TimeZone> _timeZones = new HashMap<String, TimeZone>();
085    
086    }