List of usage examples for org.apache.commons.beanutils.locale.converters DateLocaleConverter convert
public Object convert(Object value, String pattern)
From source file:org.modelibra.util.Transformer.java
/** * Transforms a date text with a pattern into a Date object, if the text is * date.//from ww w. j a v a 2 s . c om * * @param date * date text * @param datePattern * date pattern * @return Date object if date is valid, TypeRuntimeException otherwise */ public static Date date(String date, String datePattern) { try { Locale locale = Locale.getDefault(); DateLocaleConverter converter = new DateLocaleConverter(locale, datePattern); // It does not accept dates with months greater than 12 and days // greater than 31. // It accepts years greater than the current year. converter.convert(date, datePattern); SimpleDateFormat formatter = new SimpleDateFormat(datePattern, locale); return formatter.parse(date); } catch (ParseException e) { throw new TypeRuntimeException(e); } catch (ConversionException e) { throw new TypeRuntimeException(e); } }
From source file:org.modelibra.util.Validator.java
/** * Validates a date./* ww w . j a v a 2s. c o m*/ * * @param date * date * @param datePattern * date pattern * @return <code>true</code> if date is valid. */ public static boolean date(String date, String datePattern) { boolean result = false; try { // String datePattern = "yyyy-MM-dd"; Locale locale = Locale.getDefault(); DateLocaleConverter converter = new DateLocaleConverter(locale, datePattern); // It does not accept dates with months greater than 12 and days // greater // than 31. // It accepts years greater than the current year. converter.convert(date, datePattern); result = true; } catch (RuntimeException e) { return false; } return result; }