Java Hour Format formatDate(Date date, String pattern)

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

Description

Formats the given date according to the specified pattern.

License

Open Source License

Parameter

Parameter Description
date The date to format.
pattern The pattern to use for formatting the date.

Exception

Parameter Description
IllegalArgumentException If the given date pattern is invalid.

Return

A formatted date string.

Declaration

public static String formatDate(Date date, String pattern) 

Method Source Code

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

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

public class Main {
    /**/*from   w w  w .  java  2 s . c  o m*/
     * Date format pattern used to parse HTTP date headers in RFC 1123 format.
     */
    public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
    private static final TimeZone GMT = TimeZone.getTimeZone("GMT");

    /**
     * Formats the given date according to the RFC 1123 pattern.
     *
     * @param date The date to format.
     * @return An RFC 1123 formatted date string.
     * @see #PATTERN_RFC1123
     */
    public static String formatDate(Date date) {
        return formatDate(date, PATTERN_RFC1123);
    }

    /**
     * Formats the given date according to the specified pattern.  The pattern
     * must conform to that used by the {@link SimpleDateFormat simple date
     * format} class.
     *
     * @param date    The date to format.
     * @param pattern The pattern to use for formatting the date.
     * @return A formatted date string.
     * @throws IllegalArgumentException If the given date pattern is invalid.
     * @see SimpleDateFormat
     */
    public static String formatDate(Date date, String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.US);
        formatter.setTimeZone(GMT);
        return formatter.format(date);
    }
}

Related

  1. formatDate(Date date, String format)
  2. formatDate(Date date, String format)
  3. formatDate(Date date, String format, Locale locale)
  4. formatDate(Date date, String patter)
  5. formatDate(Date date, String pattern)
  6. formatDate(Date date, String timeFormat, String timeZone)
  7. formatDate(Date date, StringBuffer buffer)
  8. formatDate(Date date, TimeZone tz)
  9. formatDate(Date dateString)