Java Parse Date parseDate(String str, boolean strict, String... parsePatterns)

Here you can find the source of parseDate(String str, boolean strict, String... parsePatterns)

Description

Parse a date/time string with multiple patterns.

License

MIT License

Parameter

Parameter Description
str the date/time string to be parsed
strict when true, parsing is strict
parsePatterns date patterns

Return

a date parsed from the string

Declaration

public static Date parseDate(String str, boolean strict, String... parsePatterns) 

Method Source Code


//package com.java2s;
/*!// w ww .  j  ava  2s . c o  m
 * mifmi-commons4j
 * https://github.com/mifmi/mifmi-commons4j
 *
 * Copyright (c) 2015 mifmi.org and other contributors
 * Released under the MIT license
 * https://opensource.org/licenses/MIT
 */

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

import java.util.Date;

import java.util.Locale;

import java.util.TimeZone;

public class Main {
    /**
     * Parse a date/time string with multiple patterns.
     * Locale is used Locale.US.
     * TimeZone is used UTC.
     * 
     * @param str the date/time string to be parsed
     * @param strict when true, parsing is strict
     * @param parsePatterns date patterns
     * @return a date parsed from the string
     */
    public static Date parseDate(String str, boolean strict, String... parsePatterns) {
        return parseDate(str, strict, null, null, parsePatterns);
    }

    /**
     * Parse a date/time string with multiple patterns.
     * 
     * @param str the date/time string to be parsed
     * @param strict when true, parsing is strict
     * @param timeZone the TimeZone of date/time. Locale.US if argument is null
     * @param locale the Locale of date/time. UTC if argument is null
     * @param parsePatterns date patterns
     * @return a date parsed from the string
     */
    public static Date parseDate(String str, boolean strict, TimeZone timeZone, Locale locale,
            String... parsePatterns) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        if (parsePatterns == null || parsePatterns.length == 0) {
            throw new IllegalArgumentException();
        }

        if (locale == null) {
            locale = Locale.US;
        }
        if (timeZone == null) {
            timeZone = TimeZone.getTimeZone("UTC");
        }

        for (String parsePattern : parsePatterns) {
            DateFormat dateFormat = new SimpleDateFormat(parsePattern, locale);
            dateFormat.setTimeZone(timeZone);
            dateFormat.setLenient(!strict);
            try {
                return dateFormat.parse(str);
            } catch (ParseException e) {
                continue;
            }
        }

        return null;
    }
}

Related

  1. parseDate(String str)
  2. parseDate(String str)
  3. parseDate(String str)
  4. parseDate(String str)
  5. parseDate(String str)
  6. parseDate(String str, DateFormat df)
  7. parseDate(String str, Locale locale, String... parsePatterns)
  8. parseDate(String str, String pattern)
  9. parseDate(String str, String timezone)