Java SQL Date to String toString(java.sql.Date sqlDate, String format)

Here you can find the source of toString(java.sql.Date sqlDate, String format)

Description

converts a java.sql.Date string with appropriate format

License

BSD License

Parameter

Parameter Description
sqlDate java.sql.Date
format specifying the output format for the return string

Return

formatted string

Declaration

public static String toString(java.sql.Date sqlDate, String format) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright Duke Comprehensive Cancer Center and SemanticBits
 * //from  w w  w . j a  v  a  2  s .  com
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/c3pr/LICENSE.txt for details.
 ******************************************************************************/

import java.sql.Time;

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static final String DISPLAY_DATE_FORMAT = "MM/dd/yyyy";
    public static final String DISPLAY_DATE_TIME_FORMAT = "Day mm/dd/yyyy HH:MM:SS EDT";
    public static final String JDBC_DATE_ESCAPE_FORMAT = "yyyy-MM-dd";
    private static final String DISPLAY_DATE_DELIMITER = "/";
    private static final String ZONE = "EDT";
    private static String[] nameOfDay = { "0 no day", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

    /**
     * converts a java.sql.Date string with appropriate format
     * 
     * @param sqlDate
     *                java.sql.Date
     * @param format
     *                specifying the output format for the return string
     * @return formatted string
     */
    public static String toString(java.sql.Date sqlDate, String format) {
        // convert Date to String of the form "mm/dd/yyyy"
        if (DISPLAY_DATE_FORMAT.equalsIgnoreCase(format)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sqlDate);
            return getDateAsString(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE),
                    calendar.get(Calendar.YEAR));
        }
        // convert Date to String of the form "mm/dd/yyyy HH:MM"
        else if (DISPLAY_DATE_TIME_FORMAT.equals(format)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sqlDate);

            return getDateTimeStringFromCalendar(calendar);
        }
        // convert Date to String of the form "yyyy-mm-dd"
        else if (JDBC_DATE_ESCAPE_FORMAT.equalsIgnoreCase(format)) {
            return sqlDate.toString();
        }
        // default util.date to string convertion
        else {
            java.util.Date defaultDate = new java.util.Date(sqlDate.getTime());
            return defaultDate.toString();
        }
    }

    /**
     * converts a java.util.Date string with appropriate format
     * 
     * @param utilDate
     *                java.util.Date
     * @param format
     *                specifying the output format for the return string
     * @return formatted string if utilDate is not null
     */
    public static String toString(java.util.Date utilDate, String format) {
        if (utilDate == null) {
            return "";
        }

        // convert Date to String of the form "mm/dd/yyyy"
        if (DISPLAY_DATE_FORMAT.equalsIgnoreCase(format)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(utilDate);

            return getDateAsString(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE),
                    calendar.get(Calendar.YEAR));
        }
        // convert Date to String of the form "mm/dd/yyyy HH:MM"
        else if (DISPLAY_DATE_TIME_FORMAT.equals(format)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(utilDate);

            return getDateTimeStringFromCalendar(calendar);
        }
        // convert Date to String of the form "yyyy-mm-dd"
        else if (JDBC_DATE_ESCAPE_FORMAT.equalsIgnoreCase(format)) {
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            return sqlDate.toString();
        }
        // default util.date to string convertion
        else {
            return utilDate.toString();
        }
    }

    private static String getDateAsString(int month, int date, int year) {
        StringBuffer dateAsString = new StringBuffer();

        if (month > 9) {
            dateAsString.append(month);
        } else {
            dateAsString.append("0" + month);
        }
        dateAsString.append(DISPLAY_DATE_DELIMITER);
        if (date > 9) {
            dateAsString.append(date);
        } else {
            dateAsString.append("0" + date);
        }
        dateAsString.append(DISPLAY_DATE_DELIMITER).append(year);

        return dateAsString.toString();
    }

    private static String getDateTimeStringFromCalendar(Calendar calendar) {
        String dateString = nameOfDay[calendar.get(Calendar.DAY_OF_WEEK)] + " ";
        dateString += getDateAsString(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DATE),
                calendar.get(Calendar.YEAR));
        dateString += (" " + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE) + ":"
                + calendar.get(calendar.SECOND) + " " + ZONE);
        return dateString;
    }

    public static Time getTime(Date date) {
        return new Time(date.getTime());
    }
}

Related

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