Java Parse Date parseDate(final String str, final Locale locale, final String... parsePatterns)

Here you can find the source of parseDate(final String str, final Locale locale, final String... parsePatterns)

Description

parse Date

License

Open Source License

Declaration

public static Date parseDate(final String str, final Locale locale, final String... parsePatterns)
            throws ParseException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

import java.util.Date;
import java.util.Locale;

public class Main {
    public static Date parseDate(final String str, final String... parsePatterns) throws ParseException {
        return parseDate(str, null, parsePatterns);
    }/*  ww  w  . ja va2 s  .c  o  m*/

    public static Date parseDate(final String str, final Locale locale, final String... parsePatterns)
            throws ParseException {
        return parseDateWithLeniency(str, locale, parsePatterns, true);
    }

    private static Date parseDateWithLeniency(final String str, final Locale locale, final String[] parsePatterns,
            final boolean lenient) throws ParseException {
        if (str == null || parsePatterns == null) {
            throw new IllegalArgumentException("Date and Patterns must not be null");
        }

        SimpleDateFormat parser;
        if (locale == null) {
            parser = new SimpleDateFormat();
        } else {
            parser = new SimpleDateFormat("", locale);
        }

        parser.setLenient(lenient);
        final ParsePosition pos = new ParsePosition(0);
        for (final String parsePattern : parsePatterns) {

            String pattern = parsePattern;

            // LANG-530 - need to make sure 'ZZ' output doesn't get passed to SimpleDateFormat
            if (parsePattern.endsWith("ZZ")) {
                pattern = pattern.substring(0, pattern.length() - 1);
            }

            parser.applyPattern(pattern);
            pos.setIndex(0);

            String str2 = str;
            // LANG-530 - need to make sure 'ZZ' output doesn't hit SimpleDateFormat as it will ParseException
            if (parsePattern.endsWith("ZZ")) {
                str2 = str.replaceAll("([-+][0-9][0-9]):([0-9][0-9])$", "$1$2");
            }

            final Date date = parser.parse(str2, pos);
            if (date != null && pos.getIndex() == str2.length()) {
                return date;
            }
        }
        throw new ParseException("Unable to parse the date: " + str, -1);
    }
}

Related

  1. parseDate(final String pDate)
  2. parseDate(final String revision)
  3. parseDate(final String source)
  4. parseDate(final String source)
  5. parseDate(final String str)
  6. parseDate(final String value)
  7. parseDate(Long date, String format)
  8. parseDate(Map obj, String field, String format)
  9. parseDate(Object o)