/** * Converts a string to a date with the help of the given pattern. * If the string can't be converted an IllegalArgumentException will * be thrown. * * @param s e.g. "28-Nov-2009" * @param pattern e.g. "dd-MMM-yyyy" * @return a valid date or NULL_DATE (if an empty string is given) */ public static Date toDate(final String s, final String pattern) { return toDate(s, pattern, true); }
/** * Converts a string to a date with the help of the given pattern. If the * string can't be converted an IllegalArgumentException will be thrown. * * @param s * e.g. "28-Nov-2009" * @param pattern * e.g. "dd-MMM-yyyy" * @return a valid date or NULL_DATE (if an empty string is given) */ public static Date toDate(final String s, final String pattern) { return toDate(s, pattern, true); }
/** * Converts a string to a date by trying different date patterns. * If the string can't be converted an IllegalArgumentException will * be thrown. * * @param s e.g. "28-Nov-2009" or "Thu Nov 26 14:30:25 CET 2009" * @return a valid date or NULL_DATE (if an empty string is given) */ public static Date toDate(final String s) { try { return toDate(s, false); } catch (IllegalArgumentException iae) { return toDate(s, true); } }
/** * Converts a string to a date by trying different date patterns. If the * string can't be converted an IllegalArgumentException will be thrown. * * @param s * e.g. "28-Nov-2009" or "Thu Nov 26 14:30:25 CET 2009" * @return a valid date or NULL_DATE (if an empty string is given) */ public static Date toDate(final String s) { try { return toDate(s, false); } catch (IllegalArgumentException iae) { LOG.trace("Cannot convert '" + s + "' to Date with lenient=false:", iae); return toDate(s, true); } }
private static Date toDate(final String s, final boolean lenient) { if (StringUtils.isEmpty(s)) { return NULL_DATE; } String[] patterns = { "dd-MMM-yyyy", "dd-MM-yyyy", "yyyy-MMM-dd", "yyyy-MM-dd", "MMM-dd-yyyy", "dd MMM yyyy", "dd MM yyyy", "yyyy MMM dd", "yyyy MM dd", "MMM dd yyyy", "dd.MMM.yyyy", "dd.MM.yyyy", "yyyy.MMM.dd", "MMM.dd.yyyy" }; for (int i = 0; i < patterns.length; i++) { try { return toDate(s, patterns[i], lenient); } catch (IllegalArgumentException iae) { log.trace(iae.getMessage()); } } throw new IllegalArgumentException("can't convert \"" + s + "\" to date"); }
return toDate(s, patterns[i], lenient); } catch (IllegalArgumentException iae) { if (LOG.isTraceEnabled()) {
why = broken.value(); Date till = Converter.toDate(broken.till()); if (!NullConstants.NULL_DATE.equals(till) && till.after(today)) { LOG.info(method + "() SKIPPED till {} because {}.", broken.till(), why);