Top Description Fields Constructors Methods
org.apache.commons.lang3.time

public Class DateFormatUtils

extends Object
Class Inheritance
Imports
java.util.Calendar, .Date, .Locale, .TimeZone

Date and time formatting utilities and constants.

Formatting is performed using the thread-safe org.apache.commons.lang3.time.FastDateFormat class.

Note that the JDK has a bug wherein calling Calendar.get(int) will override any previously called Calendar.clear() calls. See LANG-755.

Note that when using capital YYYY instead of lowercase yyyy, the formatter will assume current year as week year is not supported. See java.util.GregorianCalendar Week Year section for an explanation on the difference between calendar and week years.

Since
2.0

Field Summary

Modifier and TypeField and Description
public static final FastDateFormat
ISO_8601_EXTENDED_DATE_FORMAT

ISO 8601 formatter for date without time zone.

public static final FastDateFormat
ISO_8601_EXTENDED_DATETIME_FORMAT

ISO 8601 formatter for date-time without time zone.

public static final FastDateFormat
ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT

ISO 8601 formatter for date-time with time zone.

public static final FastDateFormat
ISO_8601_EXTENDED_TIME_FORMAT

ISO 8601 formatter for time without time zone.

public static final FastDateFormat
ISO_8601_EXTENDED_TIME_TIME_ZONE_FORMAT

ISO 8601 formatter for time with time zone.

public static final FastDateFormat
ISO_DATE_FORMAT

Deprecated - as of 4.0, ISO_DATE_FORMAT will be replaced by ISO_8601_EXTENDED_DATE_FORMAT.
public static final FastDateFormat
ISO_DATE_TIME_ZONE_FORMAT

Deprecated - as of 4.0, ISO_DATE_TIME_ZONE_FORMAT will be removed.
ISO 8601-like formatter for date with time zone.
public static final FastDateFormat
ISO_DATETIME_FORMAT

Deprecated - as of 4.0, ISO_DATETIME_FORMAT will be replaced by ISO_8601_EXTENDED_DATETIME_FORMAT.
public static final FastDateFormat
ISO_DATETIME_TIME_ZONE_FORMAT

Deprecated - as of 4.0, ISO_DATETIME_TIME_ZONE_FORMAT will be replaced by ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.
public static final FastDateFormat
ISO_TIME_FORMAT

Deprecated - as of 4.0, ISO_TIME_FORMAT will be removed.
Non-compliant formatter for time without time zone (ISO 8601 does not prefix 'T' for standalone time value).
public static final FastDateFormat
ISO_TIME_NO_T_FORMAT

Deprecated - as of 4.0, ISO_TIME_NO_T_FORMAT will be replaced by ISO_8601_EXTENDED_TIME_FORMAT.
public static final FastDateFormat
ISO_TIME_NO_T_TIME_ZONE_FORMAT

Deprecated - as of 4.0, ISO_TIME_NO_T_TIME_ZONE_FORMAT will be replaced by ISO_8601_EXTENDED_TIME_TIME_ZONE_FORMAT.
public static final FastDateFormat
ISO_TIME_TIME_ZONE_FORMAT

Deprecated - as of 4.0, ISO_TIME_TIME_ZONE_FORMAT will be removed.
Non-compliant formatter for time with time zone (ISO 8601 does not prefix 'T' for standalone time value).
public static final FastDateFormat
SMTP_DATETIME_FORMAT

SMTP (and probably other) date headers.

private static final TimeZone
UTC_TIME_ZONE

The UTC time zone (often referred to as GMT).

Constructor Summary

AccessConstructor and Description
public
DateFormatUtils()
Deprecated TODO Make private in 4.0.

DateFormatUtils instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

the formatted calendar.
format
(final Calendar
the calendar to format, not null.
calendar
,
final String
the pattern to use to format the calendar, not null.
pattern
)

Formats a calendar into a specific pattern.

