Java TimeZone Get getTimezoneDifference(String timezone1, String timezone2)

Here you can find the source of getTimezoneDifference(String timezone1, String timezone2)

Description

get Timezone Difference

License

Open Source License

Declaration

public static int getTimezoneDifference(String timezone1, String timezone2) 

Method Source Code

//package com.java2s;
/**/*from  w w  w  .  j  a v  a  2  s .c o  m*/
 * Copyright (c) 2013 M. Alexander Nugent Consulting <i@alexnugent.name>
 *
 * M. Alexander Nugent Consulting Research License Agreement
 * Non-Commercial Academic Use Only
 *
 * This Software is proprietary. By installing, copying, or otherwise using this
 * Software, you agree to be bound by the terms of this license. If you do not agree,
 * do not install, copy, or use the Software. The Software is protected by copyright
 * and other intellectual property laws.
 *
 * You may use the Software for non-commercial academic purpose, subject to the following
 * restrictions. You may copy and use the Software for peer-review and methods verification
 * only. You may not create derivative works of the Software. You may not use or distribute
 * the Software or any derivative works in any form for commercial or non-commercial purposes.
 *
 * Violators will be prosecuted to the full extent of the law.
 *
 * All rights reserved. No warranty, explicit or implicit, provided.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARR?ANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Main {
    public static int getTimezoneDifference(String timezone1, String timezone2) {

        // Timezone1
        Calendar lCalendar = new GregorianCalendar(TimeZone.getTimeZone(timezone1));
        lCalendar.setTimeInMillis(new Date().getTime());
        int Timezone1HourOfDay = lCalendar.get(Calendar.HOUR_OF_DAY);
        int Timezone1DayOfMonth = lCalendar.get(Calendar.DAY_OF_MONTH);

        // Timezone2
        lCalendar = new GregorianCalendar(TimeZone.getTimeZone(timezone2));
        lCalendar.setTimeInMillis(new Date().getTime());
        int TimezoneHourOfDay = lCalendar.get(Calendar.HOUR_OF_DAY);
        int TimezoneDayOfMonth = lCalendar.get(Calendar.DAY_OF_MONTH);

        int hourDifference = Timezone1HourOfDay - TimezoneHourOfDay;
        int dayDifference = Timezone1DayOfMonth - TimezoneDayOfMonth;
        if (dayDifference != 0) {
            hourDifference = hourDifference + 24;
        }
        return hourDifference;
    }
}

Related

  1. getTimeTry(boolean lenient, TimeZone tz, int year, int month, int day, int hour, int minute, int second, int millis)
  2. getTimeWithTimeZone(final String timezoneId)
  3. getTimeZone(Calendar cal)
  4. getTimeZone(final String ID)
  5. getTimeZoneByOffSet(String offSetInString)
  6. getTimeZoneID()
  7. getTimeZoneIDs()
  8. getTimeZoneOffset()
  9. getTimeZoneOffset(long t)