30.6.09

Searching A String For A Date with Java

The SimpleDateFormat class in java has a method 'parse' which returns a date. The 'parse' method can be used to sequentially attempt to find a date (in the specified simple date format) in a string beginning at the 0th character and ending at the last character. It will return the first valid date it finds.

A good way to do this is as follows:
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class searchStringForDate {

static public void main(String[] args) {

SimpleDateFormat sdf =
new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
String str =
"junkCharacters11/01/1999" +
"junkCharacters";
ParsePosition pos = new ParsePosition(0);
Date date = new Date();

for(int i = 0 ; i < str.length() ; i++)
{
pos.setIndex(i);

if(sdf.parse(str, pos) != null){
pos.setIndex(i);
date = sdf.parse(str, pos);
break;
}

}

System.out.println("date: " + date);
//and print date in simpleDateFormat
System.out.println("sdf date: " +
sdf.format(date));
}
}
OUTPUT:
date: Mon Nov 01 00:00:00 EST 1999
sdf date: 11/01/1999

.........................
A few things to note:

1: This isn't the most compact way to do this, it is instead set up so as to show the user more things about the nature of the methods. Can you figure out the more compact way?

2: This is not a good idea for searching through a string that may have numbers before the date in any format that is similar to your simple Date Format. Even though the parsing here is set not lenient it will still parse things like, 1/1/09, and give you an output like:
date: Tue Jan 01 00:00:00 EST 9
sdf date: 01/01/0009
I do not know of a way to parse only dates that are in the exact format you specify without first using a regex expression.

3: The break is there for a reason. If it isn't the program will continue past the start of the date in the string and it will get the last sequence of characters that could be interpreted to be a date, see "2:".

4: The 'setIndex(i)' after the 'if' is there for a reason also. Just testing to see if there is a date at the parse position causes the parse position to be set. (see ParsePosition class for more details)

5: Putting a - before any part of the date will result in that many months, days, or years being subtracted instead of added to the date.

6: Now you know how to find a date no matter how nerdy or ugly you are.

1 comment:

DominicWasserzug@gmail.com said...
This comment has been removed by the author.