Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intelligent date / time parser for Java [closed]

Tags:

java

datetime

Is there some intelligent date / time parser library for Java? By intelligent I mean, that I don't need to specify the date / time format. The API should be similar to this:

Calendar cal = DateTimeParser.parse("01/06/10 14:55");
cal = DateTimeParser.parse("1 Jan 2009"); // assumes 00:00 time
cal = DateTimeParser.parse("1.2.2010");
cal = DateTimeParser.parse("kygyutrtf"); // throws exception

UPDATE:

// I'm telling the parser: "If unsure, assume US date format"
cal = DateTimeParser.parse("01/02/03", new Locale("en-us"));
like image 214
fhucho Avatar asked Nov 18 '10 15:11

fhucho


2 Answers

java.time

You can build a parser using DateTimeFormatterBuilder that can take care of case-insensitive parsing, optional patterns (specified inside square brackets), defaulting the missing fields (e.g. HOUR_OF_DAY) etc.

Demo:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.util.Locale;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        final DateTimeFormatter parser = new DateTimeFormatterBuilder()
                    .parseCaseInsensitive() // parse in case-insensitive manner                                     
                    .appendPattern("[M/d/uu[ H:m]][d MMM u][M.d.u][E MMM d, u]")
                    .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                    .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
                    .toFormatter(Locale.ENGLISH);
        
        // Test
        Stream.of(
                    "Thu Apr 1, 2021",
                    "THU Apr 1, 2021",
                    "01/06/10",
                    "1 Jan 2009",
                    "1.2.2010",
                    "asdf"
                ).forEach(s -> {
                    try {
                        System.out.println(LocalDateTime.parse(s, parser));
                    } catch(DateTimeParseException e) {
                        System.out.println("\"" + s + "\"" + " could not be parsed. Error: " + e.getMessage());
                    }
                });     
    }   
}

Output:

2021-04-01T00:00
2021-04-01T00:00
2010-01-06T00:00
2009-01-01T00:00
2010-01-02T00:00
"asdf" could not be parsed. Error: Text 'asdf' could not be parsed, unparsed text found at index 0

Learn more about the modern date-time API from Trail: Date Time.

like image 161
Arvind Kumar Avinash Avatar answered Sep 20 '22 03:09

Arvind Kumar Avinash


If you are asking for an intelligent date/time Parser then, check this one https://github.com/zoho/hawking. Devolped by ZOHO ZIA Team.

Hawking Parser is a Java-based NLP parser for parsing date and time information. The most popular parsers out there like Heidel Time, SuTime, and Natty Date time parser are distinctly rule-based. As such, they often tend to struggle with parsing date/time information where more complex factors like context, tense, multiple values, and more need to be considered.

With this in mind, Hawking Parser is designed to address a lot of these challenges and has many distinct advantages over other available date/time parsers.

It's a open source Library under GPL v3 and the best one. To know why it's best, check out this blog that explains in detail : https://www.zoho.com/blog/general/zias-nlp-based-hawking-date-time-parser-is-now-open-source.html

P.S: I'm one of the developers of this project

like image 27
Heisenberg Avatar answered Sep 20 '22 03:09

Heisenberg