List of usage examples for org.apache.commons.lang3.time DateUtils parseDateStrictly
public static Date parseDateStrictly(final String str, final String... parsePatterns) throws ParseException
Parses a string representing a date by trying a variety of different parsers.
The parse will try each parse pattern in turn.
From source file:org.openestate.io.core.XmlUtils.java
private static Calendar parseDate(String value, boolean tryDateTimeOnError) { value = StringUtils.trimToNull(value); if (value == null) return null; try {/*from w w w . j a v a 2s. c o m*/ return DatatypeConverter.parseDate(value); } catch (IllegalArgumentException ex) { //LOGGER.warn( "Can't parse value '" + value + "' as date!" ); //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex ); } try { Date date = DateUtils.parseDateStrictly(value, new String[] { "dd.MM.yyyy", "dd.MM.yy", "dd/MM/yyyy", "dd/MM/yy", "dd-MM-yyyy", "dd-MMM-yyyy", "yyyy-MM-dd", "yyyy/MM/dd", "yyyy-D", "MM/yyyy", "MMM yyyy", "MMMMM yyyy", "yyyy" }); Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal; } catch (ParseException ex) { //LOGGER.warn( "Can't parse value '" + value + "' as date!" ); //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex ); } // try to parse the value as xsd:dateTime instead if (tryDateTimeOnError) { try { return XmlUtils.parseDateTime(value, false); } catch (IllegalArgumentException ex) { //LOGGER.warn( "Can't parse value '" + value + "' as datetime!" ); //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex ); } } throw new IllegalArgumentException("Can't parse date value '" + value + "'!"); }
From source file:org.openestate.io.core.XmlUtils.java
private static Calendar parseDateTime(String value, boolean tryDateOnError) { value = StringUtils.trimToNull(value); if (value == null) return null; try {//from ww w . j av a2 s . co m return DatatypeConverter.parseDateTime(value); } catch (IllegalArgumentException ex) { //LOGGER.warn( "Can't parse value '" + value + "' as datetime!" ); //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex ); } try { Date date = DateUtils.parseDateStrictly(value, new String[] { "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm" }); Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal; } catch (ParseException ex) { //LOGGER.warn( "Can't parse value '" + value + "' as datetime!" ); //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex ); } // try to parse the value as xsd:date instead if (tryDateOnError) { try { return XmlUtils.parseDate(value, false); } catch (IllegalArgumentException ex) { //LOGGER.warn( "Can't parse value '" + value + "' as datetime!" ); //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex ); } } throw new IllegalArgumentException("Can't parse date-time value '" + value + "'!"); }
From source file:org.openestate.io.trovit.TrovitUtils.java
public static Calendar parseTime(String value) { value = StringUtils.trimToNull(value); if (value == null) return null; try {//from ww w. j a v a2 s . c o m Date date = DateUtils.parseDateStrictly(value, new String[] { "HH:mm:ss", "HH:mm" }); Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal; } catch (ParseException ex) { //LOGGER.warn( "Can't parse value '" + value + "' as date!" ); //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex ); } throw new IllegalArgumentException("Can't parse time value '" + value + "'!"); }
From source file:org.openestate.io.wis_it.WisItUtils.java
public static Calendar parseDateTime(String value) { value = StringUtils.trimToNull(value); if (value == null) return null; try {//from w ww. ja v a 2 s.c om Date date = DateUtils.parseDateStrictly(value, new String[] { "yyyy-MM-dd mm:hh:ss", "yyyy-MM-dd mm:hh", "yyyy-MM-dd" }); Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal; } catch (ParseException ex) { //LOGGER.warn( "Can't parse value '" + value + "' as date-time!" ); //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex ); } // try to parse the value as xsd:dateTime instead try { return XmlUtils.parseDateTime(value); } catch (IllegalArgumentException ex) { //LOGGER.warn( "Can't parse value '" + value + "' as date-time!" ); //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex ); } throw new IllegalArgumentException("Can't parse date-time value '" + value + "'!"); }
From source file:org.openestate.is24.restapi.utils.XmlUtils.java
/** * Read a {@link Calendar} value from XML. * * @param value/*from w w w .java2 s . c o m*/ * XML string * * @return * parsed value */ public static Calendar parseDate(String value) { value = StringUtils.trimToNull(value); if (value == null) return null; try { return DatatypeConverter.parseDate(value); } catch (Exception ex) { //LOGGER.debug( "Invalid xsd:date value '" + value + "'!" ); try { Date date = DateUtils.parseDateStrictly(value, new String[] { "dd.MM.yyyy", "dd.MM.yy", "dd/MM/yyyy", "dd/MM/yy", "dd-MM-yyyy", "dd-MMM-yyyy", "yyyy-MM-dd", "yyyy/MM/dd", "yyyy-D", "MM/yyyy", "MMM yyyy", "MMMMM yyyy", "yyyy" }); Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal; } catch (Exception ex2) { throw new IllegalArgumentException("Can't parse xsd:date value '" + value + "'!", ex2); } } }
From source file:yoyo.actor.screen.iface.jsf.converter.DateConverter.java
/** * Create date.//from w ww . j a v a 2s . co m * @param aDateText date text * @return created date * @throws ParseException parse exception */ private static Date createDate(final String aDateText) throws ParseException { final Calendar cal = Calendar.getInstance(); final Date now = cal.getTime(); Date date = DateUtils.parseDateStrictly(aDateText, new String[] { FORMAL_PATTERN, "yyyyMMdd", "yyMMdd", "MMdd", "dd" }); if (aDateText.length() == LENGTH_MMDD) { date = DateUtils.setYears(date, cal.get(Calendar.YEAR)); if (date.before(now)) { date = DateUtils.addYears(date, 1); } } else if (aDateText.length() == LENGTH_DD) { date = DateUtils.setYears(date, cal.get(Calendar.YEAR)); date = DateUtils.setMonths(date, cal.get(Calendar.MONTH)); if (date.before(now)) { date = DateUtils.addMonths(date, 1); } } return date; }