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