natty
Natty is a natural language date parser written in Java. Given a date expression, natty will apply standard language recognition and translation techniques to produce a list of corresponding dates with optional parse and syntax information. Give it a try.
Natty recognizes dates described in many ways, including formal dates (02/28/1979), relaxed dates (oct 1st), relative dates (the day before next thursday), and even date alternatives (next wed or thurs).
Natty is also able to search for date components within a larger block of text, detect their structure, and report line and column information.
Take a look at the documentation for a complete description, and let us know if natty doesn't recognize something it should.
Getting started with natty is easy:
-
Include the following dependency in your pom.xml file:
<dependency> <groupId>org.natty</groupId> <artifactId>natty</artifactId> <version>1.1.2</version> </dependency>If you're not using Maven, you can find other snippets or download the jar manually via: maven central.
-
Use the
Parserclass to parse your input string into a list ofDateGroup's. EachDateGroupcontains a list ofDate's, the original value and location of the matching text, granular parse and abstract syntax tree information, and simple recurrence information.import org.natty.*; Parser parser = new Parser(); List<DateGroup> groups = parser.parse("the day before next thursday"); for(DateGroup group:groups) { List<Date> dates = group.getDates(); int line = group.getLine(); int column = group.getPosition(); String matchingValue = group.getText(); String syntaxTree = group.getSyntaxTree().toStringTree(); Map<String, List<ParseLocation>> parseMap = group.getParseLocations(); boolean isRecurring = group.isRecurring(); Date recursUntil = group.getRecursUntil(); }
Keep in mind that natty could probably be improved on. If you'd like to leave some feedback, you can submit an issue on GitHub. If incompatible changes are made to the API, the major version will be increased. The major version may also be increased for other impactful updates. E.g. when requiring newer java version (currently still at 8).