Java Calendar Calculate calcTimezone(Calendar cal)

Here you can find the source of calcTimezone(Calendar cal)

Description

calc Timezone

License

Apache License

Declaration

private static String calcTimezone(Calendar cal) 

Method Source Code

//package com.java2s;
/*/*from w  ww  .  ja v a 2s  .c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Calendar;
import java.util.Date;

import java.util.TimeZone;

public class Main {
    private static String calcTimezone(Calendar cal) {
        Date date = cal.getTime();
        TimeZone z = cal.getTimeZone();
        int tz = z.getRawOffset();

        if (z.inDaylightTime(date)) {
            int tzDst = z.getDSTSavings();
            tz = tz + tzDst;
        }

        String sign = "+";
        if (tz < 0) {
            sign = "-";
            tz = -tz;
        }

        int tzH = tz / (60 * 60 * 1000); // Integer divide towards zero.
        int tzM = (tz - tzH * 60 * 60 * 1000) / (60 * 1000);

        String tzH_str = Integer.toString(tzH);
        String tzM_str = Integer.toString(tzM);

        if (tzH < 10)
            tzH_str = "0" + tzH_str;
        if (tzM < 10)
            tzM_str = "0" + tzM_str;
        return sign + tzH_str + ":" + tzM_str;
    }
}

Related

  1. cal2Str(Calendar pCal)
  2. calcAge(Calendar dateOfBirth, Calendar now)
  3. calcDifferenceAsDays(Calendar aBaseDate, Calendar aTargetDate)
  4. calcDifferenceAsYearsPrivate(Calendar aBaseDate, Calendar aTargetDate)
  5. calcTime(Date end, Date start, int calendarField)
  6. calculateDuration(Calendar para_c1, Calendar para_c2)
  7. calculateHourDiff(Calendar cal1, Calendar cal2)
  8. calculateMinimalDaysInFirstWeek(Calendar calendar)
  9. calculateMonthsInBetween(Calendar startPoint, Calendar endPoint)