public class DateMathParser extends Object
The basic syntax support addition, subtraction and rounding at various levels of granularity (or "units"). Commands can be chained together and are parsed from left to right. '+' and '-' denote addition and subtraction, while '/' denotes "round". Round requires only a unit, while addition/subtraction require an integer value and a unit. Command strings must not include white space, but the "No-Op" command (empty string) is allowed....
/HOUR ... Round to the start of the current hour /DAY ... Round to the start of the current day +2YEARS ... Exactly two years in the future from now -1DAY ... Exactly 1 day prior to now /DAY+6MONTHS+3DAYS ... 6 months and 3 days in the future from the start of the current day +6MONTHS+3DAYS/DAY ... 6 months and 3 days in the future from now, rounded down to nearest day
All commands are relative to a "now" which is fixed in an instance of
DateMathParser such that
p.parseMath("+0MILLISECOND").equals(p.parseMath("+0MILLISECOND"))
no matter how many wall clock milliseconds elapse between the two
distinct calls to parse (Assuming no other thread calls
"setNow
" in the interim)
Multiple aliases exist for the various units of time (ie:
MINUTE
and MINUTES
; MILLI
,
MILLIS
, MILLISECOND
, and
MILLISECONDS
.) The complete list can be found by
inspecting the keySet of CALENDAR_UNITS
.
Modifier and Type | Field and Description |
---|---|
static Map<String,Integer> |
CALENDAR_UNITS
A mapping from (uppercased) String labels idenyifying time units,
to the corresponding Calendar constant used to set/add/roll that unit
of measurement.
|
Constructor and Description |
---|
DateMathParser(TimeZone tz,
Locale l) |
Modifier and Type | Method and Description |
---|---|
static void |
add(Calendar c,
int val,
String unit)
Modifies the specified Calendar by "adding" the specified value of units
|
Date |
getNow()
Returns a cloned of this instance's concept of "now"
|
Date |
parseMath(String math)
Parses a string of commands relative "now" are returns the resulting Date.
|
static void |
round(Calendar c,
String unit)
Modifies the specified Calendar by "rounding" down to the specified unit
|
void |
setNow(Date n)
Redefines this instance's concept of "now"
|
public static final Map<String,Integer> CALENDAR_UNITS
A single logical unit of time might be represented by multiple labels
for convenience (ie: DATE==DAY
,
MILLI==MILLISECOND
)
Calendar
public DateMathParser(TimeZone tz, Locale l)
tz
- The TimeZone used for rounding (to determine when hours/days begin)l
- The Locale used for rounding (to determine when weeks begin)Calendar.getInstance(TimeZone,Locale)
public static void add(Calendar c, int val, String unit)
IllegalArgumentException
- if unit isn't recognized.CALENDAR_UNITS
public static void round(Calendar c, String unit)
IllegalArgumentException
- if unit isn't recognized.CALENDAR_UNITS
public void setNow(Date n)
public Date getNow()
public Date parseMath(String math) throws ParseException
ParseException
- positions in ParseExceptions are token positions, not character positions.