Java TimeZone To createTimeZoneFromDouble(double timeZoneOffsetInHours)

Here you can find the source of createTimeZoneFromDouble(double timeZoneOffsetInHours)

Description

create Time Zone From Double

License

Open Source License

Declaration

public static TimeZone createTimeZoneFromDouble(double timeZoneOffsetInHours) 

Method Source Code

//package com.java2s;
/* MOD_V2.0//from   w w  w  .  j av  a  2  s . com
 * Copyright (c) 2012 OpenDA Association
 * All rights reserved.
 *
 * This file is part of OpenDA.
 *
 * OpenDA is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * OpenDA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with OpenDA.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;

public class Main {
    private static final long hoursToMillis = 1000 * 60 * 60;
    private static final long minutesToMillis = 1000 * 60;

    public static TimeZone createTimeZoneFromDouble(double timeZoneOffsetInHours) {
        long offsetInMinutes = Math.round(timeZoneOffsetInHours * 60d);
        if (offsetInMinutes == 0) {
            return TimeZone.getTimeZone("GMT");
        }
        return TimeZone.getTimeZone(getTimeZoneString(offsetInMinutes * minutesToMillis));
    }

    /**
     * @param timeZoneOffsetMillis has to be between -12 and +12 hours and has to be rounded on minutes.
     * @return String timeZone.
     */
    public static String getTimeZoneString(long timeZoneOffsetMillis) {
        if (timeZoneOffsetMillis % minutesToMillis != 0) {
            throw new IllegalArgumentException("timeZoneOffsetMillis % minutesToMillis != 0");
        }

        if (Math.abs(timeZoneOffsetMillis) > 12 * hoursToMillis) {
            throw new IllegalArgumentException("Math.abs(timeZoneOffsetMillis) > 12 * hoursToMillis");
        }

        char sign = timeZoneOffsetMillis >= 0 ? '+' : '-';
        long unsignedTimeZoneOffsetMillis = Math.abs(timeZoneOffsetMillis);
        long minuteMillis = unsignedTimeZoneOffsetMillis % hoursToMillis;
        long hourMillis = unsignedTimeZoneOffsetMillis - minuteMillis;

        int minutes = (int) (minuteMillis / minutesToMillis);
        int hours = (int) (hourMillis / hoursToMillis);

        if (minutes == 0) {
            return "GMT" + sign + hours;
        }

        if (minutes <= 9) {
            return "GMT" + sign + hours + ":0" + minutes;
        } else {
            return "GMT" + sign + hours + ':' + minutes;
        }
    }
}

Related

  1. extractTimeZone(String strdate, Date thedate)
  2. parseTimeZoneString(String timeZoneString)
  3. setTimeZone(String name)
  4. timeZoneToCode(TimeZone tz)