public class DateParser extends Object
This class is neither thread-safe nor reusable. Calling the parse() method more than once will yield undefined results.
Modifier and Type | Field and Description |
---|---|
static int |
DAY
Constant for index position of parsed day value.
|
static int |
HOUR
Constant for index position of parsed hour value.
|
static int |
MILLISECOND
Constant for index position of parsed millisecond value.
|
static int |
MINUTE
Constant for index position of parsed minute value.
|
static int |
MONTH
Constant for index position of parsed month value.
|
static int |
SECOND
Constant for index position of parsed second value.
|
static int |
TIMEZONE
Constant for index position of parsed time zone offset value.
|
static int |
YEAR
Constant for index position of parsed year value.
|
Constructor and Description |
---|
DateParser(JSRealm realm,
String string)
Construct a new DateParser instance for parsing the given string.
|
Modifier and Type | Method and Description |
---|---|
Integer[] |
getDateFields()
Get the parsed date and time fields as an array of Integers.
|
boolean |
parse()
Try parsing the given string as date according to the extended ISO 8601 format
specified in ES5 15.9.1.15.
|
boolean |
parseEcmaDate()
Try parsing the date string according to the rules laid out in ES5 15.9.1.15.
|
boolean |
parseLegacyDate()
Try parsing the date using a fuzzy algorithm that can handle a variety of formats.
|
public static final int YEAR
public static final int MONTH
public static final int DAY
public static final int HOUR
public static final int MINUTE
public static final int SECOND
public static final int MILLISECOND
public static final int TIMEZONE
public boolean parse()
public boolean parseEcmaDate()
[('-'|'+')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).
public boolean parseLegacyDate()
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.
public Integer[] getDateFields()
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.