public static void registerConverters() { ConvertUtils.register(new StringConverter(), String.class); //date ConvertUtils.register(new DateConverter(null),java.util.Date.class); ConvertUtils.register(new SqlDateConverter(null),java.sql.Date.class); ConvertUtils.register(new SqlTimeConverter(null),Time.class); ConvertUtils.register(new SqlTimestampConverter(null),Timestamp.class); //number ConvertUtils.register(new BooleanConverter(null), Boolean.class); ConvertUtils.register(new ShortConverter(null), Short.class); ConvertUtils.register(new IntegerConverter(null), Integer.class); ConvertUtils.register(new LongConverter(null), Long.class); ConvertUtils.register(new FloatConverter(null), Float.class); ConvertUtils.register(new DoubleConverter(null), Double.class); ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class); ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class); }
public static void registerConverters(ConvertUtilsBean convertUtils,String[] datePatterns) { convertUtils.register(new StringConverter(), String.class); //date convertUtils.register(ConvertRegisterHelper.setPatterns(new DateConverter(null),datePatterns),java.util.Date.class); convertUtils.register(ConvertRegisterHelper.setPatterns(new SqlDateConverter(null),datePatterns),java.sql.Date.class); convertUtils.register(ConvertRegisterHelper.setPatterns(new SqlTimeConverter(null),datePatterns),Time.class); convertUtils.register(ConvertRegisterHelper.setPatterns(new SqlTimestampConverter(null),datePatterns),Timestamp.class); //number convertUtils.register(new BooleanConverter(null), Boolean.class); convertUtils.register(new ShortConverter(null), Short.class); convertUtils.register(new IntegerConverter(null), Integer.class); convertUtils.register(new LongConverter(null), Long.class); convertUtils.register(new FloatConverter(null), Float.class); convertUtils.register(new DoubleConverter(null), Double.class); convertUtils.register(new BigDecimalConverter(null), BigDecimal.class); convertUtils.register(new BigIntegerConverter(null), BigInteger.class); }
/** * Register array converters. * * @param throwException <code>true</code> if the converters should * throw an exception when a conversion error occurs, otherwise <code> * <code>false</code> if a default value should be used. * @param defaultArraySize The size of the default array value for array converters * (N.B. This values is ignored if <code>throwException</code> is <code>true</code>). * Specifying a value less than zero causes a <code>null<code> value to be used for * the default. */ private void registerArrays(final boolean throwException, final int defaultArraySize) { // Primitives registerArrayConverter(Boolean.TYPE, new BooleanConverter(), throwException, defaultArraySize); registerArrayConverter(Byte.TYPE, new ByteConverter(), throwException, defaultArraySize); registerArrayConverter(Character.TYPE, new CharacterConverter(), throwException, defaultArraySize); registerArrayConverter(Double.TYPE, new DoubleConverter(), throwException, defaultArraySize); registerArrayConverter(Float.TYPE, new FloatConverter(), throwException, defaultArraySize); registerArrayConverter(Integer.TYPE, new IntegerConverter(), throwException, defaultArraySize); registerArrayConverter(Long.TYPE, new LongConverter(), throwException, defaultArraySize); registerArrayConverter(Short.TYPE, new ShortConverter(), throwException, defaultArraySize); // Standard registerArrayConverter(BigDecimal.class, new BigDecimalConverter(), throwException, defaultArraySize); registerArrayConverter(BigInteger.class, new BigIntegerConverter(), throwException, defaultArraySize); registerArrayConverter(Boolean.class, new BooleanConverter(), throwException, defaultArraySize); registerArrayConverter(Byte.class, new ByteConverter(), throwException, defaultArraySize); registerArrayConverter(Character.class, new CharacterConverter(), throwException, defaultArraySize); registerArrayConverter(Double.class, new DoubleConverter(), throwException, defaultArraySize); registerArrayConverter(Float.class, new FloatConverter(), throwException, defaultArraySize); registerArrayConverter(Integer.class, new IntegerConverter(), throwException, defaultArraySize); registerArrayConverter(Long.class, new LongConverter(), throwException, defaultArraySize); registerArrayConverter(Short.class, new ShortConverter(), throwException, defaultArraySize); registerArrayConverter(String.class, new StringConverter(), throwException, defaultArraySize); // Other registerArrayConverter(Class.class, new ClassConverter(), throwException, defaultArraySize); registerArrayConverter(java.util.Date.class, new DateConverter(), throwException, defaultArraySize); registerArrayConverter(Calendar.class, new DateConverter(), throwException, defaultArraySize); registerArrayConverter(File.class, new FileConverter(), throwException, defaultArraySize); registerArrayConverter(java.sql.Date.class, new SqlDateConverter(), throwException, defaultArraySize); registerArrayConverter(java.sql.Time.class, new SqlTimeConverter(), throwException, defaultArraySize); registerArrayConverter(Timestamp.class, new SqlTimestampConverter(), throwException, defaultArraySize); registerArrayConverter(URL.class, new URLConverter(), throwException, defaultArraySize); }
/** * register date converter. */ 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); }
public static <T> T copyProperties(Object srcObj, Class<T> toClass) { try { ConvertUtils.register(new DateConverter(null), Date.class); T instance = toClass.newInstance(); BeanUtils.copyProperties(instance, srcObj); return instance; } catch (Exception e) { throw new RuntimeException("拷贝对象属性值异常", e); } }
/** * 定义Apache BeanUtils日期Converter的格式,可注册多个格式,以','分隔 */ public static void registerDateConverter(String patterns) { DateConverter dc = new DateConverter(); dc.setUseLocaleFormat(true); dc.setPatterns(StringUtils.split(patterns, ",")); ConvertUtils.register(dc, Date.class); }
/** * 转换字符串类型到clazz的property类型的值. * * @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); } }
/** * Method for initializing required staff for commons-beanutils classes. */ @PostConstruct public void init() { DateTimeConverter dtConverter = new DateConverter(); dtConverter.setPattern(Constants.DEFAULT_DATEFORMAT); ConvertUtils.register(dtConverter, Date.class); }
public static <T> T copyProperties(Object srcObj, Class<T> toClass) { try { ConvertUtils.register(new DateConverter(null), java.util.Date.class); T instance = toClass.newInstance(); BeanUtils.copyProperties(instance,srcObj); return instance; } catch (Exception e) { throw new RuntimeException("拷贝对象属性值异常", e); } }
public static void copyEntityProperties(MainFrame mainFrame, AbstractEntity entity, AbstractEntity newEntity) { try { ConvertUtils.register(new DateConverter(null), Date.class); ConvertUtils.register(new SqlTimestampConverter(null), Timestamp.class); ConvertUtils.register(new NullConverter(), Integer.class); BeanUtils.copyProperties(newEntity, entity); } catch (IllegalAccessException | InvocationTargetException e) { System.err.println("EntityUtil.copyEntityProperties() Exception : " + e.getMessage()); } }
public static AbstractEntity cloneEntity(MainFrame mainFrame, AbstractEntity entity) { try { ConvertUtils.register(new DateConverter(null), Date.class); ConvertUtils.register(new SqlTimestampConverter(null), Timestamp.class); ConvertUtils.register(new NullConverter(), Integer.class); return (AbstractEntity) BeanUtils.cloneBean(entity); } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) { System.err.println("EntityUtil.cloneEntityProperties() Exception : " + e.getMessage()); } return null; }
/** * 定义日期Converter的格式: yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss */ 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); }
public BaseServiceWithValidationImpl() { DateTimeConverter dtConverter = new DateConverter(); // TODO This list of patterns will need to be database driven in the future // Also this kind of thing could be handled in the validation layer as well. Do the convert, validate the converted // Date Obj etc.... String[] patterns = { "MM/dd/yyyy", "M/d/yyyy", "M/dd/yyyy", "MM/d/yyyy", "yyyy-MM-dd", "yyyy-M-d", "yyyy-M-dd", "yyyy-MM-d", "yyyy-MM-dd HH:mm:ss.SSS" }; dtConverter.setPatterns(patterns); ConvertUtils.register(dtConverter, java.util.Date.class); }
private void loadDatePattern() { String pattern = config.getString(CFG_DATE_PATTERN); LOGGER.info("Setting date format to: {}", pattern); List<String> splits = Splitter.on(SIGN_SEMI_COLON).omitEmptyStrings().trimResults().splitToList(pattern); String[] patterns = splits.toArray(new String[splits.size()]); DateConverter converter = new DateConverter(); converter.setPatterns(patterns); ConvertUtils.register(converter, Date.class); }
/** * 定义Apache BeanUtils日期Converter的格式: yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss */ public 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); }
/** * 注册一个时间类型的转换器,当前默认的格式为:yyyy-MM-dd * * @param patterns 日期格式 */ public static void registerDateConverter(String... patterns) { DateConverter dc = new DateConverter(); dc.setUseLocaleFormat(true); dc.setPatterns(patterns); register(dc, Date.class); }
@Override public void processTuple(byte[] tuple) { if (tuple == null) { if (err.isConnected()) { err.emit(new KeyValPair<String, String>(null, "Blank/null tuple")); } errorTupleCount++; return; } String incomingString = new String(tuple); if (StringUtils.isBlank(incomingString)) { if (err.isConnected()) { err.emit(new KeyValPair<String, String>(incomingString, "Blank tuple")); } errorTupleCount++; return; } try { if (out.isConnected() && clazz != null) { Matcher matcher = pattern.matcher(incomingString); boolean patternMatched = false; Constructor<?> ctor = clazz.getConstructor(); Object object = ctor.newInstance(); if (matcher.find()) { for (int i = 0; i <= matcher.groupCount() - 1; i++) { if (delimitedParserSchema.getFields().get(i).getType() == DelimitedSchema.FieldType.DATE) { DateTimeConverter dtConverter = new DateConverter(); dtConverter.setPattern((String)delimitedParserSchema.getFields().get(i).getConstraints().get(DelimitedSchema.DATE_FORMAT)); ConvertUtils.register(dtConverter, Date.class); } BeanUtils.setProperty(object, delimitedParserSchema.getFields().get(i).getName(), matcher.group(i + 1)); } patternMatched = true; } if (!patternMatched) { throw new ConversionException("The incoming tuple do not match with the Regex pattern defined."); } out.emit(object); emittedObjectCount++; } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | InstantiationException | ConversionException e) { if (err.isConnected()) { err.emit(new KeyValPair<String, String>(incomingString, e.getMessage())); logger.debug("Regex Expression : {} Incoming tuple : {}", splitRegexPattern, incomingString); } errorTupleCount++; logger.error("Tuple could not be parsed. Reason {}", e.getMessage()); } }
public Date getDate(String key) { Object value = super.get(key); ConvertUtils.register(new DateConverter(null), java.util.Date.class); return (Date) ConvertUtils.convert(value, Date.class); }
public static void SetupDateConverters() { DateTimeConverter dtConverter = new DateConverter(null); dtConverter.setPatterns(new String[] {"yyyy-MM-dd", "yyyy-MM-dd hh:mm:ss"}); ConvertUtils.register(dtConverter, Date.class); }
/** * Register the converters for other types. * </p> * This method registers the following converters: * <ul> * <li><code>Class.class</code> - {@link ClassConverter}</li> * <li><code>java.util.Date.class</code> - {@link DateConverter}</li> * <li><code>java.util.Calendar.class</code> - {@link CalendarConverter}</li> * <li><code>File.class</code> - {@link FileConverter}</li> * <li><code>java.sql.Date.class</code> - {@link SqlDateConverter}</li> * <li><code>java.sql.Time.class</code> - {@link SqlTimeConverter}</li> * <li><code>java.sql.Timestamp.class</code> - {@link SqlTimestampConverter}</li> * <li><code>URL.class</code> - {@link URLConverter}</li> * </ul> * @param throwException <code>true</code> if the converters should * throw an exception when a conversion error occurs, otherwise <code> * <code>false</code> if a default value should be used. */ private void registerOther(final boolean throwException) { register(Class.class, throwException ? new ClassConverter() : new ClassConverter(null)); register(java.util.Date.class, throwException ? new DateConverter() : new DateConverter(null)); register(Calendar.class, throwException ? new CalendarConverter() : new CalendarConverter(null)); register(File.class, throwException ? new FileConverter() : new FileConverter(null)); register(java.sql.Date.class, throwException ? new SqlDateConverter() : new SqlDateConverter(null)); register(java.sql.Time.class, throwException ? new SqlTimeConverter() : new SqlTimeConverter(null)); register(Timestamp.class, throwException ? new SqlTimestampConverter() : new SqlTimestampConverter(null)); register(URL.class, throwException ? new URLConverter() : new URLConverter(null)); }