A Simple Utility class for parsing "math" like strings relating to Dates.
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 .
author: taken from Solr |