001    /**
002     * Copyright (c) 2000-2010 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 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    }