Top Description Fields Constructors Methods
org.apache.tools.ant.util

public final Class DateUtils

extends Object
Class Inheritance
Imports
java.text.ChoiceFormat, .DateFormat, .MessageFormat, .ParseException, .SimpleDateFormat, java.util.Calendar, .Date, .Locale, .TimeZone, java.util.regex.Matcher, .Pattern

Helper methods to deal with date/time formatting with a specific defined format (ISO8601) or a correct pluralization of elapsed time in minutes and seconds.
Since
Ant 1.5

Field Summary

Modifier and TypeField and Description
public static final DateFormat
DATE_HEADER_FORMAT

Format used for SMTP (and probably other) Date headers.

private static final DateFormat
public static final ThreadLocal<DateFormat>
EN_US_DATE_FORMAT_MIN

Provides a thread-local US-style date format.

public static final ThreadLocal<DateFormat>
EN_US_DATE_FORMAT_SEC

Provides a thread-local US-style date format.

public static final String
ISO8601_DATE_PATTERN

ISO8601-like pattern for date.

public static final String
ISO8601_DATETIME_PATTERN

ISO8601-like pattern for date-time.

public static final String
ISO8601_TIME_PATTERN

ISO8601-like pattern for time.

private static final Pattern
private static final ThreadLocal<DateFormat>
private static final double[]
private static final MessageFormat
private static final ChoiceFormat
private static final String[]
private static final int
private static final int
private static final int
private static final ChoiceFormat
private static final String[]
private static final int

Constructor Summary

AccessConstructor and Description
private
DateUtils()

private constructor

Method Summary

Modifier and TypeMethod and Description
private static DateFormat

Returns:

the configured format for this pattern.
createDateFormat
(String
the pattern used for date/time formatting.
pattern
)

return a lenient date format set to GMT time zone.

public static String

Returns:

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

Format a date/time into a specific pattern.

public static String

Returns:

the formatted date.
format
(Date
the date to format expressed in milliseconds.
date
,
String
the pattern to use to format the date.
pattern
)

Format a date/time into a specific pattern.

public static String

Returns:

the formatted text in minutes/seconds.
formatElapsedTime
(long
the elapsed time to report in milliseconds.
millis
)

Format an elapsed time into a pluralization correct string.

public static String

Returns:

the current date.
getDateForHeader
()

Returns the current Date in a format suitable for a SMTP date header.

public static int

Returns:

The phase of the moon as a number between 0 and 7 with 0 meaning new moon and 4 meaning full moon.
getPhaseOfMoon
(Calendar
the calendar.
cal
)

Calculate the phase of the moon for a given date.

public static Date

Returns:

a java.util.Date object as parsed by the format.
parseDateFromHeader
(String
string to be parsed
datestr
)

Parses the string in a format suitable for a SMTP date header.

public static Date

Returns:

a java.util.Date object as parsed by the format.
parseIso8601Date
(String
string to be parsed
datestr
)

Parse a string as a date using the ISO8601_DATE format which is yyyy-MM-dd

public static Date

Returns:

a java.util.Date object as parsed by the format.
parseIso8601DateTime
(String
string to be parsed
datestr
)

Parse a string as a datetime using the ISO8601_DATETIME format which is yyyy-MM-dd'T'HH:mm:ss

public static Date

Returns:

a java.util.Date object as parsed by the formats.
parseIso8601DateTimeOrDate
(String
string to be parsed
datestr
)

Parse a string as a date using the either the ISO8601_DATETIME or ISO8601_DATE formats.

public static Date

Returns:

Date
parseLenientDateTime
(String
String
dateStr
)

Parse a lenient ISO 8601, ms since epoch, or <touch>-style date.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DATE_HEADER_FORMATback to summary
public static final DateFormat DATE_HEADER_FORMAT

Deprecated

DateFormat is not thread safe, and we cannot guarantee that some other code is using the format in parallel. Deprecated since ant 1.8

Format used for SMTP (and probably other) Date headers.

Annotations
@Deprecated
DATE_HEADER_FORMAT_INTback to summary
private static final DateFormat DATE_HEADER_FORMAT_INT
EN_US_DATE_FORMAT_MINback to summary
public static final ThreadLocal<DateFormat> EN_US_DATE_FORMAT_MIN

Provides a thread-local US-style date format. Exactly as used by <touch>, to minute precision: SimpleDateFormat("MM/dd/yyyy hh:mm a", Locale.US)

Since
Ant 1.10.2
EN_US_DATE_FORMAT_SECback to summary
public static final ThreadLocal<DateFormat> EN_US_DATE_FORMAT_SEC

Provides a thread-local US-style date format. Exactly as used by <touch>, to second precision: SimpleDateFormat("MM/dd/yyyy hh:mm:ss a", Locale.US)

Since
Ant 1.10.2
ISO8601_DATE_PATTERNback to summary
public static final String ISO8601_DATE_PATTERN

ISO8601-like pattern for date. yyyy-MM-dd

ISO8601_DATETIME_PATTERNback to summary
public static final String ISO8601_DATETIME_PATTERN

ISO8601-like pattern for date-time. It does not support timezone. yyyy-MM-ddTHH:mm:ss

ISO8601_TIME_PATTERNback to summary
public static final String ISO8601_TIME_PATTERN

ISO8601-like pattern for time. HH:mm:ss

