Java Parse Date parseDate(String str, DateFormat df)

Here you can find the source of parseDate(String str, DateFormat df)

Description

Parses a presumptive date or date/time, using a supplied format pattern.

License

Apache License

Parameter

Parameter Description
str a String, possibly a date or date/time String.
df A date formatter.

Return

A date value, resulting from parsing the String using the supplied formatter. Returns null if the parsing attempt fails.

Declaration

public static Date parseDate(String str, DateFormat df) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

import java.util.Date;

public class Main {
    /**/*from ww  w.  ja  v a2  s .com*/
     * Parses a presumptive date or date/time, using a supplied format pattern.
     *
     * @param str      a String, possibly a date or date/time String.
     * @param pattern  A date or date/time pattern.
     *
     * @return         A date value, resulting from parsing the String using the
     *                 supplied pattern.  Returns null if the parsing attempt fails.
     */
    public static Date parseDate(String str, String pattern) {
        if (pattern == null || pattern.trim().isEmpty()) {
            return null;
        }
        if (str == null || str.trim().isEmpty()) {
            return null;
        }
        DateFormat df = null;
        Date date = null;
        try {
            df = new SimpleDateFormat(pattern);
            date = parseDate(str, df);
        } catch (IllegalArgumentException iae) {
            return null;
        }
        return date;
    }

    /**
     * Parses a presumptive date or date/time, using a supplied format pattern.
     *
     * @param str      a String, possibly a date or date/time String.
     * @param df       A date formatter.
     *
     * @return         A date value, resulting from parsing the String using the
     *                 supplied formatter.  Returns null if the parsing attempt fails.
     */
    public static Date parseDate(String str, DateFormat df) {
        if (df == null) {
            return null;
        }
        if (str == null || str.trim().isEmpty()) {
            return null;
        }
        Date date = null;
        try {
            df.setLenient(false);
            date = df.parse(str);
        } catch (ParseException pe) {
            return null;
        }
        return date;
    }
}

Related

  1. parseDate(String str)
  2. parseDate(String str)
  3. parseDate(String str)
  4. parseDate(String str)
  5. parseDate(String str, boolean strict, String... parsePatterns)
  6. parseDate(String str, Locale locale, String... parsePatterns)
  7. parseDate(String str, String pattern)
  8. parseDate(String str, String timezone)
  9. parseDate(String str, String[] parsePatterns)