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 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    }