com.github.dactiv.common.utils.ConvertUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.github.dactiv.common.utils.ConvertUtils.java

Source

/**
 * Copyright (c) 2005-2012 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.github.dactiv.common.utils;

import java.util.Date;

import org.apache.commons.beanutils.converters.DateConverter;

/**
 * ?
 * 
 * @author calvin
 *
 */
public class ConvertUtils extends org.apache.commons.beanutils.ConvertUtils {

    static {
        registerDateConverter("yyyy-MM-dd");
    }

    /**
     * ?,??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);
    }

    /**
     * Apache BeanUtils?.
     * 
     * @param value ?.
     * @param toType ?.
     */
    public static Object convertToObject(String value, Class<?> toType) {
        try {
            return convert(value, toType);
        } catch (Exception e) {
            throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
        }
    }

    /**
     * ?.
     * 
     * @param values ?.
     * @param toType ?.
     */
    public static Object convertToObject(String[] values, Class<?> toType) {
        try {
            return convert(values, toType);
        } catch (Exception e) {
            throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
        }
    }
}