edu.stanford.nlp.ie.pascal
Class ISODateInstance

java.lang.Object
  extended by edu.stanford.nlp.ie.pascal.ISODateInstance

public class ISODateInstance
extends java.lang.Object

Represents dates and times according to ISO8601 standard while also allowing for wild cards - e.g., can represent "21 June" without a year (Standard ISO8601 only allows removing less precise annotations (e.g., 200706 rather than 20070621 but not a way to represent 0621 without a year)

Format stores date and time separately since the majority of current use cases involve only one of these items. Standard ISO 8601 instead requires <date>T<time>.

Ranges are specified within the strings via forward slash. For example 6 June - 8 June is represented ****0606/****0608. 6 June onward is ****0606/ and until 8 June is /****0608.

Author:
Anna Rafferty TODO: add time support - currently just dates are supported

Nested Class Summary
static class ISODateInstance.DateField
          Enum for the fields *
 
Field Summary
static java.lang.String BOUNDED_RANGE
           
static int DAY_OF_HALF_MONTH
           
static java.util.HashMap<java.lang.Integer,java.lang.Integer> daysPerMonth
           
static int LAST_DAY_OF_MONTH
           
static java.lang.String LAST_MONTH_OF_YEAR
           
static java.lang.String MONTH_OF_HALF_YEAR
           
static java.lang.String NO_RANGE
           
static java.lang.String OPEN_RANGE_AFTER
           
static java.lang.String OPEN_RANGE_BEFORE
           
static java.util.HashMap<java.lang.String,Pair<ISODateInstance.DateField,java.lang.Integer>> relativeDateMap
          HashMap for mapping a relativeDate String to a pair with the field that should be modified and the amount to modify it *
 
Constructor Summary
ISODateInstance()
          Creates an empty date instance; you probably don't want this in most cases.
ISODateInstance(ISODateInstance start, ISODateInstance end)
          Constructor for a range of dates, beginning at date start and finishing at date end
ISODateInstance(ISODateInstance referenceDate, java.lang.String relativeDate)
          Construct a new ISODate based on its relation to a referenceDate.
ISODateInstance(java.lang.String date)
          Takes a string that represents a date, and attempts to normalize it into ISO 8601-compatible format.
ISODateInstance(java.lang.String date, java.lang.String openRangeMarker)
           
 
Method Summary
 boolean contains(ISODateInstance other)
          Returns true iff this date contains the date represented by other.
 boolean extractDay(java.lang.String inputDate)
           
 boolean extractFields(java.lang.String inputDate)
          Uses regexp matching to match month, day, and year fields TODO: Find a way to mark what;s already been handled in the string
 boolean extractMonth(java.lang.String inputDate)
           
 boolean extractWeekday(java.lang.String inputDate)
          This is a backup method if everything else fails.
 boolean extractYear(java.lang.String inputDate)
           
static ISODateInstance fromDateString(java.lang.String date)
          Takes a string already formatted in ISODateInstance format (such as one previously written out using toString) and creates a new date instance from it
 java.lang.String getDateString()
          Provided for backwards compatibility with DateInstance; returns the same thing as toString()
 java.lang.String getEndDate()
          Returns this date or if it is a range, the date the range ends.
 java.lang.String getStartDate()
          Returns this date or if it is a range, the date the range starts.
 boolean isAfter(java.lang.String dateString)
          Returns true if this date instance is after the given dateString.
static boolean isCompatible(java.lang.String date1, java.lang.String date2)
          Returns true iff date1 could represent the same value as date2 e.g.
 boolean isCompatibleDate(ISODateInstance other)
           
 boolean isDayCompatible(ISODateInstance other)
          Looks if the days for the two dates are compatible.
 boolean isMonthCompatible(ISODateInstance other)
          Looks if the months for the two dates are compatible.
 boolean isRange()
          Returns true iff this date represents a range The range must have at least a start or end date, but is not guaranteed to have both
 boolean isUnparseable()
          Returns true iff we were unable to parse the input String associated with this date; in that case, we just store the input string and shortcircuit all of the comparison methods
 boolean isYearCompatible(ISODateInstance other)
          Looks if the years for the two dates are compatible.
static void main(java.lang.String[] args)
          For testing only
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

OPEN_RANGE_AFTER

public static final java.lang.String OPEN_RANGE_AFTER
See Also:
Constant Field Values

OPEN_RANGE_BEFORE

public static final java.lang.String OPEN_RANGE_BEFORE
See Also:
Constant Field Values

BOUNDED_RANGE

public static final java.lang.String BOUNDED_RANGE
See Also:
Constant Field Values

NO_RANGE

public static final java.lang.String NO_RANGE
See Also:
Constant Field Values

DAY_OF_HALF_MONTH

public static final int DAY_OF_HALF_MONTH
See Also:
Constant Field Values

LAST_DAY_OF_MONTH

public static final int LAST_DAY_OF_MONTH
See Also:
Constant Field Values

MONTH_OF_HALF_YEAR

public static final java.lang.String MONTH_OF_HALF_YEAR
See Also:
Constant Field Values

LAST_MONTH_OF_YEAR

public static final java.lang.String LAST_MONTH_OF_YEAR
See Also:
Constant Field Values

relativeDateMap

public static final java.util.HashMap<java.lang.String,Pair<ISODateInstance.DateField,java.lang.Integer>> relativeDateMap
HashMap for mapping a relativeDate String to a pair with the field that should be modified and the amount to modify it *


daysPerMonth

public static final java.util.HashMap<java.lang.Integer,java.lang.Integer> daysPerMonth
Constructor Detail

ISODateInstance

public ISODateInstance()
Creates an empty date instance; you probably don't want this in most cases.


ISODateInstance

public ISODateInstance(java.lang.String date)
Takes a string that represents a date, and attempts to normalize it into ISO 8601-compatible format.


ISODateInstance

public ISODateInstance(java.lang.String date,
                       java.lang.String openRangeMarker)

ISODateInstance

public ISODateInstance(ISODateInstance start,
                       ISODateInstance end)
Constructor for a range of dates, beginning at date start and finishing at date end


ISODateInstance

public ISODateInstance(ISODateInstance referenceDate,
                       java.lang.String relativeDate)
Construct a new ISODate based on its relation to a referenceDate. relativeDate should be something like "today" or "tomorrow" or "last year" and the resulting ISODate will be the same as the referenceDate, a day later, or a year earlier, respectively.

Method Detail

fromDateString

public static ISODateInstance fromDateString(java.lang.String date)
Takes a string already formatted in ISODateInstance format (such as one previously written out using toString) and creates a new date instance from it


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getDateString

public java.lang.String getDateString()
Provided for backwards compatibility with DateInstance; returns the same thing as toString()


extractFields

public boolean extractFields(java.lang.String inputDate)
Uses regexp matching to match month, day, and year fields TODO: Find a way to mark what;s already been handled in the string


isRange

public boolean isRange()
Returns true iff this date represents a range The range must have at least a start or end date, but is not guaranteed to have both

Returns:
Whether this date represents a range

isUnparseable

public boolean isUnparseable()
Returns true iff we were unable to parse the input String associated with this date; in that case, we just store the input string and shortcircuit all of the comparison methods


getStartDate

public java.lang.String getStartDate()
Returns this date or if it is a range, the date the range starts. If the date is of the form /<date>, "" is returned

Returns:
Start date of range

getEndDate

public java.lang.String getEndDate()
Returns this date or if it is a range, the date the range ends. If the date is of the form <date>/, "" is returned

Returns:
End date of range

isCompatible

public static boolean isCompatible(java.lang.String date1,
                                   java.lang.String date2)
Returns true iff date1 could represent the same value as date2 e.g. ****07 is compatible with 200207 (and 200207 is compatible with ****07) 200207 is compatible with 20020714 (?maybe need a better idea of use case here...)


contains

public boolean contains(ISODateInstance other)
Returns true iff this date contains the date represented by other. A range contains a date if it is equal to or after the start date and equal to or before the end date. For open ranges, contains is also inclusive of the one end point.


isAfter

public boolean isAfter(java.lang.String dateString)
Returns true if this date instance is after the given dateString. If this date instance is a range, then returns true only if both start and end dates are after dateString.

Several tricky cases exist, and implementation tries to go with the commonsense interpretation: When a year and a month are given for one, but only a month for the other, it is assumed that both have the same year e.g: ****12 is after 200211

When a year and a month are given for one but only a year for the other, it is assumed that one of these is after the other only if the years differ, e.g.: 2003 is after 200211 2002 is not after 200211 200211 is not after 2002


isCompatibleDate

public boolean isCompatibleDate(ISODateInstance other)

isYearCompatible

public boolean isYearCompatible(ISODateInstance other)
Looks if the years for the two dates are compatible. This method does not consider ranges and uses only the start date.


isMonthCompatible

public boolean isMonthCompatible(ISODateInstance other)
Looks if the months for the two dates are compatible. This method does not consider ranges and uses only the start date.


isDayCompatible

public boolean isDayCompatible(ISODateInstance other)
Looks if the days for the two dates are compatible. This method does not consider ranges and uses only the start date.


extractYear

public boolean extractYear(java.lang.String inputDate)

extractMonth

public boolean extractMonth(java.lang.String inputDate)

extractDay

public boolean extractDay(java.lang.String inputDate)

extractWeekday

public boolean extractWeekday(java.lang.String inputDate)
This is a backup method if everything else fails. It searches for named days of the week and if it finds one, it sets that as the date in lowercase form


main

public static void main(java.lang.String[] args)
For testing only



Stanford NLP Group