I'm trying to convert a date-string, like "14-05-2007", to a Date object using the SimpleDateFormat class and the SimpleDateFormat.parse method.
There are a few ways the date-string can be formated, and i need to get the correct correspondig date. This is the code i'm using atm:
Java Code:
package com.upjlib.audio.tags.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import com.upjlib.audio.tags.InvalidTagException;
}
if (date == null) {
try {
date = tryFormat(format, "yyyy-MM-dd'T'HH:mm:ss");
}
}
if (date == null) {
try {
date = tryFormat(format, "yyyy-MM-dd'T'HH:mm");
}
}
if (date == null) {
try {
date = tryFormat(format, "yyyy-MM-dd'T'HH");
}
}
if (date == null) {
try {
date = tryFormat(format, "yyyy-MM-dd");
}
}
if (date == null) {
try {
date = tryFormat(format, "yyyy-MM");
}
}
if (date == null) {
try {
date = tryFormat(format, "yyyy");
}
}
if (date == null) {
throw new InvalidTagException("Invalid date format found: "
+ format);
} else {
this.date.setTime(date);
}
}
return sdf.parse(format);
}
return date;
}
public static void main(String[] args) {
try {
System.
out.
println(df.
getDate().
getTime());
System.
out.
println(e.
getMessage());
}
}
}
When running this script, with an invalid date-string "12/", it still parses it as a valid date-string instead of throwing an InvalidTagException or ParseException. I found out that the last date-pattern ("yyyy") matches anything that is passed on to the SimpleDateFormat.parse method as long as it's not an empty String.
So now my question is: is there a way to get an exact match of the entered date-string and the date-pattern? It says in the manual that the SimpleDateFormat.parse method doesn't always use all characters of the entered date-string, so....
Thanks in advance