Class DateParser
This class is neither thread-safe nor reusable. Calling the parse() method more than once will yield undefined results.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intConstant for index position of parsed day value.static final intConstant for index position of parsed hour value.static final intConstant for index position of parsed millisecond value.static final intConstant for index position of parsed minute value.static final intConstant for index position of parsed month value.static final intConstant for index position of parsed second value.static final intConstant for index position of parsed time zone offset value.static final intConstant for index position of parsed year value. -
Constructor Summary
ConstructorsConstructorDescriptionDateParser(JSRealm realm, String string, boolean extraLenient) Construct a new DateParser instance for parsing the given string. -
Method Summary
Modifier and TypeMethodDescriptionInteger[]Get the parsed date and time fields as an array of Integers.booleanparse()Try parsing the given string as date according to the extended ISO 8601 format specified in ES5 15.9.1.15.booleanTry parsing the date string according to the rules laid out in ES5 15.9.1.15.booleanTry parsing the date using a fuzzy algorithm that can handle a variety of formats.
-
Field Details
-
YEAR
public static final int YEARConstant for index position of parsed year value.- See Also:
-
MONTH
public static final int MONTHConstant for index position of parsed month value.- See Also:
-
DAY
public static final int DAYConstant for index position of parsed day value.- See Also:
-
HOUR
public static final int HOURConstant for index position of parsed hour value.- See Also:
-
MINUTE
public static final int MINUTEConstant for index position of parsed minute value.- See Also:
-
SECOND
public static final int SECONDConstant for index position of parsed second value.- See Also:
-
MILLISECOND
public static final int MILLISECONDConstant for index position of parsed millisecond value.- See Also:
-
TIMEZONE
public static final int TIMEZONEConstant for index position of parsed time zone offset value.- See Also:
-
-
Constructor Details
-
DateParser
-
-
Method Details
-
parse
public boolean parse()Try parsing the given string as date according to the extended ISO 8601 format specified in ES5 15.9.1.15. Fall back to legacy mode if that fails. This method returns true if the string could be parsed.- Returns:
- true if the string could be parsed as date
-
parseEcmaDate
public boolean parseEcmaDate()Try parsing the date string according to the rules laid out in ES5 15.9.1.15. The date string must conform to the following format:[('-'|'+')yy]yyyy[-MM[-dd]][Thh:mm[:ss[.sss]][Z|(+|-)hh:mm]]If the string does not contain a time zone offset, the TIMEZONE field is set to 0 (GMT).
- Returns:
- true if string represents a valid ES5 date string.
-
parseLegacyDate
public boolean parseLegacyDate()Try parsing the date using a fuzzy algorithm that can handle a variety of formats.Numbers separated by ':' are treated as time values, optionally followed by a millisecond value separated by '.'. Other number values are treated as date values. The exact sequence of day, month, and year values to apply is determined heuristically.
English month names and selected time zone names as well as AM/PM markers are recognized and handled properly. Additionally, numeric time zone offsets such as (+|-)hh:mm or (+|-)hhmm are recognized. If the string does not contain a time zone offset the TIMEZONEfield is left undefined, meaning the local time zone should be applied.
English weekday names are recognized but ignored. All text in parentheses is ignored as well. All other text causes parsing to fail.
- Returns:
- true if the string could be parsed
-
getDateFields
Get the parsed date and time fields as an array of Integers.If parsing was successful, all fields are guaranteed to be set except for the TIMEZONE field which may be null, meaning that local time zone offset should be applied.
- Returns:
- the parsed date fields
-