This is a very short tutorial on how to parse any string that even slightly resembles a date into java.util.Date (instant in time, with millisecond precision).
The best I found to do this, was to use Natty library written by Joe Stelmach.
As a prerequisite, either:
- Download the com.joestelmach.natty jar file and include it in your project: Click to Download: [natty-0.12.jar](http://search.maven.org/remotecontent?filepath=com/joestelmach/natty/0.12/natty-0.12.jar)
- Or, include the following dependency in your pom.xml file:
<dependencies>
<dependency>
<groupId>com.joestelmach</groupId>
<artifactId>natty</artifactId>
<version>0.12</version>
</dependency>
</dependencies>
- Then you can use following Java code to parse practically any date:
List<Date> dates = new Parser().parse("14 Aug 2003 00:03:19 +1200").get(0).getDates();
System.out.println(dates.get(0));
Above example code would return correct result in EDT format:
- Input string: 14 Aug 2003 00:03:19 +1200
- Result: Wed Aug 13 08:03:19 EDT 2003
This library is so flexible, that you can even try some really crazy date formats, such as:
- day before christmas 2015 at 8pm in EST - result: Thu Dec 24 20:00:00 EST 2015
- first thursday of may 2005 at 3:13 am pdt - result: Thu May 05 06:13:00 EDT 2005
You get the idea :)
Enjoy, hope this helps.