public final class DateUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.text.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
|
static java.lang.String |
ISO8601_DATE_PATTERN
ISO8601-like pattern for date.
|
static java.lang.String |
ISO8601_DATETIME_PATTERN
ISO8601-like pattern for date-time.
|
static java.lang.String |
ISO8601_TIME_PATTERN
ISO8601-like pattern for time.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
format(java.util.Date date,
java.lang.String pattern)
Format a date/time into a specific pattern.
|
static java.lang.String |
format(long date,
java.lang.String pattern)
Format a date/time into a specific pattern.
|
static java.lang.String |
formatElapsedTime(long millis)
Format an elapsed time into a plurialization correct string.
|
static java.lang.String |
getDateForHeader()
Returns the current Date in a format suitable for a SMTP date
header.
|
static int |
getPhaseOfMoon(java.util.Calendar cal)
Calculate the phase of the moon for a given date.
|
static java.util.Date |
parseDateFromHeader(java.lang.String datestr)
Parses the string in a format suitable for a SMTP date header.
|
static java.util.Date |
parseIso8601Date(java.lang.String datestr)
Parse a string as a date using the ISO8601_DATE format which is
yyyy-MM-dd |
static java.util.Date |
parseIso8601DateTime(java.lang.String datestr)
Parse a string as a datetime using the ISO8601_DATETIME format which is
yyyy-MM-dd'T'HH:mm:ss |
static java.util.Date |
parseIso8601DateTimeOrDate(java.lang.String datestr)
Parse a string as a date using the either the ISO8601_DATETIME
or ISO8601_DATE formats.
|
public static final java.lang.String ISO8601_DATETIME_PATTERN
public static final java.lang.String ISO8601_DATE_PATTERN
public static final java.lang.String ISO8601_TIME_PATTERN
public static final java.text.DateFormat DATE_HEADER_FORMAT
public static java.lang.String format(long date, java.lang.String pattern)
date
- the date to format expressed in milliseconds.pattern
- the pattern to use to format the date.public static java.lang.String format(java.util.Date date, java.lang.String pattern)
date
- the date to format expressed in milliseconds.pattern
- the pattern to use to format the date.public static java.lang.String formatElapsedTime(long millis)
millis
- the elapsed time to report in milliseconds.public static int getPhaseOfMoon(java.util.Calendar cal)
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
cal
- the calander.public static java.lang.String getDateForHeader()
public static java.util.Date parseDateFromHeader(java.lang.String datestr) throws java.text.ParseException
datestr
- string to be parsedjava.text.ParseException
- if the supplied string cannot be parsed by
this pattern.public static java.util.Date parseIso8601DateTime(java.lang.String datestr) throws java.text.ParseException
yyyy-MM-dd'T'HH:mm:ss
datestr
- string to be parsedjava.text.ParseException
- if the supplied string cannot be parsed by
this pattern.public static java.util.Date parseIso8601Date(java.lang.String datestr) throws java.text.ParseException
yyyy-MM-dd
datestr
- string to be parsedjava.text.ParseException
- if the supplied string cannot be parsed by
this pattern.public static java.util.Date parseIso8601DateTimeOrDate(java.lang.String datestr) throws java.text.ParseException
datestr
- string to be parsedjava.text.ParseException
- if the supplied string cannot be parsed by
either of these patterns.