parsing a text date in java
To parse a text date in java you first need to create a SimpleDateFormat object with the pattern matching the format that the date is in, then just use the parse function to complete the parse.
The following method gives an example
public void parseDateFromString()
{
String convDate = "21/03/2009";
SimpleDateFormat format = new SimpleDateFormat("d/M/y");
try {
Date theDate = format.parse(convDate);
System.out.println("The date to parse was " + convDate);
System.out.println("The parsed date was " + theDate);
} catch (ParseException pe) {
System.out.println("Couldnt parse the date Error: " + pe);
}
}
The following method gives an example
public void parseDateFromString()
{
String convDate = "21/03/2009";
SimpleDateFormat format = new SimpleDateFormat("d/M/y");
try {
Date theDate = format.parse(convDate);
System.out.println("The date to parse was " + convDate);
System.out.println("The parsed date was " + theDate);
} catch (ParseException pe) {
System.out.println("Couldnt parse the date Error: " + pe);
}
}
| Next > |
|---|
