Java Parse Date Pattern YYYY parse(String text)

Here you can find the source of parse(String text)

Description

Parses a ISO8601-compliant date/time string.

License

Apache License

Parameter

Parameter Description
text the date/time string to be parsed

Return

a Calendar, or null if the input could not be parsed

Declaration

public static Calendar parse(String text) 

Method Source Code

//package com.java2s;
/**/*from   w  w  w  . j av a 2 s . co  m*/
 * Copyright 2015 Marco Scavuzzo
 * Contact: Marco Scavuzzo <marco.scavuzzo@polimi.it>
 *
 * 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.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    /**
     * specific ISO8601-compliant format string (without time zone information)
     */
    private static String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";

    /**
     * Parses a ISO8601-compliant date/time string.
     * @param text the date/time string to be parsed
     * @return a <code>Calendar</code>, or <code>null</code>
     * if the input could not be parsed
     */
    public static Calendar parse(String text) {
        // parse time zone designator (Z or +00:00 or -00:00)
        // and build time zone id (GMT or GMT+00:00 or GMT-00:00)
        String tzID = "GMT"; // Zulu, i.e. UTC/GMT (default)
        int tzPos = text.indexOf('Z');
        if (tzPos == -1) {
            // not Zulu, try +
            tzPos = text.indexOf('+');
            if (tzPos == -1) {
                // not +, try -, but remember it might be used within first
                // 8 charaters for separating year, month and day, yyyy-mm-dd
                tzPos = text.indexOf('-', 8);
            }
            if (tzPos == -1) {
                // no time zone specified, assume Zulu
            } else {
                // offset to UTC specified in the format +00:00/-00:00
                tzID += text.substring(tzPos);
                text = text.substring(0, tzPos);
            }
        } else {
            // Zulu, i.e. UTC/GMT
            text = text.substring(0, tzPos);
        }

        TimeZone tz = TimeZone.getTimeZone(tzID);
        SimpleDateFormat format = new SimpleDateFormat(ISO_FORMAT);
        format.setLenient(false);
        format.setTimeZone(tz);
        Date date = format.parse(text, new ParsePosition(0));
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance(tz);
        cal.setTime(date);
        return cal;
    }
}

Related

  1. parse(String strDate)
  2. parse(String strDate)
  3. parse(String string)
  4. parse(String string)
  5. parse(String stringData)
  6. parse(String text)
  7. parse(String time)
  8. parse(String time, String df)
  9. parse1(String DateString, String dateTimeSeparator)