Java Hour Format formatDate(final Date date, String pattern)

Here you can find the source of formatDate(final Date date, String pattern)

Description

format date by given pattern

License

Apache License

Declaration

public static String formatDate(final Date date, String pattern) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_PATTERN = "yyyy-MM-dd";

    /**//from ww  w.j a va  2 s  .c o m
     * format date by given pattern
     */
    public static String formatDate(final Date date, String pattern) {
        if (date == null) {
            return null;
        }
        if (pattern == null) {
            pattern = DEFAULT_PATTERN;
        }
        final SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }

    public static String formatDate(final Date date) {
        if (null == date) {
            return "";
        }
        final SimpleDateFormat formatter = new SimpleDateFormat(DATE_PATTERN);
        return formatter.format(date);
    }
}

Related

  1. formatDate(Date date, StringBuffer buffer)
  2. formatDate(Date date, TimeZone tz)
  3. formatDate(Date dateString)
  4. formatDate(Date src, String formatPattern)
  5. formatDate(final Date date, final String pattern)
  6. formatDate(final String date)
  7. formatDate(java.util.Date date, String pattern)
  8. formatDate(java.util.Date dtFormat, String sFormatString)
  9. formatDate(Object obj, String format)