    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.lang3.time;
    019
    020import java.time.ZoneId;
    021import java.util.TimeZone;
    022
    023import org.apache.commons.lang3.JavaVersion;
    024import org.apache.commons.lang3.ObjectUtils;
    025import org.apache.commons.lang3.SystemProperties;
    026import org.apache.commons.lang3.SystemUtils;
    027
    028/**
    029 * Helps dealing with {@link java.util.TimeZone}s.
    030 *
    031 * @since 3.7
    032 */
    033public class TimeZones {
    034
    035    /**
    036     * A public version of {@link java.util.TimeZone}'s package private {@code GMT_ID} field.
    037     */
    038    public static final String GMT_ID = "GMT";
    039
    040    /**
    041     * The GMT time zone.
    042     *
    043     * @since 3.13.0
    044     */
    045    public static final TimeZone GMT = TimeZones.getTimeZone(GMT_ID);
    046
    047    private static final boolean JAVA_25 = SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_25);
    048
    049    /**
    050     * Delegates to {@link TimeZone#getTimeZone(String)}, on Java 25 and up, maps an ID if it's a key in {@link ZoneId#SHORT_IDS}.
    051     * <p>
    052     * On Java 25, calling {@link TimeZone#getTimeZone(String)} with an ID in {@link ZoneId#SHORT_IDS} writes a message to {@link System#err} in the form:
    053     * </p>
    054     *
    055     * <pre>
    056     * WARNING: Use of the three-letter time zone ID "the-short-id" is deprecated and it will be removed in a future release
    057     * </pre>
    058     * <p>
    059     * You can disable mapping from {@link ZoneId#SHORT_IDS} by setting the system property {@code "TimeZones.mapShortIDs=false"}.
    060     * </p>
    061     *
    062     * @param id Same as {@link TimeZone#getTimeZone(String)}.
    063     * @return Same as {@link TimeZone#getTimeZone(String)}.
    064     * @since 3.20.0
    065     */
    066    public static TimeZone getTimeZone(final String id) {
    067        return TimeZone.getTimeZone(JAVA_25 && mapShortIDs() ? ZoneId.SHORT_IDS.getOrDefault(id, id) : id);
    068    }
    069
    070    private static boolean mapShortIDs() {
    071        return SystemProperties.getBoolean(TimeZones.class, "mapShortIDs", () -> true);
    072    }
    073
    074    /**
    075     * Returns the given TimeZone if non-{@code null}, otherwise {@link TimeZone#getDefault()}.
    076     *
    077     * @param timeZone a locale or {@code null}.
    078     * @return the given locale if non-{@code null}, otherwise {@link TimeZone#getDefault()}.
    079     * @since 3.13.0
    080     */
    081    public static TimeZone toTimeZone(final TimeZone timeZone) {
    082        return ObjectUtils.getIfNull(timeZone, TimeZone::getDefault);
    083    }
    084
    085    /** Do not instantiate. */
    086    private TimeZones() {
    087    }
    088}