Java TimeZone 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;
// Lesser General Public License as published by the Free Software Foundation.

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

import java.util.TimeZone;

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 ww w  . j  ava  2 s .  c  o 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. getDateAtTimeZone(Date when, TimeZone from, TimeZone to)
  2. getDateAttributes(long millisec, TimeZone tz, boolean showNumber)
  3. getDateBoxValue(TimeZone zone, Date date)
  4. getDateValue(TimeZone zone, Long dateBoxValue, Long timeBoxValue)