001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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 GMT;
030    
031            public static TimeZone getDefault() {
032                    return getInstance()._getDefault();
033            }
034    
035            public static TimeZoneUtil getInstance() {
036                    PortalRuntimePermission.checkGetBeanProperty(TimeZoneUtil.class);
037    
038                    return _instance;
039            }
040    
041            public static TimeZone getTimeZone(String timeZoneId) {
042                    return getInstance()._getTimeZone(timeZoneId);
043            }
044    
045            public static void setDefault(String timeZoneId) {
046                    getInstance()._setDefault(timeZoneId);
047            }
048    
049            private TimeZoneUtil() {
050                    _timeZone = _getTimeZone(StringPool.UTC);
051            }
052    
053            private TimeZone _getDefault() {
054                    TimeZone timeZone = TimeZoneThreadLocal.getDefaultTimeZone();
055    
056                    if (timeZone != null) {
057                            return timeZone;
058                    }
059    
060                    return _timeZone;
061            }
062    
063            private TimeZone _getTimeZone(String timeZoneId) {
064                    TimeZone timeZone = _timeZones.get(timeZoneId);
065    
066                    if (timeZone == null) {
067                            timeZone = TimeZone.getTimeZone(timeZoneId);
068    
069                            _timeZones.put(timeZoneId, timeZone);
070                    }
071    
072                    return timeZone;
073            }
074    
075            private void _setDefault(String timeZoneId) {
076                    PortalRuntimePermission.checkSetBeanProperty(getClass());
077    
078                    if (Validator.isNotNull(timeZoneId)) {
079                            _timeZone = TimeZone.getTimeZone(timeZoneId);
080                    }
081            }
082    
083            private static TimeZoneUtil _instance = new TimeZoneUtil();
084    
085            static {
086                    GMT = getTimeZone("GMT");
087            }
088    
089            private TimeZone _timeZone;
090            private Map<String, TimeZone> _timeZones = new HashMap<String, TimeZone>();
091    
092    }