Java Date ISO Parse parseISODate(String date)

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

Description

Returns a date parsed from a date/dateTime string formatted accorings to ISO 8601 rules.

License

BSD License

Parameter

Parameter Description
date the formatted date/dateTime string.

Exception

Parameter Description
ParseException if the date/dateTime string could not be parsed.

Return

the parsed date.

Declaration

public static Date parseISODate(String date) throws ParseException 

Method Source Code


//package com.java2s;
/*/*  w ww  . j a  v a  2  s .  c  o m*/
 * Copyright (c) 2012. betterFORM Project - http://www.betterform.de
 * Licensed under the terms of BSD License
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    /**
     * Returns a date parsed from a date/dateTime string formatted accorings to
     * ISO 8601 rules.
     *
     * @param date the formatted date/dateTime string.
     * @return the parsed date.
     * @throws ParseException if the date/dateTime string could not be parsed.
     */
    public static Date parseISODate(String date) throws ParseException {
        String pattern;
        StringBuffer buffer = new StringBuffer(date);

        switch (buffer.length()) {
        case 4:
            // Year: yyyy (eg 1997)
            pattern = "yyyy";
            break;
        case 7:
            // Year and month: yyyy-MM (eg 1997-07)
            pattern = "yyyy-MM";
            break;
        case 10:
            // Complete date: yyyy-MM-dd (eg 1997-07-16)
            pattern = "yyyy-MM-dd";
            break;
        default:
            // Complete date plus hours and minutes: yyyy-MM-ddTHH:mmTZD (eg 1997-07-16T19:20+01:00)
            // Complete date plus hours, minutes and seconds: yyyy-MM-ddTHH:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
            // Complete date plus hours, minutes, seconds and a decimal fraction of a second: yyyy-MM-ddTHH:mm:ss.STZD (eg 1997-07-16T19:20:30.45+01:00)
            pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

            if (buffer.length() == 16) {
                // add seconds
                buffer.append(":00");
            }
            if (buffer.length() > 16 && buffer.charAt(16) != ':') {
                // insert seconds
                buffer.insert(16, ":00");
            }
            if (buffer.length() == 19) {
                // add milliseconds
                buffer.append(".000");
            }
            if (buffer.length() > 19 && buffer.charAt(19) != '.') {
                // insert milliseconds
                buffer.insert(19, ".000");
            }
            if (buffer.length() == 23) {
                if (buffer.charAt(22) == 'Z') {
                    // replace 'Z' with '+0000'
                    buffer.replace(22, 23, "+0000");
                } else {
                    // append timzeone
                    buffer.append("+0000");
                }
            }
            if (buffer.length() == 24 && buffer.charAt(23) == 'Z') {
                // replace 'Z' with '+0000'
                buffer.replace(23, 24, "+0000");
            }
            if (buffer.length() == 29 && buffer.charAt(26) == ':') {
                // delete '.' from 'HH:mm'
                buffer.deleteCharAt(26);
            }
        }

        // always set time zone on formatter
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        final String dateFromBuffer = buffer.toString();
        if (dateFromBuffer.length() > 10) {
            format.setTimeZone(TimeZone.getTimeZone("GMT" + dateFromBuffer.substring(23)));
        }
        if (!format.format(format.parse(dateFromBuffer)).equals(dateFromBuffer)) {
            throw new ParseException("Not a valid ISO date", 0);
        }
        format.setTimeZone(TimeZone.getTimeZone("UTC"));

        return format.parse(buffer.toString());
    }
}

Related

  1. parseIso8601TimeStamp(String dateString)
  2. parseIsoDate(@Nonnull String asString)
  3. parseIsoDate(final String isoDateString)
  4. parseIsoDate(final String string)
  5. parseISODate(String date)
  6. parseIsoDate(String str)
  7. parseIsoDate2(String s)
  8. parseIsoDateAndTimeString(String aDateString, String aTimeString)
  9. parseIsoDateInput(String str)