public static String

Returns:

the formatted calendar.
format
(final Calendar
the calendar to format, not null.
calendar
,
final String
the pattern to use to format the calendar, not null.
pattern
,
final Locale
the locale to use, may be null.
locale
)

Formats a calendar into a specific pattern in a locale.

public static String

Returns:

the formatted calendar.
format
(final Calendar
the calendar to format, not null.
calendar
,
final String
the pattern to use to format the calendar, not null.
pattern
,
final TimeZone
the time zone to use, may be null.
timeZone
)

Formats a calendar into a specific pattern in a time zone.

public static String

Returns:

the formatted calendar.
format
(final Calendar
the calendar to format, not null.
calendar
,
final String
the pattern to use to format the calendar, not null.
pattern
,
final TimeZone
the time zone to use, may be null.
timeZone
,
final Locale
the locale to use, may be null.
locale
)

Formats a calendar into a specific pattern in a time zone and locale.

public static String

Returns:

the formatted date.
format
(final Date
the date to format, not null.
date
,
final String
the pattern to use to format the date, not null.
pattern
)

Formats a date/time into a specific pattern.

public static String

Returns:

the formatted date.
format
(final Date
the date to format, not null.
date
,
final String
the pattern to use to format the date, not null.
pattern
,
final Locale
the locale to use, may be null.
locale
)

Formats a date/time into a specific pattern in a locale.

public static String

Returns:

the formatted date.
format
(final Date
the date to format, not null.
date
,
final String
the pattern to use to format the date, not null.
pattern
,
final TimeZone
the time zone to use, may be null.
timeZone
)

Formats a date/time into a specific pattern in a time zone.

public static String

Returns:

the formatted date.
format
(final Date
the date to format, not null.
date
,
final String
the pattern to use to format the date, not null, not null.
pattern
,
final TimeZone
the time zone to use, may be null.
timeZone
,
final Locale
the locale to use, may be null.
locale
)

Formats a date/time into a specific pattern in a time zone and locale.

public static String

Returns:

the formatted date.
format
(final long
the date to format expressed in milliseconds.
millis
,
final String
the pattern to use to format the date, not null.
pattern
)

Formats a date/time into a specific pattern.

public static String

Returns:

the formatted date.
format
(final long
the date to format expressed in milliseconds.
millis
,
final String
the pattern to use to format the date, not null.
pattern
,
final Locale
the locale to use, may be null.
locale
)

Formats a date/time into a specific pattern in a locale.

public static String

Returns:

the formatted date.
format
(final long
the time expressed in milliseconds.
millis
,
final String
the pattern to use to format the date, not null.
pattern
,
final TimeZone
the time zone to use, may be null.
timeZone
)

Formats a date/time into a specific pattern in a time zone.

public static String

Returns:

the formatted date.
format
(final long
the date to format expressed in milliseconds.
millis
,
final String
the pattern to use to format the date, not null.
pattern
,
final TimeZone
the time zone to use, may be null.
timeZone
,
final Locale
the locale to use, may be null.
locale
)

Formats a date/time into a specific pattern in a time zone and locale.

public static String

Returns:

the formatted date.
formatUTC
(final Date
the date to format, not null.
date
,
final String
the pattern to use to format the date, not null.
pattern
)

Formats a date/time into a specific pattern using the UTC time zone.

public static String

Returns:

the formatted date.
formatUTC
(final Date
the date to format, not null.
date
,
final String
the pattern to use to format the date, not null.
pattern
,
final Locale
the locale to use, may be null.
locale
)

Formats a date/time into a specific pattern using the UTC time zone.

public static String

Returns:

the formatted date.
formatUTC
(final long
the date to format expressed in milliseconds.
millis
,
final String
the pattern to use to format the date, not null.
pattern
)

Formats a date/time into a specific pattern using the UTC time zone.

public static String

Returns:

