Here you can find the source of parseDate(String str, DateFormat df)
Parameter | Description |
---|---|
str | a String, possibly a date or date/time String. |
df | A date formatter. |
public static Date parseDate(String str, DateFormat df)
//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; } }