Java Timestamp Create getTimestamp(Object value, int columnType)

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

Description

get Timestamp

License

Open Source License

Declaration

public static java.sql.Timestamp getTimestamp(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.Timestamp getTimestamp(Object value, int columnType) throws Exception {
        // check for null
        if (value == null) {
            return null;
        }//from w w w  .  j  a v  a 2  s . c  om

        /*
         * 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.TIMESTAMP: {
            return (java.sql.Timestamp) value;
        }
        case java.sql.Types.TIME: {
            long sec = ((java.sql.Time) value).getTime();
            return new java.sql.Timestamp(sec);
        }
        case java.sql.Types.DATE: {
            long sec = ((java.sql.Date) value).getTime();
            return new java.sql.Timestamp(sec);
        }
        case java.sql.Types.CHAR:
        case java.sql.Types.VARCHAR:
        case java.sql.Types.LONGVARCHAR: {
            try {
                DateFormat df = DateFormat.getDateInstance();
                return ((java.sql.Timestamp) (df.parse(value.toString())));
            } catch (ParseException ex) {
                throw new Exception(
                        "timestamp conversion failed. (" + value.toString().trim() + "/" + columnType + ")");
            }
        }
        default: {
            throw new Exception(
                    "timestamp 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. getTimestamp(int year, int month, int day, int hour, int min, int second)
  2. getTimestamp(java.util.Date utilDate)
  3. getTimestamp(long time)
  4. getTimestamp(Object o)
  5. getTimestamp(Object value)
  6. getTimestamp(ResultSet rs, String colName)
  7. getTimestamp(ResultSet rs, String strColName)
  8. getTimestamp(String date, String time)
  9. getTimestamp(String dateStr)