Java TimeZone To extractTimeZone(String strdate, Date thedate)

Here you can find the source of extractTimeZone(String strdate, Date thedate)

Description

extract Time Zone

License

Open Source License

Declaration

private static Date extractTimeZone(String strdate, Date thedate) 

Method Source Code

//package com.java2s;
// are made available under the terms of the Eclipse Public License v1.0

import java.util.*;

public class Main {
    private static Date extractTimeZone(String strdate, Date thedate) {
        // try to extract -06:00
        String tzSign = strdate.substring(strdate.length() - 6, strdate.length() - 5);
        String tzHour = strdate.substring(strdate.length() - 5, strdate.length() - 3);
        String tzMin = strdate.substring(strdate.length() - 2);
        if (tzSign.equals("-") || tzSign.equals("+")) {
            int h = Integer.parseInt(tzHour);
            int m = Integer.parseInt(tzMin);
            // NOTE: this is really plus, since perspective is from GMT
            if (tzSign.equals("+")) {
                h = -1 * h;//from  w  w  w. j a  v a 2  s. co m
                m = -1 * m;
            }
            Calendar cal = Calendar.getInstance();
            cal.setTime(thedate);
            cal.add(Calendar.HOUR_OF_DAY, h);
            cal.add(Calendar.MINUTE, m);
            // calculate according the used timezone
            cal.add(Calendar.MILLISECOND, localTimeDiff(cal.getTimeZone(), thedate));
            thedate = cal.getTime();
        }
        return thedate;
    }

    private static int localTimeDiff(TimeZone tz, Date date) {
        if (tz.inDaylightTime(date)) {
            int dstSavings = 0;
            if (tz.useDaylightTime()) {
                dstSavings = 3600000; // shortcut, JDK 1.4 allows cleaner impl
            }
            return tz.getRawOffset() + dstSavings;
        }
        return tz.getRawOffset();
    }
}

Related

  1. createTimeZoneFromDouble(double timeZoneOffsetInHours)
  2. parseTimeZoneString(String timeZoneString)
  3. setTimeZone(String name)
  4. timeZoneToCode(TimeZone tz)
  5. timeZoneToString(TimeZone tz)