Here you can find the source of formatDate(Date date, String pattern)
Parameter | Description |
---|---|
date | The date to format. |
pattern | The pattern to use for formatting the date. |
Parameter | Description |
---|---|
IllegalArgumentException | If the given date pattern is invalid. |
public static String formatDate(Date date, String pattern)
//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); } }