Detect Date and Time Patterns in Strings

In this example you will learn how to detect date and time patterns in strings using DateTimePatternDetector class . From this you will be able to detect the pattern, fieldType, zone and be able to know if the string is a local date or local time.

Behind the scenes, DataPipeline uses this feature in its Dataset class to analyze possible date types and determine the best Java types to hold their values.

You can see other ways to analyze data here.

package com.northconcepts.datapipeline.foundations.examples.time;

import com.northconcepts.datapipeline.foundations.time.DateTimePatternDetector;
import com.northconcepts.datapipeline.foundations.time.DateTimePatternMatch;

import java.time.temporal.TemporalQueries;
import java.util.List;

public class DetectDateTimePatternsInStrings {

    public static void main(String[] args) {
        detectDateTimePattern("20");
        detectDateTimePattern("2001");
        detectDateTimePattern("2001-01");
        detectDateTimePattern("2001-01-12");
        detectDateTimePattern("03/04/05");
        detectDateTimePattern("22-03-1999 05:06:07");
        detectDateTimePattern("1997-07-16T19:20:30.45+01:00");
        detectDateTimePattern("Tuesday Nov 27 23:34:09 UTC 2018");
    }

    public static void detectDateTimePattern(String value){
        DateTimePatternDetector detector = new DateTimePatternDetector();
        List patterns = detector.matchAll(value);

        System.out.println("=======================================================================");
        System.out.println("Input: " + value);
        System.out.println("=======================================================================");

        if (patterns.size() == 0) {
            System.out.println("    ------ NO MATCHING PATTERNS ------");
            System.out.println();
            return;
        }

        for (DateTimePatternMatch pattern : patterns) {
            try {
                System.out.println("    Pattern:   " + pattern.getPattern());
                System.out.println("    LocalDate: " + pattern.getValue().query(TemporalQueries.localDate()));
                System.out.println("    LocalTime: " + pattern.getValue().query(TemporalQueries.localTime()));
                System.out.println("    Zone:      " + pattern.getValue().query(TemporalQueries.zone()));
                
                System.out.print("    FieldType: ");
                System.out.println(pattern.getFieldType());
            } catch (Throwable e) {
                System.out.println(e.getMessage());
            }
            System.out.println();
            System.out.println();
        }
    }
}

Code walkthrough

  1. The detectDateTimePattern() method will be used to detect if the string is a date.
  2. pattern.getPattern() returns the date pattern.
  3. getValue().query(TemporalQueries.localDate()) returns the local date.
  4. getValue().query(TemporalQueries.localTime()) returns the local time.
  5. getValue().query(TemporalQueries.zone()) returns zone.
  6. pattern.getFieldType() returns the field type of the string..

Console Output

=======================================================================
String: 20
    ------ NO MATCHING PATTERNS ------
=======================================================================
String: 2001
    Pattern:   yyyy
    LocalDate: null
    LocalTime: null
    Zone:      null
    FieldType: Unsupported temporalAccessor, it doesn't have a LocalDate or LocalTime
=======================================================================
String: 2001-01
    Pattern:   yyyy-M
    LocalDate: null
    LocalTime: null
    Zone:      null
    FieldType: Unsupported temporalAccessor, it doesn't have a LocalDate or LocalTime
=======================================================================
String: 2001-01-12
    Pattern:   yyyy-M-d
    LocalDate: 2001-01-12
    LocalTime: null
    Zone:      null
    FieldType: DATE
=======================================================================
String: 22-03-1999 05:06:07
    Pattern:   d-M-yyyy H:mm[:ss]
    LocalDate: 1999-03-22
    LocalTime: 05:06:07
    Zone:      null
    FieldType: DATETIME
=======================================================================
String: 1997-07-16T19:20:30.45+01:00
    Pattern:   yyyy-M-d'T'H:mm:ss.SSXXX
    LocalDate: 1997-07-16
    LocalTime: 19:20:30.450
    Zone:      +01:00
    FieldType: DATETIME
=======================================================================
String: Tuesday Nov 27 23:34:09 UTC 2018
    ------ NO MATCHING PATTERNS ------
Mobile Analytics