Class DateTimeParse

java.lang.Object
com.Ostermiller.util.DateTimeParse

public class DateTimeParse extends Object
Parses a variety of formatted date strings with minimal configuration. Unlike other date parsers, there are no formats to specify. There is a single parse string method.

A wide variety of formats are supported:

  • Four digit years (1999)
  • Two digit years with an apostrophe ('99) with customizable year extension policy
  • Month numbers (11 for November; 7 or 07 for July)
  • Full month names spelled out (November)
  • Abbreviated month names (Nov and No)
  • Month names in several languages (Noviembre)
  • Day numbers (1 or 01 for the first day of the month)
  • Ordinal day of the month numbers (June first or 1st)
  • Ordinal day of the month in several languages (primera, 1o)
  • Locale appropriate parsing for ambiguous ordering (01-02-1999) with set-able locale
  • Day of the week in several languages
  • Era in several languages (AD, BC, BCE)
  • Hour minute time (11:52)
  • Hour minute second time (11:52:33)
  • Standard date format with a "T" separating date and time (1997-07-16T19:20)
  • AM/PM in several languages
  • Numeric time zones (-0500)
Since:
ostermillerutils 1.08.00
Author:
Stephen Ostermiller https://ostermiller.org/contact.pl?regarding=Java+Utilities
  • Constructor Details

    • DateTimeParse

      public DateTimeParse()
      Create a date/time parser using the default locale.
    • DateTimeParse

      public DateTimeParse(Locale locale)
      Create a date/time parser for the given locale.
      Parameters:
      locale - the locale to use for parsing
  • Method Details

    • setFieldOrder

      public void setFieldOrder(List<DateTimeParse.Field> fieldOrder)
      Set the order in which date fields should be parsed.
      Parameters:
      fieldOrder - the order of date fields
    • setFieldOrder

      public void setFieldOrder(DateTimeParse.Field[] fieldOrder)
      Set the order in which date fields should be parsed.
      Parameters:
      fieldOrder - the order of date fields
    • getFieldOrder

      public List<DateTimeParse.Field> getFieldOrder()
      Get the order in which date fields are parsed.
      Returns:
      the field order
    • setDefaultYear

      public void setDefaultYear(int defaultYear)
      Set the default year to use when there is no year in the parsed date.
      Since:
      ostermillerutils 1.08.00
    • setYearExtensionPolicy

      public DateTimeParse setYearExtensionPolicy(YearExtensionPolicy yearExtensionPolicy)
      Set the year extension policy. This policy is responsible for extending two digit years into full years. eg. 99 to 1999

      The default policy is YearExtensionAround.NEAREST. Several other policies are implemented:

      • YearExtensionAround.NEAREST
      • YearExtensionAround.LATEST
      • YearExtensionAround.CENTURY_1999
      • YearExtensionAround.CENTURY_2000
      • YearExtensionNone.YEAR_EXTENSION_NONE
      Since:
      ostermillerutils 1.08.00
    • parse

      public Date parse(String dateString)
      Parse the given string into a Date.
      Parameters:
      dateString - String with a date representation.
      Returns:
      timestamp represented by the date string, or null if the date could not be parsed.
      Since:
      ostermillerutils 1.08.00
    • setTimeZone

      public DateTimeParse setTimeZone(TimeZone zone)
      Set the time zone for parsing date/time values.
      Parameters:
      zone - the time zone to use
      Returns:
      this object for method chaining