Java Date ISO Parse parseISO8601Date(String date)

Here you can find the source of parseISO8601Date(String date)

Description

Parses an ISO 8601 date and time value.

License

Apache License

Parameter

Parameter Description
date the date and time value as an ISO 8601 string

Return

the parsed date/time

Declaration

public static Date parseISO8601Date(String date) 

Method Source Code

//package com.java2s;
/*//from w w w. j a v a 2 s . c  o  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    private static final String ISO_8601_DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";

    /**
     * Parses an ISO 8601 date and time value.
     * @param date the date and time value as an ISO 8601 string
     * @return the parsed date/time
     */
    public static Date parseISO8601Date(String date) {
        final String errorMessage = "Invalid ISO 8601 date format: ";
        date = formatDateToParse(date, errorMessage);
        DateFormat dateFormat = new SimpleDateFormat(ISO_8601_DATE_PATTERN
                + "Z");
        try {
            return dateFormat.parse(date);
        } catch (ParseException ex) {
            throw new IllegalArgumentException(errorMessage + date);
        }
    }

    private static String formatDateToParse(String date, String errorMessage) {
        /* Remove the colon from the time zone difference (+08:00) so that it can be parsed
         * by the SimpleDateFormat string.
         */
        if (!date.contains("Z")) {
            int lastColonIndex = date.lastIndexOf(":");
            if (lastColonIndex < 0) {
                throw new IllegalArgumentException(errorMessage + date);
            }
            date = date.substring(0, lastColonIndex)
                    + date.substring(lastColonIndex + 1, date.length());
        } else {
            date = date.replace("Z", "+0000");
        }
        return date;
    }
}

Related

  1. parseISO(String strValue)
  2. parseIso8601(String iso8601String)
  3. parseIso8601(String rawstr)
  4. parseIso8601(String time)
  5. parseISO8601Date(final String dateString)
  6. parseISO8601Date(String dateStr)
  7. parseIso8601Date(String datestr)
  8. parseIso8601Date(String dateString)
  9. parseISO8601Date(String dateString)