Java Date ISO Parse parseIso8601(String iso8601String)

Here you can find the source of parseIso8601(String iso8601String)

Description

parse Iso

License

Open Source License

Declaration

public static long parseIso8601(String iso8601String) throws ParseException 

Method Source Code

//package com.java2s;
/**//from w w w  .  j  a v a  2  s  .  c  o m
 * Copyright (c) 2012 Todoroo Inc
 *
 * See the file "LICENSE" for the full license governing this code.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static long parseIso8601(String iso8601String) throws ParseException {
        if (iso8601String == null)
            return 0;
        String formatString;
        if (isoStringHasTime(iso8601String)) { // Time exists
            iso8601String = iso8601String.replace("Z", "+00:00"); //$NON-NLS-1$ //$NON-NLS-2$
            try {
                iso8601String = iso8601String.substring(0, 22) + iso8601String.substring(23);
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
                throw new ParseException("Invalid ISO 8601 length for string " + iso8601String, 0); //$NON-NLS-1$
            }
            formatString = "yyyy-MM-dd'T'HH:mm:ssZ"; //$NON-NLS-1$
        } else {
            formatString = "yyyy-MM-dd"; //$NON-NLS-1$
        }

        Date result = new SimpleDateFormat(formatString).parse(iso8601String);
        return result.getTime();
    }

    public static boolean isoStringHasTime(String iso8601String) {
        return iso8601String.length() > 10;
    }
}

Related

  1. parseDateTime(String iso8061StrDateTime)
  2. parseFromIso8601(String s)
  3. parseISO(String aIsoString)
  4. parseISO(String input)
  5. parseISO(String strValue)
  6. parseIso8601(String rawstr)
  7. parseIso8601(String time)
  8. parseISO8601Date(final String dateString)
  9. parseISO8601Date(String date)