Class DateTimeScanner

java.lang.Object
io.github.connellite.util.internal.dateparse.DateTimeScanner

public final class DateTimeScanner extends Object
Single-pass scanner that recognises the layout of a date-time string while reading it, instead of trying a list of patterns until one matches.

The scanner walks the input once, classifies every run of digits or letters, and assigns it to a field. Nothing is allocated for the input itself (no trim, no substring) and no exception is thrown when the input does not match: scan(java.lang.CharSequence, int, int, io.github.connellite.util.internal.dateparse.CalendarNames, io.github.connellite.util.internal.dateparse.ParsedFields) simply returns false.

Recognised shapes include ISO and RFC 3339 date-times with offset or bracketed zone, SQL timestamps, compact yyyyMMdd[THHmmss], numeric dates with a two-digit year resolved against a sliding window, localized month and weekday names, ordinal day suffixes, AM/PM markers and zone abbreviations. Day-month order in all-numeric dates is decided by orderDayAndMonth().

Based on araddon/dateparse: ParseAny and its iterRunes state machine, which records the start and length of every field while scanning and derives the layout from them rather than trying patterns in turn. Digit-by-index reading of ISO fields also follows Jackson's ISO8601Utils.parse and pydantic/speedate's get_digit!.

  • Method Details

    • scan

      public static boolean scan(CharSequence text, int start, int end, CalendarNames names, ParsedFields out)
      Scans text between start and end and fills out.
      Parameters:
      names - name tables to resolve month, weekday and AM/PM tokens with
      Returns:
      true when the whole range was recognised; when false is returned the contents of out are undefined and the caller must reset or discard it
    • scan

      public static boolean scan(CharSequence text, int start, int end, CalendarNames names, Boolean dayFirst, ParsedFields out)
      Same as scan(CharSequence, int, int, CalendarNames, ParsedFields), with the order of an ambiguous all-numeric date given explicitly.
      Parameters:
      dayFirst - true to read an ambiguous pair as day-month, false as month-day, or null to follow the locale
    • mayContainLocalizedNames

      public static boolean mayContainLocalizedNames(CharSequence text, int start, int end)
      Cheap test for whether consulting CalendarNames.global() could pay off: the input has to hold both a word that may be a month name and a number.