Java SQL Date to String toString(Date date, String format)

Here you can find the source of toString(Date date, String format)

Description

to String

License

Open Source License

Declaration

public static String toString(Date date, String format) 

Method Source Code


//package com.java2s;
import java.sql.Date;
import java.sql.Time;

import java.text.*;

public class Main {
    public static final String DEFAULT_DATE_YMD_FORMAT = "yyyy-MM-dd";
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    public static String toString(Date date) {
        return toString((java.util.Date) date);
    }//  w  ww . j  av a 2s  . c  o m

    public static String toString(java.util.Date date) {
        return toString(date, DEFAULT_DATE_YMD_FORMAT);
    }

    public static String toString(Date date, String format) {
        return toString((java.util.Date) date, format);
    }

    public static String toString(java.util.Date date, String format) {

        if (date == null) {
            return null;
        }

        if (format == null) {
            throw new IllegalArgumentException("The value of an argument is inaccurate.");
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(date);
    }

    public static String toString(Time time, String format) {

        if (time == null) {
            return null;
        }

        if (format == null) {
            throw new IllegalArgumentException("The value of an argument is inaccurate.");
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(time);
    }

    public static String toString(Time time) {
        return toString(time, DEFAULT_TIME_FORMAT);
    }
}

Related

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