iso8601normalizerback to summary
private static final Pattern iso8601normalizer
iso8601WithTimeZoneback to summary
private static final ThreadLocal<DateFormat> iso8601WithTimeZone
LIMITSback to summary
private static final double[] LIMITS
MINUTE_SECONDSback to summary
private static final MessageFormat MINUTE_SECONDS
MINUTES_FORMATback to summary
private static final ChoiceFormat MINUTES_FORMAT
MINUTES_PARTback to summary
private static final String[] MINUTES_PART
ONE_HOURback to summary
private static final int ONE_HOUR
ONE_MINUTEback to summary
private static final int ONE_MINUTE
ONE_SECONDback to summary
private static final int ONE_SECOND
SECONDS_FORMATback to summary
private static final ChoiceFormat SECONDS_FORMAT
SECONDS_PARTback to summary
private static final String[] SECONDS_PART
TENback to summary
private static final int TEN

Constructor Detail

DateUtilsback to summary
private DateUtils()

private constructor

Method Detail

createDateFormatback to summary
private static DateFormat createDateFormat(String pattern)

return a lenient date format set to GMT time zone.

Parameters
pattern:String

the pattern used for date/time formatting.

Returns:DateFormat

the configured format for this pattern.

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

Format a date/time into a specific pattern.

Parameters
date:long

the date to format expressed in milliseconds.

pattern:String

the pattern to use to format the date.

Returns:String

the formatted date.

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

Format a date/time into a specific pattern.

Parameters
date:Date

the date to format expressed in milliseconds.

pattern:String

the pattern to use to format the date.

Returns:String

the formatted date.

formatElapsedTimeback to summary
public static String formatElapsedTime(long millis)

Format an elapsed time into a pluralization correct string. It is limited only to report elapsed time in minutes and seconds and has the following behavior.

  • minutes are not displayed when 0. (ie: "45 seconds")
  • seconds are always displayed in plural form (ie "0 seconds" or "10 seconds") except for 1 (ie "1 second")
Parameters
millis:long

the elapsed time to report in milliseconds.

Returns:String

the formatted text in minutes/seconds.

getDateForHeaderback to summary
public static String getDateForHeader()

Returns the current Date in a format suitable for a SMTP date header.

Returns:String

the current date.

Since
Ant 1.5.2
getPhaseOfMoonback to summary
public static int getPhaseOfMoon(Calendar cal)

Calculate the phase of the moon for a given date.

Code heavily influenced by hacklib.c in Nethack

The Algorithm:

moon period = 29.53058 days ~= 30, year = 365.2422 days

days moon phase advances on first day of year compared to preceding year
 = 365.2422 - 12 * 29.53058 ~= 11

years in Metonic cycle (time until same phases fall on the same days of
 the month) = 18.6 ~= 19

moon phase on first day of year (epact) ~= (11*(year%19) + 18) % 30
 (18 as initial condition for 1900)

current phase in days = first day phase + days elapsed in year

6 moons ~= 177 days
177 ~= 8 reported phases * 22
+ 11 / 22 for rounding
Parameters
cal:Calendar

the calendar.

Returns:int

The phase of the moon as a number between 0 and 7 with 0 meaning new moon and 4 meaning full moon.

Since
1.2, Ant 1.5
parseDateFromHeaderback to summary
public static Date parseDateFromHeader(String datestr) throws ParseException

Parses the string in a format suitable for a SMTP date header.

Parameters
datestr:String

string to be parsed

Returns:Date

a java.util.Date object as parsed by the format.

Exceptions
ParseException:
if the supplied string cannot be parsed by this pattern.
Since
Ant 1.8.0
parseIso8601Dateback to summary
public static Date parseIso8601Date(String datestr) throws ParseException

Parse a string as a date using the ISO8601_DATE format which is yyyy-MM-dd

Parameters
datestr:String

string to be parsed

Returns:Date

a java.util.Date object as parsed by the format.

Exceptions
ParseException:
if the supplied string cannot be parsed by this pattern.
Since
Ant 1.6
parseIso8601DateTimeback to summary
public static Date parseIso8601DateTime(String datestr) throws ParseException

Parse a string as a datetime using the ISO8601_DATETIME format which is yyyy-MM-dd'T'HH:mm:ss

Parameters
datestr:String

string to be parsed

Returns:Date

a java.util.Date object as parsed by the format.

Exceptions
ParseException:
if the supplied string cannot be parsed by this pattern.
Since
Ant 1.6
parseIso8601DateTimeOrDateback to summary
public static Date parseIso8601DateTimeOrDate(String datestr) throws ParseException

Parse a string as a date using the either the ISO8601_DATETIME or ISO8601_DATE formats.

Parameters
datestr:String

string to be parsed

Returns:Date

a java.util.Date object as parsed by the formats.

Exceptions
ParseException:
if the supplied string cannot be parsed by either of these patterns.
Since
Ant 1.6
parseLenientDateTimeback to summary
public static Date parseLenientDateTime(String dateStr) throws ParseException

Parse a lenient ISO 8601, ms since epoch, or <touch>-style date. That is:

  • Milliseconds since 1970-01-01 00:00
  • YYYY-MM-DD{T| }HH:MM[:SS[.SSS]][ ][±ZZ[[:]ZZ]]
  • MM/DD/YYYY HH:MM[:SS] {AM|PM}
where {a|b} indicates that you must choose one of a or b, and [c] indicates that you may use or omit c. ±ZZZZ is the timezone offset, and may be literally "Z" to mean GMT.
Parameters
dateStr:String

String

Returns:Date

Date

Exceptions
ParseException:
if date string does not match ISO 8601
Since
Ant 1.10.2