示例#1
0
  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);
  }
示例#2
0
 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);
   }
 }
示例#3
0
  /**
   * 转换字符串类型到 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);
    }
  }
示例#4
0
 /**
  * 定义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);
 }
示例#5
0
  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);
  }
示例#6
0
 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);
 }
示例#7
0
 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);
 }