Class DateTimeParser


  • public class DateTimeParser
    extends java.lang.Object
    Analyze String data to determine whether input represents a date or datetime.

    Typical usage is:

     
    		DateTimeParser dtp = new DateTimeParser(false);
    
    		dtp.train("2/7/2012 06:24:47");
    		dtp.train("2/7/2012 09:44:04");
    		dtp.train("2/7/2012 06:21:38");
    		dtp.train("1/7/2012 23:16:14");
    		dtp.train("19/7/2012 17:49:53");
    
    		DateTimeParserResult result = dtp.getResult();
          // Expect "d/M/yyyy HH:mm:ss");
    		System.err.println(result.getFormatString());
     
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  DateTimeParser.DateResolutionMode
      When we have ambiguity - should we prefer to conclude day first, month first or unspecified.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.util.Set<java.lang.String> timeZones  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String determineFormatString​(java.lang.String input)
      Determine a FormatString from an input string that may represent a Date, Time, DateTime, OffsetDateTime or a ZonedDateTime.
      java.lang.String determineFormatString​(java.lang.String input, DateTimeParser.DateResolutionMode resolutionMode)
      Determine a FormatString from an input string that may represent a Date, Time, DateTime, OffsetDateTime or a ZonedDateTime.
      DateTimeParserResult getResult()
      Determine the result of the training complete to date.
      static java.time.format.DateTimeFormatter ofPattern​(java.lang.String formatString)
      Given an input string with a DateTimeFormatter pattern return a suitable DateTimeFormatter.
      static java.time.format.DateTimeFormatter ofPattern​(java.lang.String formatString, java.util.Locale locale)
      Given an input string with a DateTimeFormatter pattern return a suitable DateTimeFormatter.
      static boolean plausibleDateCore​(boolean lenient, int day, int month, int year, int yearLength)  
      java.lang.String train​(java.lang.String input)
      Train is the core entry point used to supply input to the DateTimeParser.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • timeZones

        protected static final java.util.Set<java.lang.String> timeZones
    • Method Detail

      • ofPattern

        public static java.time.format.DateTimeFormatter ofPattern​(java.lang.String formatString,
                                                                   java.util.Locale locale)
        Given an input string with a DateTimeFormatter pattern return a suitable DateTimeFormatter. This is very similar to DateTimeFormatter.ofPattern(), however, there are a set of key differences: - This will cache the Formatters - It supports a slightly extended syntax, the following are supported: - Year only - "yyyy" - S{min,max} to reflect a variable number of digits in a fractional seconds component - Year month only - "MM/YYYY" or "MM-YYYY" or "YYYY/MM" or "YYYY-MM" - The formatter returned is always case-insensitive
        Parameters:
        formatString - A DateTimeString using DateTimeFormatter patterns
        locale - Locale the input string is in
        Returns:
        The corresponding DateTimeFormatter (note - this will be a case-insensitive parser).
      • ofPattern

        public static java.time.format.DateTimeFormatter ofPattern​(java.lang.String formatString)
        Given an input string with a DateTimeFormatter pattern return a suitable DateTimeFormatter.
        Parameters:
        formatString - A DateTimeString using DateTimeFormatter patterns
        Returns:
        The corresponding DateTimeFormatter (note - this will be a case-insensitive parser).
        See Also:
        for more detail
      • train

        public java.lang.String train​(java.lang.String input)
        Train is the core entry point used to supply input to the DateTimeParser.
        Parameters:
        input - The String representing a date with possible surrounding whitespace.
        Returns:
        A String representing the DateTime detected (Using DateTimeFormatter Patterns) or null if no match.
      • getResult

        public DateTimeParserResult getResult()
        Determine the result of the training complete to date. Typically invoked after all training is complete, but may be invoked at any stage.
        Returns:
        A DateTimeParserResult with the analysis of any training completed, or null if no answer.
      • plausibleDateCore

        public static boolean plausibleDateCore​(boolean lenient,
                                                int day,
                                                int month,
                                                int year,
                                                int yearLength)
      • determineFormatString

        public java.lang.String determineFormatString​(java.lang.String input)
        Determine a FormatString from an input string that may represent a Date, Time, DateTime, OffsetDateTime or a ZonedDateTime.
        Parameters:
        input - The String representing a date with optional leading/trailing whitespace The resolution mode will default to the mode provided when the DateTimeParser was initially constructed.
        Returns:
        A String representing the DateTime detected (Using DateTimeFormatter Patterns) or null if no match.
      • determineFormatString

        public java.lang.String determineFormatString​(java.lang.String input,
                                                      DateTimeParser.DateResolutionMode resolutionMode)
        Determine a FormatString from an input string that may represent a Date, Time, DateTime, OffsetDateTime or a ZonedDateTime.
        Parameters:
        input - The String representing a date with optional leading/trailing whitespace
        resolutionMode - When we have ambiguity - should we prefer to conclude day first, month first or unspecified
        Returns:
        A String representing the DateTime detected (Using DateTimeFormatter Patterns) or null if no match.