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

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

Description

Formats the given date according to the specified pattern.

License

Apache 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(final Date date, final String pattern) 

Method Source Code

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

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

import java.util.TimeZone;

public class Main {
    private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
    /**/*from   w w  w . jav a2  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";

    /**
     * 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(final 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(final Date date, final String pattern) {
        if (date == null)
            throw new IllegalArgumentException("date is null");
        if (pattern == null)
            throw new IllegalArgumentException("pattern is null");

        final SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.US);
        formatter.setTimeZone(GMT);
        return formatter.format(date);
    }
}

Related

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