the formatted date.
formatUTC
(final long
the date to format expressed in milliseconds.
millis
,
final String
the pattern to use to format the date, not null.
pattern
,
final Locale
the locale to use, may be null.
locale
)

Formats a date/time into a specific pattern using the UTC time zone.

private static TimeZone
getTimeZone(final Calendar calendar)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ISO_8601_EXTENDED_DATE_FORMATback to summary
public static final FastDateFormat ISO_8601_EXTENDED_DATE_FORMAT

ISO 8601 formatter for date without time zone.

The format used is yyyy-MM-dd. This format uses the default TimeZone in effect at the time of loading DateFormatUtils class.

Since
3.5
ISO_8601_EXTENDED_DATETIME_FORMATback to summary
public static final FastDateFormat ISO_8601_EXTENDED_DATETIME_FORMAT

ISO 8601 formatter for date-time without time zone.

The format used is yyyy-MM-dd'T'HH:mm:ss. This format uses the default TimeZone in effect at the time of loading DateFormatUtils class.

Since
3.5
ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMATback to summary
public static final FastDateFormat ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT

ISO 8601 formatter for date-time with time zone.

The format used is yyyy-MM-dd'T'HH:mm:ssZZ. This format uses the default TimeZone in effect at the time of loading DateFormatUtils class.

Since
3.5
ISO_8601_EXTENDED_TIME_FORMATback to summary
public static final FastDateFormat ISO_8601_EXTENDED_TIME_FORMAT

ISO 8601 formatter for time without time zone.

The format used is HH:mm:ss. This format uses the default TimeZone in effect at the time of loading DateFormatUtils class.

Since
3.5
ISO_8601_EXTENDED_TIME_TIME_ZONE_FORMATback to summary
public static final FastDateFormat ISO_8601_EXTENDED_TIME_TIME_ZONE_FORMAT

ISO 8601 formatter for time with time zone.

The format used is HH:mm:ssZZ. This format uses the default TimeZone in effect at the time of loading DateFormatUtils class.

Since
3.5
ISO_DATE_FORMATback to summary
public static final FastDateFormat ISO_DATE_FORMAT

Deprecated

- as of 4.0, ISO_DATE_FORMAT will be replaced by ISO_8601_EXTENDED_DATE_FORMAT.

Annotations
@Deprecated
ISO_DATE_TIME_ZONE_FORMATback to summary
public static final FastDateFormat ISO_DATE_TIME_ZONE_FORMAT

Deprecated

- as of 4.0, ISO_DATE_TIME_ZONE_FORMAT will be removed.

ISO 8601-like formatter for date with time zone.

The format used is yyyy-MM-ddZZ. This pattern does not comply with the formal ISO 8601 specification as the standard does not allow a time zone without a time. This format uses the default TimeZone in effect at the time of loading DateFormatUtils class.

Annotations
@Deprecated
ISO_DATETIME_FORMATback to summary
public static final FastDateFormat ISO_DATETIME_FORMAT

Deprecated

- as of 4.0, ISO_DATETIME_FORMAT will be replaced by ISO_8601_EXTENDED_DATETIME_FORMAT.

Annotations
@Deprecated
ISO_DATETIME_TIME_ZONE_FORMATback to summary
public static final FastDateFormat ISO_DATETIME_TIME_ZONE_FORMAT

Deprecated

- as of 4.0, ISO_DATETIME_TIME_ZONE_FORMAT will be replaced by ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.

Annotations
@Deprecated
ISO_TIME_FORMATback to summary
public static final FastDateFormat ISO_TIME_FORMAT

Deprecated

- as of 4.0, ISO_TIME_FORMAT will be removed.

Non-compliant formatter for time without time zone (ISO 8601 does not prefix 'T' for standalone time value).

The format used is 'T'HH:mm:ss. This format uses the default TimeZone in effect at the time of loading DateFormatUtils class.

