Java Date Format formatDate(Date date)

Here you can find the source of formatDate(Date date)

Description

Format the date into "yyyy-MM-dd" format.

License

Open Source License

Parameter

Parameter Description
date a parameter

Declaration

public static String formatDate(Date date) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final int FMT_DATE_YYYY_MM_DD = 1;
    public static final int FMT_DATE_YYYYMMDD = 2;
    public static final int FMT_DATE_YYMMDD = 3;
    public static final int FMT_DATE_YYYY = 4;
    public static final int FMT_DATE_YYMM = 5;
    public static final int FMT_DATE_YYYYMM = 6;
    public static final int FMT_DATE_YYYY_MM_DD_HH_MM_SS = 7;
    public static final int FMT_DATE_YYYYMMDDHHMM = 8;
    public static final int FMT_DATE_YYYYMMDDHHMMSS = 9;
    public static final int FMT_DATE_YYYY_MM_DD_HH_MM = 10;
    public static final int FMT_DATE_YY_MM_DD_HH_MM_SS = 11;
    public static final int FMT_DATE_YYYY_SLASH_MM = 12;

    /**//  w w  w . ja  v a  2  s .c o m
     * Format the date into "yyyy-MM-dd" format.
     * 
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        return formatDate(date, FMT_DATE_YYYY_MM_DD);
    }

    /**
     * Format date into a string 
     * 
     * @param date the date to be formatted
     * @param nFmt FMT_DATE_XXXX constant
     * @return the formatted string date.
     */
    public static String formatDate(Date date, int nFmt) {
        if (date == null) {
            return "";
        }

        SimpleDateFormat fmtDate = getDateFormat(nFmt);
        try {
            return fmtDate.format(date);
        } catch (Exception e) {
            return "";
        }
    }

    private static SimpleDateFormat getDateFormat(int nFmt) {
        SimpleDateFormat fmtDate;
        switch (nFmt) {
        case FMT_DATE_YYYY_MM_DD:
            fmtDate = new SimpleDateFormat("yyyy-MM-dd");
            break;
        case FMT_DATE_YYYYMMDD:
            fmtDate = new SimpleDateFormat("yyyyMMdd");
            break;
        case FMT_DATE_YYMMDD:
            fmtDate = new SimpleDateFormat("yyMMdd");
            break;
        case FMT_DATE_YYYY:
            fmtDate = new SimpleDateFormat("yyyy");
            break;
        case FMT_DATE_YYMM:
            fmtDate = new SimpleDateFormat("yyMM");
            break;
        case FMT_DATE_YYYYMM:
            fmtDate = new SimpleDateFormat("yyyyMM");
            break;
        case FMT_DATE_YYYY_MM_DD_HH_MM_SS:
            fmtDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            break;
        case FMT_DATE_YYYYMMDDHHMM:
            fmtDate = new SimpleDateFormat("yyyyMMddHHmm");
            break;
        case FMT_DATE_YYYYMMDDHHMMSS:
            fmtDate = new SimpleDateFormat("yyyyMMddHHmmss");
            break;
        case FMT_DATE_YYYY_MM_DD_HH_MM:
            fmtDate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            break;
        case FMT_DATE_YY_MM_DD_HH_MM_SS:
            fmtDate = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            break;
        case FMT_DATE_YYYY_SLASH_MM:
            fmtDate = new SimpleDateFormat("yyyy/MM");
            break;
        default:
            fmtDate = new SimpleDateFormat("yyyy-MM-dd");
            break;
        }
        return fmtDate;
    }
}

Related

  1. formatDate(Date date)
  2. formatDate(Date date)
  3. formatDate(Date date)
  4. formatDate(Date date)
  5. formatDate(Date date)
  6. formatDate(Date date)
  7. formatDate(Date date)
  8. formatDate(Date date)
  9. formatDate(Date date)