001    /**
002     * Copyright (c) 2000-2010 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 java.util.HashMap;
018    import java.util.Map;
019    import java.util.TimeZone;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class TimeZoneUtil {
025    
026            public static TimeZone getDefault() {
027                    return _instance._getDefault();
028            }
029    
030            public static TimeZone getTimeZone(String timeZoneId) {
031                    return _instance._getTimeZone(timeZoneId);
032            }
033    
034            public static void setDefault(String timeZoneId) {
035                    _instance._setDefault(timeZoneId);
036            }
037    
038            private TimeZoneUtil() {
039                    _timeZone = _getTimeZone(StringPool.UTC);
040            }
041    
042            private TimeZone _getDefault() {
043                    TimeZone timeZone = TimeZoneThreadLocal.getTimeZone();
044    
045                    if (timeZone != null) {
046                            return timeZone;
047                    }
048    
049                    return _timeZone;
050            }
051    
052            private TimeZone _getTimeZone(String timeZoneId) {
053                    TimeZone timeZone = _timeZones.get(timeZoneId);
054    
055                    if (timeZone == null) {
056                            timeZone = TimeZone.getTimeZone(timeZoneId);
057    
058                            _timeZones.put(timeZoneId, timeZone);
059                    }
060    
061                    return timeZone;
062            }
063    
064            private void _setDefault(String timeZoneId) {
065                    if (Validator.isNotNull(timeZoneId)) {
066                            _timeZone = TimeZone.getTimeZone(timeZoneId);
067                    }
068            }
069    
070            private static TimeZoneUtil _instance = new TimeZoneUtil();
071    
072            private TimeZone _timeZone;
073            private Map<String, TimeZone> _timeZones = new HashMap<String, TimeZone>();
074    
075    }