Example usage for org.apache.commons.beanutils.converters DateConverter setPatterns

List of usage examples for org.apache.commons.beanutils.converters DateConverter setPatterns

Introduction

In this page you can find the example usage for org.apache.commons.beanutils.converters DateConverter setPatterns.

Prototype

public void setPatterns(String[] patterns) 

Source Link

Document

Set the date format patterns to use to convert dates to/from a java.lang.String.

Usage

From source file:com.github.dactiv.common.utils.ConvertUtils.java

/**
 * ?,??yyyy-MM-dd//from w  w  w  .  j  a  va  2s. c  o m
 * 
 * @param patterns ?
 */
public static void registerDateConverter(String... patterns) {
    DateConverter dc = new DateConverter();
    dc.setUseLocaleFormat(true);
    dc.setPatterns(patterns);
    register(dc, Date.class);
}

From source file:cn.hxh.springside.mapper.ObjectMapper.java

/**
 * Apache BeanUtilsConverter?,??,','//  ww  w  .j a v a  2  s  .c  om
 */
public static void registerDateConverter(String patterns) {
    DateConverter dc = new DateConverter();
    dc.setUseLocaleFormat(true);
    dc.setPatterns(StringUtils.split(patterns, ","));
    ConvertUtils.register(dc, Date.class);
}

From source file:com.common.util.mapper.ConvertUtils.java

/**
 * Apache BeanUtilsConverter?,??,','/*from   w  ww .j a  va2  s. c o  m*/
 */
public static void registerDateConverter(String patterns) {
    DateConverter dc = new DateConverter();
    dc.setUseLocaleFormat(true);
    dc.setPatterns(StringUtils.split(patterns, ","));
    org.apache.commons.beanutils.ConvertUtils.register(dc, Date.class);
}

From source file:cn.newtouch.util.hibernate.ConvertUtils.java

/**
 * Converter?: yyyy-MM-dd  yyyy-MM-dd HH:mm:ss
 *//* w  ww  .j  a  v a 2  s.c o m*/
private static void registerDateConverter() {
    DateConverter dc = new DateConverter();
    dc.setUseLocaleFormat(true);
    dc.setPatterns(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" });
    org.apache.commons.beanutils.ConvertUtils.register(dc, Date.class);
}

From source file:com.xyz.util.ReflectionUtil.java

/**
 * ?clazzproperty.//from   www  . j  a  va 2  s . co  m
 * 
 * @param value ?
 * @param clazz ???Class
 * @param propertyName ???Class.
 */
public static Object convertValue(Object value, Class<?> toType) {
    try {
        DateConverter dc = new DateConverter();
        dc.setUseLocaleFormat(true);
        dc.setPatterns(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" });
        ConvertUtils.register(dc, Date.class);
        return ConvertUtils.convert(value.toString(), toType);
    } catch (Exception e) {
        throw convertToUncheckedException(e);
    }
}

From source file:ReflectionUtils.java

/**
 * clazzproperty.//from w ww  .  j  a  v a  2  s.c om
 * 
 * @param value
 *            
 * @param clazz
 *            Class
 * @param propertyName
 *            Class.
 */
public static Object convertValue(Object value, Class<?> toType) {
    try {
        DateConverter dc = new DateConverter();
        dc.setUseLocaleFormat(true);
        dc.setPatterns(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" });
        ConvertUtils.register(dc, Date.class);
        return ConvertUtils.convert(value, toType);
    } catch (Exception e) {
        throw convertToUncheckedException(e);
    }
}

From source file:com.xyz.util.ReflectionUtil.java

/**
 * ?clazzproperty.//  www  . j  a  va 2 s. co m
 * 
 * @param value ?
 * @param clazz ???Class
 * @param propertyName ???Class.
 */
public static Object convertValue(Object value, Class<?> clazz, String propertyName) {
    try {
        Class<?> toType = BeanUtils.getPropertyDescriptor(clazz, propertyName).getPropertyType();
        DateConverter dc = new DateConverter();
        dc.setUseLocaleFormat(true);
        dc.setPatterns(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" });
        ConvertUtils.register(dc, Date.class);
        //String???''
        if (toType.equals(String.class))
            value = "'" + value + "'";
        return ConvertUtils.convert(value.toString(), toType);

    } catch (Exception e) {
        throw convertToUncheckedException(e);
    }
}

From source file:com.abssh.util.ReflectionUtils.java

/**
 * ??/*from w  w  w  . j  ava 2 s . c  o  m*/
 * 
 * @param value
 *            ?
 * @param toType
 *            
 */
public static Object convertValue(Object value, Class<?> toType) {
    if (value == null) {
        return null;
    }
    if (value.equals("") && toType.getName().equals("java.util.Date")) {
        return null;
    }
    try {
        DateConverter dc = new DateConverter();
        dc.setUseLocaleFormat(true);
        dc.setPatterns(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" });
        ConvertUtils.register(dc, Date.class);
        return ConvertUtils.convert(value, toType);
    } catch (Exception e) {
        throw convertReflectionExceptionToUnchecked(e);
    }
}

From source file:com.hihframework.core.utils.ReflectUtil.java

/**
 * ?clazzproperty./*w  ww  . ja va2  s  . c o m*/
 * 
 * @param value ?
 * @param clazz ???Class
 * @param propertyName ???Class.
 */
public static Object convertValue(Object value, Class<?> toType) {
    try {
        DateConverter dc = new DateConverter();
        dc.setUseLocaleFormat(true);
        dc.setPatterns(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" });
        ConvertUtils.register(dc, Date.class);
        return ConvertUtils.convert(value, toType);
    } catch (Exception e) {
        throw convertReflectionExceptionToUnchecked(e);
    }
}

From source file:airlift.util.AirliftUtil.java

/**
 * Creates the bean utils bean./*from w ww  .  ja va 2  s . c  om*/
 *
 * @param _allowedDateTimePatterns the _allowed date time patterns
 * @param _timeZone the _time zone
 * @return BeansUtilsBean
 */
public static org.apache.commons.beanutils.BeanUtilsBean createBeanUtilsBean(String[] _allowedDateTimePatterns,
        java.util.TimeZone _timeZone) {
    org.apache.commons.beanutils.converters.SqlDateConverter sqlDateConverter = new org.apache.commons.beanutils.converters.SqlDateConverter();
    sqlDateConverter.setPatterns(_allowedDateTimePatterns);
    sqlDateConverter.setTimeZone(_timeZone);

    org.apache.commons.beanutils.converters.DateConverter dateConverter = new org.apache.commons.beanutils.converters.DateConverter();
    dateConverter.setPatterns(_allowedDateTimePatterns);
    dateConverter.setTimeZone(_timeZone);

    org.apache.commons.beanutils.converters.SqlTimestampConverter sqlTimestampConverter = new org.apache.commons.beanutils.converters.SqlTimestampConverter();
    sqlTimestampConverter.setPatterns(_allowedDateTimePatterns);
    sqlTimestampConverter.setTimeZone(_timeZone);

    //registering "" (empty string) as a true value to support checkboxes with
    //the value attribute not being set.  Setting the value
    //atrribute wil make the value visible on the form.  This may
    //not be desired for a simple yes-no option hence the need to
    //register "" as true.
    String[] trueStrings = { "yes", "y", "true", "on", "1", "" };
    String[] falseStrings = { "no", "n", "false", "off", "0" };
    org.apache.commons.beanutils.converters.BooleanConverter booleanConverter = new org.apache.commons.beanutils.converters.BooleanConverter(
            trueStrings, falseStrings, Boolean.FALSE);

    org.apache.commons.beanutils.ConvertUtilsBean convertUtilsBean = new org.apache.commons.beanutils.ConvertUtilsBean();
    convertUtilsBean.register(sqlDateConverter, java.sql.Date.class);
    convertUtilsBean.register(dateConverter, java.util.Date.class);
    convertUtilsBean.register(sqlTimestampConverter, java.sql.Timestamp.class);
    convertUtilsBean.register(booleanConverter, Boolean.class);
    convertUtilsBean.register(booleanConverter, Boolean.TYPE);

    return new org.apache.commons.beanutils.BeanUtilsBean(convertUtilsBean);
}