Example usage for org.apache.commons.beanutils.locale.converters DateLocaleConverter convert

List of usage examples for org.apache.commons.beanutils.locale.converters DateLocaleConverter convert

Introduction

In this page you can find the example usage for org.apache.commons.beanutils.locale.converters DateLocaleConverter convert.

Prototype

public Object convert(Object value, String pattern) 

Source Link

Document

Convert the specified locale-sensitive input object into an output object.

Usage

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;
}