Annotations
@Deprecated
ISO_TIME_NO_T_FORMATback to summary
public static final FastDateFormat ISO_TIME_NO_T_FORMAT

Deprecated

- as of 4.0, ISO_TIME_NO_T_FORMAT will be replaced by ISO_8601_EXTENDED_TIME_FORMAT.

Annotations
@Deprecated
ISO_TIME_NO_T_TIME_ZONE_FORMATback to summary
public static final FastDateFormat ISO_TIME_NO_T_TIME_ZONE_FORMAT

Deprecated

- as of 4.0, ISO_TIME_NO_T_TIME_ZONE_FORMAT will be replaced by ISO_8601_EXTENDED_TIME_TIME_ZONE_FORMAT.

Annotations
@Deprecated
ISO_TIME_TIME_ZONE_FORMATback to summary
public static final FastDateFormat ISO_TIME_TIME_ZONE_FORMAT

Deprecated

- as of 4.0, ISO_TIME_TIME_ZONE_FORMAT will be removed.

Non-compliant formatter for time with time zone (ISO 8601 does not prefix 'T' for standalone time value).

The format used is 'T'HH:mm:ssZZ. This format uses the default TimeZone in effect at the time of loading DateFormatUtils class.

Annotations
@Deprecated
SMTP_DATETIME_FORMATback to summary
public static final FastDateFormat SMTP_DATETIME_FORMAT

SMTP (and probably other) date headers.

The format used is EEE, dd MMM yyyy HH:mm:ss Z in US locale. This format uses the default TimeZone in effect at the time of loading DateFormatUtils class.

UTC_TIME_ZONEback to summary
private static final TimeZone UTC_TIME_ZONE

The UTC time zone (often referred to as GMT). This is private as it is mutable.

Constructor Detail

DateFormatUtilsback to summary
public DateFormatUtils()

Deprecated

TODO Make private in 4.0.

DateFormatUtils instances should NOT be constructed in standard programming.

This constructor is public to permit tools that require a JavaBean instance to operate.

Annotations
@Deprecated

Method Detail

formatback to summary
public static String format(final Calendar calendar, final String pattern)

Formats a calendar into a specific pattern. The TimeZone from the calendar will be used for formatting.

Parameters
calendar:Calendar

the calendar to format, not null.

pattern:String

the pattern to use to format the calendar, not null.

Returns:String

the formatted calendar.

Since
2.4
See Also
FastDateFormat#format(Calendar)
formatback to summary
public static String format(final Calendar calendar, final String pattern, final Locale locale)

Formats a calendar into a specific pattern in a locale. The TimeZone from the calendar will be used for formatting.

Parameters
calendar:Calendar

the calendar to format, not null.

pattern:String

the pattern to use to format the calendar, not null.

locale:Locale

the locale to use, may be null.

Returns:String

the formatted calendar.

Since
2.4
See Also
FastDateFormat#format(Calendar)
formatback to summary
public static String format(final Calendar calendar, final String pattern, final TimeZone timeZone)

Formats a calendar into a specific pattern in a time zone.

Parameters
calendar:Calendar

the calendar to format, not null.

pattern:String

the pattern to use to format the calendar, not null.

timeZone:TimeZone

the time zone to use, may be null.

Returns:String

the formatted calendar.

Since
2.4
See Also
FastDateFormat#format(Calendar)
formatback to summary
public static String format(final Calendar calendar, final String pattern, final TimeZone timeZone, final Locale locale)

Formats a calendar into a specific pattern in a time zone and locale.

Parameters
calendar:Calendar

the calendar to format, not null.

pattern:String

the pattern to use to format the calendar, not null.

timeZone:TimeZone

the time zone to use, may be null.

locale:Locale

the locale to use, may be null.

Returns:String

the formatted calendar.

Since
2.4
See Also
FastDateFormat#format(Calendar)
formatback to summary
public static String format(final Date date, final String pattern)

