Java Hour Format formatDateToString(java.util.Date date, int format)

Here you can find the source of formatDateToString(java.util.Date date, int format)

Description

Do format date with specified type.

License

Open Source License

Parameter

Parameter Description
date the date
format the format type

Return

the formatted date

Declaration

public final static String formatDateToString(java.util.Date date, int format) 

Method Source Code


//package com.java2s;

import java.text.SimpleDateFormat;

public class Main {
    private final static SimpleDateFormat[] DATE_FORMATTER = new SimpleDateFormat[] {
            new SimpleDateFormat("MMM d, yyy"), new SimpleDateFormat("EEE, MMM d, yyyy"),
            new SimpleDateFormat("MMM d, yyyy HH:mm:ss"), new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss"),
            new SimpleDateFormat("yyyy/MM/dd"), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") };

    /**/*from   ww w .j a  va2  s . c  om*/
     * Do format date with specified type.
     * 
     * @param date
     *            the date
     * @param format
     *            the format type
     * @return the formatted date
     */
    public final static String formatDateToString(java.util.Date date, int format) {
        String rel = null;
        if (date != null) {
            if (format >= DATE_FORMATTER.length || format < 0) {
                format = 0;
            }
            rel = DATE_FORMATTER[format].format(date);
        }
        return rel;
    }

    public final static String formatDateToString(java.util.Date date, String format) {
        String rel = null;
        if (date != null) {
            rel = new SimpleDateFormat(format).format(date);
        }
        return rel;
    }
}

Related

  1. formatDateTimeStringForEquivalentCommand(final Date date)
  2. formatDateToEndDateTime(String strDate)
  3. formatDateToHMSString(java.util.Date date)
  4. formatDateToHours(Date time)
  5. formatDateToStr(String format, Date date)
  6. formatDebugMessage(String debugLevel, Object msg)
  7. formatDicomTime(String dicomTime)
  8. formatDuration(long duration)
  9. formatDuration(long value)