Java Date ISO Parse parseISO8601Date(String dateStr)

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

Description

Parses a date that's in ISO8601 format.

License

Open Source License

Parameter

Parameter Description

Exception

Parameter Description
ParseException if there's a problem parsing the date
IllegalArgumentException if the date string is not recognized asan ISO8601 date

Return

the parsed date (converted to the local timezone)

Declaration

public static Calendar parseISO8601Date(String dateStr) throws ParseException 

Method Source Code


//package com.java2s;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import java.util.regex.Pattern;

public class Main {
    private static final Pattern ISO8601_UTC_TIME_BASIC_REGEX = Pattern.compile("\\d{8}T\\d{6}Z");
    private static final Pattern ISO8601_UTC_TIME_EXTENDED_REGEX = Pattern
            .compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z");
    private static final Pattern ISO8601_UTC_TIME_EXTENDED_DROID_REGEX = Pattern
            .compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{1,3}Z");
    private static final Pattern ISO8601_TIME_BASIC_REGEX = Pattern.compile("\\d{8}T\\d{6}[-\\+]\\d{4}");
    private static final Pattern ISO8601_TIME_EXTENDED_REGEX = Pattern
            .compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}[-\\+]\\d{2}:\\d{2}");
    private static final Pattern ISO8601_DATE_BASIC_REGEX = Pattern.compile("\\d{8}");
    private static final Pattern ISO8601_DATE_EXTENDED_REGEX = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");

    /**/*from   w w  w  .  j  av a2s . c  om*/
     * <p>
     * Parses a date that's in ISO8601 format.
     * </p>
     * 
     * @param - dateStr the date string (e.g. "2012-07-01T10:31:22+02:00")
     * @return the parsed date (converted to the local timezone)
     * @throws ParseException if there's a problem parsing the date
     * @throws IllegalArgumentException if the date string is not recognized as
     * an ISO8601 date
     */
    public static Calendar parseISO8601Date(String dateStr) throws ParseException {
        String format;
        if (ISO8601_DATE_BASIC_REGEX.matcher(dateStr).matches()) {
            //Example: 19960415
            format = "yyyyMMdd";
        } else if (ISO8601_DATE_EXTENDED_REGEX.matcher(dateStr).matches()) {
            //Example: 1996-04-15
            format = "yyyy-MM-dd";
        } else if (ISO8601_TIME_BASIC_REGEX.matcher(dateStr).matches()) {
            //Example: 19960415T231000-0600
            format = "yyyyMMdd'T'HHmmssZ";
        } else if (ISO8601_TIME_EXTENDED_REGEX.matcher(dateStr).matches()) {
            //Example: 1996-04-15T23:10:00-06:00

            //SimpleDateFormat doesn't recognize timezone offsets that have colons
            //so remove the colon from the timezone offset
            dateStr = dateStr.replaceAll("([-\\+]\\d{2}):(\\d{2})$", "$1$2");
            format = "yyyy-MM-dd'T'HH:mm:ssZ";
        } else if (ISO8601_UTC_TIME_BASIC_REGEX.matcher(dateStr).matches()) {
            //Example: 19960415T231000Z

            //SimpleDateFormat doesn't recognize "Z"
            dateStr = dateStr.replace("Z", "+0000");
            format = "yyyyMMdd'T'HHmmssZ";
        } else if (ISO8601_UTC_TIME_EXTENDED_REGEX.matcher(dateStr).matches()) {
            //Example: 1996-04-15T23:10:00Z

            //SimpleDateFormat doesn't recognize "Z"
            dateStr = dateStr.replace("Z", "+0000");
            format = "yyyy-MM-dd'T'HH:mm:ssZ";
        } else if (ISO8601_UTC_TIME_EXTENDED_DROID_REGEX.matcher(dateStr).matches()) {
            //Android export format may include 1 to 3 zeros before Z

            //SimpleDateFormat doesn't recognize "Z"
            dateStr = dateStr.replaceAll("\\.\\d{1,3}Z", "+0000");
            format = "yyyy-MM-dd'T'HH:mm:ssZ";
        } else {
            throw new IllegalArgumentException("Date string is not in a valid ISO-8601 format.");
        }

        DateFormat df = new SimpleDateFormat(format);
        Date date = df.parse(dateStr);

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c;
    }
}

Related

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