Java Date Format formatDate(Date d, int type)

Here you can find the source of formatDate(Date d, int type)

Description

ACME date formatter

License

Open Source License

Parameter

Parameter Description
d date - if null uses current date

Return

formatted date

Declaration

public static String formatDate(Date d, int type) 

Method Source Code

//package com.java2s;
/*//w w w . j  av  a 2  s .co  m
The GUFF - The GNU Ultimate Framework Facility
Copyright (C) Simeosoft di Carlo Simeone
       
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
    
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   
 */

import java.util.*;
import java.text.*;

public class Main {
    public static final int FORMAT_YYYY_MM_DD_HH_MM_SS = 0;
    public static final int FORMAT_DD_MM_YYYY_HH_MM_SS = 1;
    public static final int FORMAT_DD_MM_YYYY_SLASH = 2;

    /**
     * ACME date formatter
     * @param d  date - if null uses current date
     * @return formatted date
     */
    public static String formatDate(Date d, int type) {
        DateFormat format = null;
        switch (type) {
        case FORMAT_DD_MM_YYYY_HH_MM_SS:
            format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            break;
        case FORMAT_YYYY_MM_DD_HH_MM_SS:
            format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            break;
        case FORMAT_DD_MM_YYYY_SLASH:
            format = new SimpleDateFormat("dd/MM/yyyy");
            break;
        }
        if (d == null) {
            d = new Date(System.currentTimeMillis());
        }
        return format.format(d);
    }
}

Related

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