Java Date Format formatDate(Date date)

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

Description

returns the date in String MMddyyyy format.

License

Apache License

Parameter

Parameter Description
date a parameter

Declaration

public static String formatDate(Date date) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 Miami-Dade County//from   w  w  w .  j  ava 2 s . c o m
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final String emptyField = "----";
    public static final String blankField = "";

    public static String formatDate(String strDate, boolean time, boolean csv) {
        SimpleDateFormat sdf = null;
        if (time)
            sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        else if (strDate.contains("/"))
            sdf = new SimpleDateFormat("MM/dd/yyyy");
        else if (strDate.contains(","))
            sdf = new SimpleDateFormat("MMM dd, yyyy");
        else
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if (!strDate.trim().isEmpty())
                return formatDate(sdf.parse(strDate), time, csv);
            else
                return blankField;
        } catch (ParseException e) {
            e.printStackTrace();
            return blankField;
        }
    }

    /**
     * returns the date in string format.
     * @param java.util.Date date: If null, then returns an empty string.
     * @param boolean time: If true, returns date timestamp and only date if false
     * @return
     */
    public static String formatDate(Date date, boolean time, boolean csv) {
        StringBuilder sb = new StringBuilder("");
        if (time) {
            if (csv)
                sb.append("MMM dd yyyy hh:mm a");
            else
                sb.append("MMM dd, yyyy hh:mm a");
        } else {
            if (csv)
                sb.append("MMM dd yyyy");
            else
                sb.append("MMM dd, yyyy");
        }
        SimpleDateFormat sdf = new SimpleDateFormat(sb.toString());
        if (date != null)
            return sdf.format(date);
        else
            return emptyField;
    }

    /**
     * returns the date in String MMddyyyy format. 
     * if date is null then returns an empty string.
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        SimpleDateFormat sdf = null;
        if (date != null) {
            sdf = new SimpleDateFormat("MMddyyyy");
            return sdf.format(date);
        } else
            return emptyField;
    }
}

Related

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