Java SQL Date to String toString(Object date)

Here you can find the source of toString(Object date)

Description

Convert an Object to a String using Dates

License

Open Source License

Declaration

public static String toString(Object date) 

Method Source Code

//package com.java2s;

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");

    /**/*from  w  w  w.ja  v a  2 s . c o  m*/
     * 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());
    }
}

Related

  1. toString(Date date)
  2. toString(Date date, String dateFormat)
  3. toString(Date date, String format)
  4. toString(Date obj)
  5. toString(java.sql.Date sqlDate, String format)
  6. toString(Object object)
  7. toStringTH(Date date, String dateFormat)
  8. toStringValue(Date date)