Fields Summary |
---|
public static final String | ISO8601_DATETIME_PATTERNISO8601-like pattern for date-time. It does not support timezone.
yyyy-MM-ddTHH:mm:ss |
public static final String | ISO8601_DATE_PATTERNISO8601-like pattern for date. yyyy-MM-dd |
public static final String | ISO8601_TIME_PATTERNISO8601-like pattern for time. HH:mm:ss |
public static final DateFormat | DATE_HEADER_FORMATFormat used for SMTP (and probably other) Date headers. |
private static final MessageFormat | MINUTE_SECONDS |
private static final double[] | LIMITS |
private static final String[] | MINUTES_PART |
private static final String[] | SECONDS_PART |
private static final ChoiceFormat | MINUTES_FORMAT |
private static final ChoiceFormat | SECONDS_FORMAT |
Methods Summary |
---|
private static java.text.DateFormat | createDateFormat(java.lang.String pattern)return a lenient date format set to GMT time zone.
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
TimeZone gmt = TimeZone.getTimeZone("GMT");
sdf.setTimeZone(gmt);
sdf.setLenient(true);
return sdf;
|
public static java.lang.String | format(long date, java.lang.String pattern)Format a date/time into a specific pattern.
return format(new Date(date), pattern);
|
public static java.lang.String | format(java.util.Date date, java.lang.String pattern)Format a date/time into a specific pattern.
DateFormat df = createDateFormat(pattern);
return df.format(date);
|
public static java.lang.String | formatElapsedTime(long millis)Format an elapsed time into a plurialization 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")
long seconds = millis / 1000;
long minutes = seconds / 60;
Object[] args = {new Long(minutes), new Long(seconds % 60)};
return MINUTE_SECONDS.format(args);
|
public static java.lang.String | getDateForHeader()Returns the current Date in a format suitable for a SMTP date
header.
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
int offset = tz.getOffset(cal.get(Calendar.ERA),
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH),
cal.get(Calendar.DAY_OF_WEEK),
cal.get(Calendar.MILLISECOND));
StringBuffer tzMarker = new StringBuffer(offset < 0 ? "-" : "+");
offset = Math.abs(offset);
int hours = offset / (60 * 60 * 1000);
int minutes = offset / (60 * 1000) - 60 * hours;
if (hours < 10) {
tzMarker.append("0");
}
tzMarker.append(hours);
if (minutes < 10) {
tzMarker.append("0");
}
tzMarker.append(minutes);
return DATE_HEADER_FORMAT.format(cal.getTime()) + tzMarker.toString();
|
public static int | getPhaseOfMoon(java.util.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
int dayOfTheYear = cal.get(Calendar.DAY_OF_YEAR);
int yearInMetonicCycle = ((cal.get(Calendar.YEAR) - 1900) % 19) + 1;
int epact = (11 * yearInMetonicCycle + 18) % 30;
if ((epact == 25 && yearInMetonicCycle > 11) || epact == 24) {
epact++;
}
return (((((dayOfTheYear + epact) * 6) + 11) % 177) / 22) & 7;
|
public 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
return new SimpleDateFormat(ISO8601_DATE_PATTERN).parse(datestr);
|
public 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
return new SimpleDateFormat(ISO8601_DATETIME_PATTERN).parse(datestr);
|
public 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.
try {
return parseIso8601DateTime(datestr);
} catch (ParseException px) {
return parseIso8601Date(datestr);
}
|