简体   繁体   中英

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?

check here for a solution PHP's strtotime() in Java

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM