public void init(ActionServlet servlet, ModuleConfig config) { DateConverter converterDate = new DateConverter(); converterDate.setPattern("E MMM dd HH:mm:ss z yyyy"); ConvertUtils.register(converterDate, Date.class); ConvertUtils.register(new NestedWidgetsConverter(), NestedWidgets.class); }
static { DateConverter converter = new DateConverter(); String[] patterns = {"yyyy-MM-dd HH:mm:ss"}; converter.setPatterns(patterns); ConvertUtils.register(converter, Date.class); ConvertUtils.register(converter, String.class); }
private static ConvertUtilsBean createConverUtils() { DateConverter dateConverter = new DateConverter(); dateConverter.setPattern("yyyy-MM-dd"); ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean(); convertUtilsBean.register(dateConverter, java.util.Date.class); convertUtilsBean.register(dateConverter, Timestamp.class); convertUtilsBean.register(dateConverter, java.sql.Date.class); return convertUtilsBean; }
public static Object convertStringToObject(String 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); } }
/** * 转换字符串类型到 toType 类型, 或 toType 转为字符串 * * @param value: 待转换的字符串 * @param toType: 提供类型信息的 Class, 可以是基本数据类型的包装类或指定格式日期型 * @return */ public static <T> T convertValue(Object value, Class<T> 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 (T) ConvertUtils.convert(value, toType); } catch (Exception e) { e.printStackTrace(); throw convertToUncheckedException(e); } }
@SuppressWarnings("rawtypes") @Override protected Object convertToType(Class type, Object o) { String value = (String) o; if (isEmpty(value)) { return null; } Date date = (Date) dateConverter.convert(Date.class, o); return new LocalDate(date); }
/** * 定义Apache BeanUtils日期Converter的格式,可注册多个格式,以','分隔. * * @param patterns the patterns */ public static void registerDateConverter(String... patterns) { DateConverter dc = new DateConverter(); dc.setUseLocaleFormat(true); dc.setPatterns(patterns); ConvertUtils.register(dc, Date.class); }
static { ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean() { @Override public String convert(Object value) { if (value != null) { Class<?> type = value.getClass(); if (type.isEnum() && super.lookup(type) == null) { super.register(new EnumConverter(type), type); } else if (type.isArray() && type.getComponentType().isEnum()) { if (super.lookup(type) == null) { ArrayConverter arrayConverter = new ArrayConverter(type, new EnumConverter(type.getComponentType()), 0); arrayConverter.setOnlyFirstToString(false); super.register(arrayConverter, type); } Converter converter = super.lookup(type); return ((String) converter.convert(String.class, value)); } } return super.convert(value); } @SuppressWarnings("rawtypes") @Override public Object convert(String value, Class clazz) { if (clazz.isEnum() && super.lookup(clazz) == null) { super.register(new EnumConverter(clazz), clazz); } return super.convert(value, clazz); } @SuppressWarnings("rawtypes") @Override public Object convert(String[] values, Class clazz) { if (clazz.isArray() && clazz.getComponentType().isEnum() && super.lookup(clazz.getComponentType()) == null) { super.register(new EnumConverter(clazz.getComponentType()), clazz.getComponentType()); } return super.convert(values, clazz); } @SuppressWarnings("rawtypes") @Override public Object convert(Object value, Class targetType) { if (super.lookup(targetType) == null) { if (targetType.isEnum()) { super.register(new EnumConverter(targetType), targetType); } else if (targetType.isArray() && targetType.getComponentType().isEnum()) { ArrayConverter arrayConverter = new ArrayConverter( targetType, new EnumConverter(targetType.getComponentType()), 0); arrayConverter.setOnlyFirstToString(false); super.register(arrayConverter, targetType); } } return super.convert(value, targetType); } }; DateConverter dateConverter = new DateConverter(); dateConverter.setPatterns(CommonAttributes.DATE_PATTERNS); convertUtilsBean.register(dateConverter, Date.class); beanUtils = new BeanUtilsBean(convertUtilsBean); }
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); }
static { 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); }
public CommonsLocalDateConverter(String[] dateFormatStrings) { dateConverter = new DateConverter(); dateConverter.setPatterns(dateFormatStrings); }
public CommonsLocalDateConverter(String dateFormatString) { dateConverter = new DateConverter(); dateConverter.setPattern(dateFormatString); }
static { DateConverter dateConverter = new DateConverter(); dateConverter.setPattern(Application.DATE_FORMAT); ConvertUtils.register(dateConverter, Date.class); }