Java SQL Date parseStringValue(Class returnType, String value)

Here you can find the source of parseStringValue(Class returnType, String value)

Description

Tries to parse a String into the desired object.

License

Open Source License

Parameter

Parameter Description
T the returned object.
returnType The object type desired
value The String representation of the object

Return

an object representation of the string if successful. Otherwise will return a String

Declaration

@SuppressWarnings({ "unchecked", "deprecation" })
public static <T> T parseStringValue(Class<T> returnType, String value) 

Method Source Code

//package com.java2s;
/** //from  www .  j  a  va2s  . c  o m
 *  Copyright (C) 2012  Just Do One More
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    /**
     * Tries to parse a String into the desired object.
     * 
     * @param <T>
     *            the returned object.
     * @param returnType
     *            The object type desired
     * @param value
     *            The String representation of the object
     * @return an object representation of the string if successful. Otherwise
     *         will return a String
     */
    @SuppressWarnings({ "unchecked", "deprecation" })
    public static <T> T parseStringValue(Class<T> returnType, String value) {
        T objValue = null;

        if (returnType == null) {
            throw new IllegalArgumentException("Parameter (Class<T> returnType) is null.");
        }

        if ((!returnType.isPrimitive()) && value == null) {
            return null;
        }
        if (char[].class.equals(returnType)) {
            return (T) value.toCharArray();
        }
        if (java.util.Date.class.equals(returnType)) {
            try {
                objValue = (T) new Date(Date.parse(value));
            } catch (IllegalArgumentException e) {
                // try to parse with java.sql.Date parser
                try {
                    objValue = (T) new Date(java.sql.Date.valueOf(value).getTime());
                } catch (RuntimeException e1) {
                    // finally, try to parse with set format
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd H:mm:ss");
                    try {
                        objValue = (T) sdf.parse(value);
                    } catch (ParseException e2) {
                        System.out.println("Unable to parse date with format = " + value);
                    }
                }
            }
        } else {
            // objValue = (T) ConvertUtils.convert(value, returnType);
        }

        return objValue;
    }
}

Related

  1. isSimpleColumnType(Class columnType)
  2. isSimpleType(Object obj)
  3. issue20()
  4. now()
  5. nowPlus(int parts, int value)
  6. serialize(Instant sourceValue)
  7. stringToMillis(String str)
  8. toAge(String birthDay)
  9. today()