Class DateMathParser
- java.lang.Object
-
- org.apache.solr.util.DateMathParser
-
public class DateMathParser extends Object
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
(Multiple aliases exist for the various units of time (ie:
MINUTE
andMINUTES
;MILLI
,MILLIS
,MILLISECOND
, andMILLISECONDS
.) The complete list can be found by inspecting the keySet ofCALENDAR_UNITS
)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). The default value of 'now' is the time at the moment theDateMathParser
instance is constructed, unless overridden by theNOW
request param.All commands are also affected to the rules of a specified
TimeZone
(including the start/end of DST if any) which determine when each arbitrary day starts. This not only impacts rounding/adding of DAYs, but also cascades to rounding of HOUR, MIN, MONTH, YEAR as well. The defaultTimeZone
used isUTC
unless overridden by theTZ
request param.Historical dates: The calendar computation is completely done with the Gregorian system/algorithm. It does not switch to Julian or anything else, unlike the default
GregorianCalendar
.
-
-
Field Summary
Fields Modifier and Type Field Description static Map<String,ChronoUnit>
CALENDAR_UNITS
A mapping from (uppercased) String labels identifying time units, to the correspondingChronoUnit
enum (e.g.static TimeZone
DEFAULT_MATH_TZ
Default TimeZone for DateMath rounding (UTC)static DateTimeFormatter
PARSER
Differs byDateTimeFormatter.ISO_INSTANT
in that it's lenient.static TimeZone
UTC
-
Constructor Summary
Constructors Constructor Description DateMathParser()
Chooses defaults based on the current request.DateMathParser(Date now, TimeZone tz)
DateMathParser(TimeZone tz)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Date
getNow()
Returns a clone of this instance's concept of "now" (never null).TimeZone
getTimeZone()
Date
parseMath(String math)
Parses a string of commands relative "now" are returns the resulting Date.static Date
parseMath(Date now, String val)
Parses a String which may be a date (in the standard ISO-8601 format) followed by an optional math expression.static Date
parseMath(Date now, String val, TimeZone zone)
Parses a String which may be a date (in the standard ISO-8601 format) followed by an optional math expression.void
setNow(Date n)
Defines this instance's concept of "now".
-
-
-
Field Detail
-
UTC
public static final TimeZone UTC
-
DEFAULT_MATH_TZ
public static final TimeZone DEFAULT_MATH_TZ
Default TimeZone for DateMath rounding (UTC)
-
PARSER
public static final DateTimeFormatter PARSER
Differs byDateTimeFormatter.ISO_INSTANT
in that it's lenient.- See Also:
parseNoMath(String)
-
CALENDAR_UNITS
public static final Map<String,ChronoUnit> CALENDAR_UNITS
A mapping from (uppercased) String labels identifying time units, to the correspondingChronoUnit
enum (e.g. "YEARS") used to set/add/roll that unit of measurement.A single logical unit of time might be represented by multiple labels for convenience (ie:
DATE==DAYS
,MILLI==MILLIS
)- See Also:
Calendar
-
-
Constructor Detail
-
DateMathParser
public DateMathParser()
Chooses defaults based on the current request.
-
DateMathParser
public DateMathParser(TimeZone tz)
-
DateMathParser
public DateMathParser(Date now, TimeZone tz)
- Parameters:
now
- The current time. If null, it defaults toSolrRequestInfo.getNOW()
. otherwise the current time is assumed.tz
- The TimeZone used for rounding (to determine when hours/days begin). If null, then this method defaults to the value dictated by the SolrRequestInfo if it exists -- otherwise it uses UTC.- See Also:
DEFAULT_MATH_TZ
,Calendar.getInstance(TimeZone,Locale)
,SolrRequestInfo.getClientTimeZone()
-
-
Method Detail
-
parseMath
public static Date parseMath(Date now, String val)
Parses a String which may be a date (in the standard ISO-8601 format) followed by an optional math expression. The TimeZone is taken from theTZ
param retrieved viaSolrRequestInfo
, defaulting to UTC.- Parameters:
now
- an optional fixed date to use as "NOW".SolrRequestInfo
is consulted if unspecified.val
- the string to parse
-
parseMath
public static Date parseMath(Date now, String val, TimeZone zone)
Parses a String which may be a date (in the standard ISO-8601 format) followed by an optional math expression.- Parameters:
now
- an optional fixed date to use as "NOW"val
- the string to parsezone
- the timezone to use
-
getTimeZone
public TimeZone getTimeZone()
- Returns:
- the time zone
-
getNow
public Date getNow()
Returns a clone of this instance's concept of "now" (never null).If setNow was never called (or if null was specified) then this method first defines 'now' as the value dictated by the SolrRequestInfo if it exists -- otherwise it uses a new Date instance at the moment getNow() is first called.
- See Also:
setNow(java.util.Date)
,SolrRequestInfo.getNOW()
-
parseMath
public Date parseMath(String math) throws ParseException
Parses a string of commands relative "now" are returns the resulting Date.- Throws:
ParseException
- positions in ParseExceptions are token positions, not character positions.
-
-