Java Date Format formatDate(Date d)

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

Description

format Date

License

BSD License

Declaration

public static String formatDate(Date d) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright SemanticBits, Northwestern University and Akaza Research
 * /*from   w ww .  ja  v a2  s.com*/
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/caaers/LICENSE.txt for details.
 ******************************************************************************/

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static final String DATE_PATTERN = "MM/dd/yyyy";
    public static final String DATE_WITH_DATETIME = "MM/dd/yyyy HH:mm";

    public static String formatDate(Date d) {
        if (d.getHours() > 0 || d.getMinutes() > 0)
            return formatDate(d, DATE_WITH_DATETIME);
        return formatDate(d, DATE_PATTERN);
    }

    public static String formatDate(Date d, String pattern) {
        return formatDate(d, pattern, TimeZone.getDefault());
    }

    public static String formatDate(Date d, String pattern, TimeZone tz) {
        //BJ: date formats are not thread safe.
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        df.setTimeZone(tz);
        return df.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 d)
  7. formatDate(Date d)
  8. formatDate(Date d)
  9. formatDate(Date d, int type)