Java SQL Date Get getDate(Object value, int columnType)

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

Description

get Date

License

Open Source License

Declaration

public static java.sql.Date getDate(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.Date getDate(Object value, int columnType) throws Exception {
        // check for null
        if (value == null) {
            return null;
        }/*from  w  ww  .  ja v  a2  s  . 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.DATE: {
            long sec = ((java.sql.Date) value).getTime();
            return new java.sql.Date(sec);
        }
        case java.sql.Types.TIMESTAMP: {
            long sec = ((java.sql.Timestamp) value).getTime();
            return new java.sql.Date(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.Date) (df.parse(value.toString())));
            } catch (ParseException ex) {
                throw new Exception("date conversion failed. (" + value.toString().trim() + "/" + columnType + ")");
            }
        }
        default: {
            throw new Exception("date conversion failed. (" + value.toString().trim() + "/" + columnType + ")");
        }
        }
    }

    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. getDate(final int year, final int month, final int day)
  2. getDate(final String value)
  3. getDate(Map m, String key)
  4. getDate(Object object)
  5. getDate(Object value)
  6. getDate(ResultSet aResultSet, String aColumnName)
  7. getDate(ResultSet res, String name)
  8. getDate(ResultSet resultSet, String columnName)
  9. getDate(ResultSet rs, int colNum)