Java Date Format formatDate(final Date date)

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

Description

Formats the given date according to the RFC 1123 pattern.

License

Apache License

Parameter

Parameter Description
date The date to format.

Return

An RFC 1123 formatted date string.

Declaration

public static String formatDate(final Date date) 

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");
    /**// w w w.j  a v  a  2s.co  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(final Date date)
  2. formatDate(final Date date)
  3. formatDate(final Date date)
  4. formatDate(final Date date)
  5. formatDate(final Date date)
  6. formatDate(final java.util.Date date)
  7. formatDate(final java.util.Date date, final Supplier formatSupplier)
  8. formatDate(int day, int month, int year)
  9. formatDate(Integer day, Integer month, Integer year)