Java tutorial
/** * Copyright (c) 2005-2010 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id: ConvertUtils.java 1211 2010-09-10 16:20:45Z calvinxiu $ */ package com.dosport.system.utils; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.List; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.beanutils.converters.DateConverter; import org.apache.commons.lang.StringUtils; public class ConvertUtils { static { registerDateConverter(); } /** * ????(getter), ??List. * * @param collection * ???. * @param propertyName * ??????. */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static List convertElementPropertyToList(final Collection collection, final String propertyName) { List list = new ArrayList(); try { for (Object obj : collection) { list.add(PropertyUtils.getProperty(obj, propertyName)); } } catch (Exception e) { throw ReflectionUtils.convertReflectionExceptionToUnchecked(e); } return list; } /** * ????(getter), ??. * * @param collection * ???. * @param propertyName * ??????. * @param separator * . */ @SuppressWarnings("rawtypes") public static String convertElementPropertyToString(final Collection collection, final String propertyName, final String separator) { List list = convertElementPropertyToList(collection, propertyName); return StringUtils.join(list, separator); } /** * ?. * * @param value * ?. * @param toType * ?. */ public static Object convertStringToObject(String value, Class<?> toType) { try { return org.apache.commons.beanutils.ConvertUtils.convert(value, toType); } catch (Exception e) { throw ReflectionUtils.convertReflectionExceptionToUnchecked(e); } } /** * 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); } }