Java SQL Time Create getTime(Object value, int columnType)

Here you can find the source of getTime(Object value, int columnType)

Description

get Time

License

Open Source License

Declaration

public static java.sql.Time getTime(Object value, int columnType) throws Exception 

Method Source Code

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

import java.text.*;

public class Main {
    public static java.sql.Time getTime(Object value, int columnType) throws Exception {
        // check for null
        if (value == null) {
            return null;
        }// w  w w.  ja va2s. c  o m

        /*
         * 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. getTime(long time)
  2. getTime(long time)
  3. getTime(long time, boolean allowNull)
  4. getTime(Map map, String attr)
  5. getTime(Object value)
  6. getTime(ResultSet resultSet, String columnName)
  7. getTime(ResultSet rs, String column)
  8. getTime(String time)
  9. getTimeAndDateStamp(String line)