Java SQL Type convertTemporal(Object value, int srcType, int destType)

Here you can find the source of convertTemporal(Object value, int srcType, int destType)

Description

convert Temporal

License

Open Source License

Declaration

public static Object convertTemporal(Object value, int srcType, int destType) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.sql.*;
import java.text.*;

public class Main {
    public static Object convertTemporal(Object value, int srcType, int destType) throws Exception {
        // is same source/destination then return value
        // * commented to allow cloneObject to allow clone creation
        // if (srcType == destType) {
        //     return value;
        // }/*from  ww w .j av a  2  s.  c  o m*/

        if (isNumeric(destType) == false && isString(destType) == false) {
            throw new Exception("temporal conversion failed. (" + value.toString().trim() + "/" + srcType + ":"
                    + destType + ")");
        }

        try {
            switch (destType) {
            case java.sql.Types.DATE:
                if (srcType == java.sql.Types.TIMESTAMP) {
                    return new java.sql.Date(((java.sql.Timestamp) value).getTime());
                } else {
                    return new java.sql.Date(((java.sql.Date) value).getTime());
                }
            case java.sql.Types.TIMESTAMP:
                if (srcType == java.sql.Types.TIME) {
                    return new Timestamp(((java.sql.Time) value).getTime());
                } else {
                    return new Timestamp(((java.sql.Date) value).getTime());
                }
            case java.sql.Types.TIME:
                if (srcType == java.sql.Types.TIMESTAMP) {
                    return new Time(((java.sql.Timestamp) value).getTime());
                } else {
                    return new java.sql.Time(((java.sql.Time) value).getTime());
                }
            case java.sql.Types.CHAR:
            case java.sql.Types.VARCHAR:
            case java.sql.Types.LONGVARCHAR:
            case java.sql.Types.NCHAR:
            case java.sql.Types.NVARCHAR:
            case java.sql.Types.LONGNVARCHAR:
                return new String(value.toString());
            default:
                throw new Exception("temporal conversion failed. (" + value.toString().trim() + "/" + srcType + ":"
                        + destType + ")");
            }
        } catch (NumberFormatException ex) {
            throw new Exception("temporal conversion failed. (" + value.toString().trim() + "/" + srcType + ":"
                    + destType + ")");
        }
    }

    public static boolean isNumeric(int dataType) {
        switch (dataType) {
        case java.sql.Types.NUMERIC:
        case java.sql.Types.DECIMAL:
        case java.sql.Types.BIT:
        case java.sql.Types.TINYINT:
        case java.sql.Types.SMALLINT:
        case java.sql.Types.INTEGER:
        case java.sql.Types.BIGINT:
        case java.sql.Types.REAL:
        case java.sql.Types.DOUBLE:
        case java.sql.Types.FLOAT:
            return true;
        default:
            return false;
        }
    }

    public static boolean isString(int dataType) {
        switch (dataType) {
        case java.sql.Types.CHAR:
        case java.sql.Types.VARCHAR:
        case java.sql.Types.LONGVARCHAR:
        case java.sql.Types.NCHAR:
        case java.sql.Types.NVARCHAR:
        case java.sql.Types.LONGNVARCHAR:
            return true;
        default:
            return false;
        }
    }

    public static java.sql.Time getTime(Object value, int columnType) throws Exception {
        // check for null
        if (value == null) {
            return null;
        }

        /*
         * The object coming back from the db could be a date, a timestamp, or 
         * a char field variety. If it's a date type return it, a timestamp
         * we turn into a long and then into a date, char strings we try to 
         * parse.
         */
        switch (columnType) {
        case java.sql.Types.TIME: {
            return (java.sql.Time) value;
        }
        case java.sql.Types.TIMESTAMP: {
            long sec = ((java.sql.Timestamp) value).getTime();
            return new java.sql.Time(sec);
        }
        case java.sql.Types.CHAR:
        case java.sql.Types.VARCHAR:
        case java.sql.Types.LONGVARCHAR:
        case java.sql.Types.NCHAR:
        case java.sql.Types.NVARCHAR:
        case java.sql.Types.LONGNVARCHAR: {
            try {
                DateFormat df = DateFormat.getDateInstance();
                return ((java.sql.Time) (df.parse(value.toString())));
            } catch (ParseException ex) {
                throw new Exception("time conversion failed. (" + value.toString().trim() + "/" + columnType + ")");
            }
        }
        default: {
            throw new Exception("time conversion failed. (" + value.toString().trim() + "/" + columnType + ")");
        }
        }
    }
}

Related

  1. convertNumeric(Object value, int srcType, int destType)
  2. convertOracleValue(Object value, int type)
  3. convertParam(Object oValue, Class cType, String sFieldName)
  4. convertSQLtype2JavaClassName(int type)
  5. convertStringToType(String type)
  6. convertToJDBCType(String talendType)
  7. convertToProperty(Object value)
  8. convertToStream(final Object o)
  9. convertToString(int datatype)