Java Calendar Create getCalendarByValue(String timeZone)

Here you can find the source of getCalendarByValue(String timeZone)

Description

get Calendar By Value

License

Apache License

Declaration

public static Calendar getCalendarByValue(String timeZone) 

Method Source Code

//package com.java2s;
/*//from   ww w . j a  v  a  2  s  .  c o m
 * Copyright (C) 2014 Dell, Inc.
 * 
 * Licensed 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.TimeZone;

public class Main {
    public static Calendar getCalendarByValue(String timeZone) {

        timeZone = timeZone.trim();
        char ch = timeZone.charAt(0);
        if (ch == '+' || ch == '-') {
            timeZone = "GMT" + timeZone;
        } else if (Character.getType(ch) == Character.DECIMAL_DIGIT_NUMBER) {
            timeZone = "GMT+" + timeZone;
        }

        String prefix = timeZone.substring(0, 4);
        String val = timeZone.substring(4);
        switch (val.length()) {
        case 1:
            timeZone = prefix + "0" + val + ":00";
            break;
        case 2:
            timeZone = prefix + val + ":00";
            break;
        case 3:
            timeZone = prefix + "0" + val.substring(0, 1) + ":" + val.substring(1);
            break;
        case 4:
            if (val.charAt(1) == ':')
                timeZone = prefix + "0" + val;
            else {
                timeZone = prefix + val.substring(0, 2) + ":" + val.substring(2);
            }
            break;
        case 5:
            break;
        default:
            throw new IllegalArgumentException("Internal error: bad timezone(1)");

        }
        return getCalendarByName(timeZone);
    }

    public static Calendar getCalendarByName(String timeZone) {
        timeZone = timeZone.trim();
        TimeZone zone = TimeZone.getTimeZone(timeZone);
        String id = zone.getID();
        if (id.compareTo(timeZone) != 0) {
            throw new IllegalArgumentException("Bad timezone name: '" + timeZone + "'");
        }
        return Calendar.getInstance(zone);
    }

    public static TimeZone getTimeZone(String timeZone) {
        timeZone = timeZone.trim();
        TimeZone zone = TimeZone.getTimeZone(timeZone);
        String id = zone.getID();
        if (id.compareTo(timeZone) != 0) {
            throw new IllegalArgumentException("Bad timezone name: '" + timeZone + "'");
        }
        return zone;
    }
}

Related

  1. getCalendar(TimeZone timeZone, Locale locale)
  2. getCalendarAtMidnight(Date d)
  3. getCalendarBefore(int i)
  4. getCalendarByName(String timeZone)
  5. getCalendarBySpecific(int year, int month, int date, int hour)
  6. getCalendarDate()
  7. getCalendarDate(Calendar calendar)
  8. getCalendarDateFromToday(final int numDays)
  9. getCalendarDayString(Calendar date)