Formats a date/time into a specific pattern.

Parameters
date:Date

the date to format, not null.

pattern:String

the pattern to use to format the date, not null.

Returns:String

the formatted date.

formatback to summary
public static String format(final Date date, final String pattern, final Locale locale)

Formats a date/time into a specific pattern in a locale.

Parameters
date:Date

the date to format, not null.

pattern:String

the pattern to use to format the date, not null.

locale:Locale

the locale to use, may be null.

Returns:String

the formatted date.

formatback to summary
public static String format(final Date date, final String pattern, final TimeZone timeZone)

Formats a date/time into a specific pattern in a time zone.

Parameters
date:Date

the date to format, not null.

pattern:String

the pattern to use to format the date, not null.

timeZone:TimeZone

the time zone to use, may be null.

Returns:String

the formatted date.

formatback to summary
public static String format(final Date date, final String pattern, final TimeZone timeZone, final Locale locale)

Formats a date/time into a specific pattern in a time zone and locale.

Parameters
date:Date

the date to format, not null.

pattern:String

the pattern to use to format the date, not null, not null.

timeZone:TimeZone

the time zone to use, may be null.

locale:Locale

the locale to use, may be null.

Returns:String

the formatted date.

formatback to summary
public static String format(final long millis, final String pattern)

Formats a date/time into a specific pattern.

Parameters
millis:long

the date to format expressed in milliseconds.

pattern:String

the pattern to use to format the date, not null.

Returns:String

the formatted date.

formatback to summary
public static String format(final long millis, final String pattern, final Locale locale)

Formats a date/time into a specific pattern in a locale.

Parameters
millis:long

the date to format expressed in milliseconds.

pattern:String

the pattern to use to format the date, not null.

locale:Locale

the locale to use, may be null.

Returns:String

the formatted date.

formatback to summary
public static String format(final long millis, final String pattern, final TimeZone timeZone)

Formats a date/time into a specific pattern in a time zone.

Parameters
millis:long

the time expressed in milliseconds.

pattern:String

the pattern to use to format the date, not null.

timeZone:TimeZone

the time zone to use, may be null.

Returns:String

the formatted date.

formatback to summary
public static String format(final long millis, final String pattern, final TimeZone timeZone, final Locale locale)

Formats a date/time into a specific pattern in a time zone and locale.

Parameters
millis:long

the date to format expressed in milliseconds.

pattern:String

the pattern to use to format the date, not null.

timeZone:TimeZone

the time zone to use, may be null.

locale:Locale

the locale to use, may be null.

Returns:String

the formatted date.

formatUTCback to summary
public static String formatUTC(final Date date, final String pattern)

Formats a date/time into a specific pattern using the UTC time zone.

Parameters
date:Date

the date to format, not null.

pattern:String

the pattern to use to format the date, not null.

Returns:String

the formatted date.

formatUTCback to summary
public static String formatUTC(final Date date, final String pattern, final Locale locale)

Formats a date/time into a specific pattern using the UTC time zone.

Parameters
date:Date

the date to format, not null.

pattern:String

the pattern to use to format the date, not null.

locale:Locale

the locale to use, may be null.

Returns:String

the formatted date.

formatUTCback to summary
public static String formatUTC(final long millis, final String pattern)

Formats a date/time into a specific pattern using the UTC time zone.

Parameters
millis:long

the date to format expressed in milliseconds.

pattern:String

the pattern to use to format the date, not null.

Returns:String

the formatted date.

formatUTCback to summary
public static String formatUTC(final long millis, final String pattern, final Locale locale)

Formats a date/time into a specific pattern using the UTC time zone.

Parameters
millis:long

the date to format expressed in milliseconds.

pattern:String

the pattern to use to format the date, not null.

locale:Locale

the locale to use, may be null.

Returns:String

the formatted date.

getTimeZoneback to summary
private static TimeZone getTimeZone(final Calendar calendar)