airlocker airlocker - 8 months ago 23
Java Question

Converting human readable date to java date object

Here is my problem - I need to parse a string and find whether it contains a date. Examples of strings:

"Tomorrow 2AM"
"16 February 2010 16:00"
"Today 16:00"

Do you know of any java library which addresses human readable dates conversion?


This is two questions, one regarding identification, the other conversion.

Identification of possible date context is (relatively) easy, conversion is however deeply problematic for a number of reasons, notably the sheer variety of ways one can semantically express a date, and the concomitant ambiguities and misrepresentations which can emerge. Let's look at some obvious pitfalls here:

  • "The day after tomorrow 2pm." If we simply evaluate on "tomorrow 2pm" we are going to be a day adrift.

  • "Two days after 16.10.2010".. It gets worse...

  • "not much before 9pm Friday 13th March 2013" We can get the date back but the context renders it very ambiguous.

Assuming you only want to identify a potential date match in a String you could do something like this...

private final String[] datePatterns = {"Yesterday","Today","Tomorrow", //etc
        "Sunday","Monday","Tuesday","Thursday","Friday", // etc
         "Lundi","Mardi","Mercredi", //etc in French
        "2001","2002", // all the years 
private List lx  = new ArrayList();

public boolean mayContainDates(String toCheck)
toCheck = toCheck.toUpperCase();

// irl we'd build this list 1 time in the constructor
for(int i = 0; i < datePatterns.length; i++)

   Iterator lit = lx.iterator();

    if (toCheck.contains((String)
    return true;

    return false;

It's really a question of how you set up your datePattern String array of comparators. Alternatively you could iterate an array of regexps in a somewhat similar fashion but it would be slow. You'll probably get a lot of false positives but you can obviously improve on this simple model.