Java SQL Date to String toString(Date obj)

Here you can find the source of toString(Date obj)

Description

to String

License

Apache License

Declaration

private static String toString(Date obj) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;

public class Main {
    public static String toString(Object obj) {
        if (obj == null) {
            return "";
        }//from  w w  w.  j  a  v a  2s  .  co  m
        if (obj.getClass().getName().equals("java.lang.String")) {
            return toString((String) obj).trim();
        }
        if (obj.getClass().getName().equals("java.lang.Integer")) {
            return toString((Integer) obj).trim();
        }
        if (obj.getClass().getName().equals("java.lang.Long")) {
            return toString((Long) obj).trim();
        }
        if (obj.getClass().getName().equals("java.sql.Date")) {
            return toString((Date) obj).trim();
        }
        if (obj.getClass().getName().equals("java.util.Date")) {
            return toString((java.util.Date) obj).trim();
        }
        if (obj.getClass().getName().equals("java.lang.Float")) {
            return toString((Float) obj).trim();
        }
        if (obj.getClass().getName().equals("java.sql.Timestamp")) {
            return toString((Timestamp) obj).trim();
        }
        if (obj.getClass().getName().equals("java.lang.Double")) {
            return toString((Double) obj).trim();
        }

        return obj.toString().trim();
    }

    public static String toString(int obj) {
        return String.valueOf(obj);
    }

    public static String toString(long obj) {
        return String.valueOf(obj);
    }

    public static String toString(double obj) {
        return String.valueOf(obj);
    }

    public static String toString(float obj) {
        return String.valueOf(obj);
    }

    public static String toString(boolean obj) {
        return String.valueOf(obj);
    }

    public static String toString(char obj) {
        return String.valueOf(obj);
    }

    private static String toString(String obj) {
        if (obj == null) {
            return "";
        }
        return obj;
    }

    private static String toString(Integer obj) {
        if (obj == null) {
            return "";
        }
        return obj.toString();
    }

    private static String toString(Long obj) {
        if (obj == null) {
            return "";
        }
        return obj.toString();
    }

    private static String toString(Date obj) {
        if (obj == null) {
            return "";
        }

        DateFormat dftime = new java.text.SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        String str = dftime.format(obj);
        if (str.indexOf("00:00:00") < 0) {
            return str;
        } else {
            DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
            return df.format(obj);
        }
    }

    private static String toString(Double obj) {
        if (obj == null) {
            return "";
        }
        // liuyl fix bug .old BigDecimal return like
        // 2.45459999999999999999999999999999999999999345345
        return obj.doubleValue() + "";
        // BigDecimal bd = new BigDecimal(obj);
        // return bd.toString();
    }

    private static String toString(Float obj) {
        if (obj == null) {
            return "";
        }
        return obj.toString();
    }

    private static String toString(Timestamp obj) {
        if (obj == null) {
            return "";
        }
        return obj.toString();
    }

    private static String toString(java.util.Date obj) {
        if (obj == null) {
            return "";
        }
        return getDateString(obj);
    }

    private static String getDateString(java.util.Date adate) {

        if (adate == null) {
            return "";
        }

        DateFormat dftime = new java.text.SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        String str = dftime.format(adate);
        if (str.indexOf("00:00:00") < 0) {
            return str;
        } else {
            DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
            return df.format(adate);
        }

    }
}

Related

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