Java Milliseconds getTimeZoneOffSetInMillisecond(String dateString)

Here you can find the source of getTimeZoneOffSetInMillisecond(String dateString)

Description

get Time Zone Off Set In Millisecond

License

Open Source License

Declaration

public static long getTimeZoneOffSetInMillisecond(String dateString) 

Method Source Code

//package com.java2s;

public class Main {
    private final static String DATE_SEPARATOR_COLON = ":";
    private final static String DATE_SEPARATOR_MINUS = "-";
    private final static String DATE_SEPARATOR_T = "T";
    private final static String DATE_SEPARATOR_PLUS = "+";
    private final static String DATE_SEPARATOR_GMT = "GMT";

    public static long getTimeZoneOffSetInMillisecond(String dateString) {
        String timeZoneOffSetInHour = getTimeZoneOffSetInHour(dateString);
        if (timeZoneOffSetInHour.length() == 5) {
            String flag = timeZoneOffSetInHour.substring(0, 1);
            Integer hour = Integer.parseInt(timeZoneOffSetInHour.substring(1, 3));
            Integer minute = Integer.parseInt(timeZoneOffSetInHour.substring(3, 5));
            if (DATE_SEPARATOR_MINUS.equals(flag)) {
                hour = -hour;/*  w  ww .  jav a2 s  .c  om*/
                minute = -minute;
            }
            return hour * 60 * 60 * 1000 + minute * 60 * 1000;
        }
        return 0;
    }

    public static String getTimeZoneOffSetInHour(String dateString) {
        if (dateString != null
                && (dateString.contains(DATE_SEPARATOR_T) || dateString.contains(DATE_SEPARATOR_GMT))) {
            String postFixLen6 = dateString.substring(dateString.length() - 6, dateString.length());
            String postFixLen5 = dateString.substring(dateString.length() - 5, dateString.length());
            String changedPostFix = changePostFixTimeZoneFormate(postFixLen6);
            if (changedPostFix.length() == 5) {
                return changedPostFix;
            } else if ((postFixLen5.contains(DATE_SEPARATOR_PLUS) || postFixLen5.contains(DATE_SEPARATOR_MINUS))
                    && !postFixLen5.contains(DATE_SEPARATOR_COLON)) {
                return postFixLen5;
            }
        }
        return "";
    }

    private static String changePostFixTimeZoneFormate(String dateString) {
        if (isEndWithTimeZoneOffSetInHourLen6(dateString)) {
            String postFix = dateString.substring(dateString.length() - 6, dateString.length());
            postFix = postFix.replace(DATE_SEPARATOR_COLON, "");
            dateString = dateString.substring(0, dateString.length() - 6) + postFix;
        }

        return dateString;
    }

    private static boolean isEndWithTimeZoneOffSetInHourLen6(String dateString) {
        String postFix = dateString.substring(dateString.length() - 6, dateString.length());
        if (postFix.length() == 6
                && (postFix.contains(DATE_SEPARATOR_PLUS) || postFix.contains(DATE_SEPARATOR_MINUS))
                && postFix.contains(DATE_SEPARATOR_COLON)) {
            return true;
        }
        return false;
    }
}

Related

  1. getNumSamplesForMillisAtSampleRate(final int sampleRate, final float millis)
  2. getOLEDateFromMillisRounded(long millis)
  3. getPeriodDurationInMillis(int period)
  4. GetTimeInMilliseconds(Date date)
  5. getTimeStringFromMilliseconds(long millis)
  6. getUnitLengthMillis(String unit)
  7. hasExpiredMillis(long now, long eventTime, long timeBuffer)
  8. hhmmssToMillis(String text)
  9. humanDateToMillisecondsWithoutException(String date)