tags:

views:

206

answers:

2

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?

thanks in advance!

+2  A: 

check here for a solution http://stackoverflow.com/questions/1268174/phps-strtotime-in-java

it's basically a implementation of php's strtotime in java

Charles Ma
thanks, this more or less the thing I was trying to find!
airlocker
A: 

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 
        "AM","PM",
        "January","February","March","August"};
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++)
{
lx.add(datePatterns[i].toUpperCase()); 
}

   Iterator lit = lx.iterator();

    while(lit.hasNext())
    {    
    if (toCheck.contains((String) lit.next())
    {
    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.

Szyzygy
Thank you very much for your answer. You're right, the identification of the date is more difficult to address, and it seems to me that a simple knowledge based decision database should help
airlocker