Java SQL Date Get getDate(Object value)

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

Description

Convert an Object to a Date, without an Exception

License

Open Source License

Declaration

public static java.sql.Date getDate(Object value) 

Method Source Code

//package com.java2s;
import java.util.*;
import java.text.*;

public class Main {
    public static DateFormat OUT_DATE_FORMAT = new SimpleDateFormat(
            "dd/MM/yyyy");
    public static DateFormat OUT_TIME_FORMAT = new SimpleDateFormat(
            "H:mm:ss");
    public static DateFormat OUT_DATETIME_FORMAT = new SimpleDateFormat(
            "d/M/yyyy H:mm:ss");
    public static DateFormat OUT_TIMESTAMP_FORMAT = new SimpleDateFormat(
            "d/M/yy H:mm:ss.SSS");
    public static DateFormat IN_DATE_FORMAT = new SimpleDateFormat("d/M/yy");
    public static DateFormat IN_TIME_FORMAT = new SimpleDateFormat(
            "H:mm:ss");
    public static Calendar calendar = new GregorianCalendar();

    /**/*from ww  w . ja v a 2s  .co  m*/
     * Convert an Object to a Date, without an Exception
     */
    public static java.sql.Date getDate(Object value) {
        try {
            return toDate(value);
        } catch (ParseException pe) {
            pe.printStackTrace();
            return null;
        }
    }

    public static int getDate(Date date) {
        calendar.setTime(date);
        return calendar.get(Calendar.DATE);
    }

    /**
     * Convert an Object to a Date.
     */
    public static java.sql.Date toDate(Object value) throws ParseException {
        if (value == null) {
            return null;
        }
        if (value instanceof java.sql.Date) {
            return (java.sql.Date) value;
        }
        if (value instanceof String) {
            if ("".equals((String) value)) {
                return null;
            }
            return new java.sql.Date(IN_DATE_FORMAT.parse((String) value)
                    .getTime());
        }

        return new java.sql.Date(IN_DATE_FORMAT.parse(value.toString())
                .getTime());
    }

    /**
     * Convert an Object to a Time, without an Exception
     */
    public static java.sql.Time getTime(Object value) {
        try {
            return toTime(value);
        } catch (ParseException pe) {
            pe.printStackTrace();
            return null;
        }
    }

    /**
     * Convert an Object to a String using Dates
     */
    public static String toString(Object date) {
        if (date == null) {
            return null;
        }

        if (java.sql.Timestamp.class.isAssignableFrom(date.getClass())) {
            return OUT_TIMESTAMP_FORMAT.format(date);
        }
        if (java.sql.Time.class.isAssignableFrom(date.getClass())) {
            return OUT_TIME_FORMAT.format(date);
        }
        if (java.sql.Date.class.isAssignableFrom(date.getClass())) {
            return OUT_DATE_FORMAT.format(date);
        }
        if (java.util.Date.class.isAssignableFrom(date.getClass())) {
            return OUT_DATETIME_FORMAT.format(date);
        }

        throw new IllegalArgumentException("Unsupported type "
                + date.getClass());
    }

    /**
     * Convert an Object to a Time.
     */
    public static java.sql.Time toTime(Object value) throws ParseException {
        if (value == null) {
            return null;
        }
        if (value instanceof java.sql.Time) {
            return (java.sql.Time) value;
        }
        if (value instanceof String) {
            if ("".equals((String) value)) {
                return null;
            }
            return new java.sql.Time(IN_TIME_FORMAT.parse((String) value)
                    .getTime());
        }

        return new java.sql.Time(IN_TIME_FORMAT.parse(value.toString())
                .getTime());
    }
}

Related

  1. getDate(Date value)
  2. getDate(final int year, final int month, final int day)
  3. getDate(final String value)
  4. getDate(Map m, String key)
  5. getDate(Object object)
  6. getDate(Object value, int columnType)
  7. getDate(ResultSet aResultSet, String aColumnName)
  8. getDate(ResultSet res, String name)
  9. getDate(ResultSet resultSet, String columnName)