I found and reviewed a similar post from 2004 and my code seems to be similar with that found in that post. However, I am not receiving the results that I expected. When I test this code against an invalid time of 02:65:10 it validates it as being 03:05:10 where it increments the hour by 1 and decreases the minutes by 60. Following is my code. Any help is appreciated.
SimpleDateFormat sdf;
if (Pattern.matches(("([0-9]{2}[\\:][0-9]{2}[\\:][0-9]{2})"), fieldValue)) {
sdf = new SimpleDateFormat("HH:mm:ss");
} else if (Pattern.matches(("([0-9]{2}[\\.][0-9]{2}[\\.][0-9]{2})"), fieldValue)) {
sdf = new SimpleDateFormat("HH.mm.ss");
} else if (Pattern.matches(("([0-9]{2}[\\:][0-9]{2})"), fieldValue)) {
sdf = new SimpleDateFormat("HH:mm");
} else if (Pattern.matches(("([0-9]{2}[\\.][0-9]{2})"), fieldValue)) {
sdf = new SimpleDateFormat("HH.mm");
} else if (Pattern.matches(("([0-9]{6})"), fieldValue)) {
sdf = new SimpleDateFormat("HHmmss");
} else if (Pattern.matches(("([0-9]{4})"), fieldValue)) {
sdf = new SimpleDateFormat("HHmm");
} else {
return 3011;
}
try {
Date testTime = sdf.parse(fieldValue);
System.out.println(fieldValue + " " + testTime);
return 0;
} catch (ParseException e) {
e.printStackTrace();
return 3011;
}
Rod
SimpleDateFormat sdf;
if (Pattern.matches(("([0-9]{2}[\\:][0-9]{2}[\\:][0-9]{2})"), fieldValue)) {
sdf = new SimpleDateFormat("HH:mm:ss");
} else if (Pattern.matches(("([0-9]{2}[\\.][0-9]{2}[\\.][0-9]{2})"), fieldValue)) {
sdf = new SimpleDateFormat("HH.mm.ss");
} else if (Pattern.matches(("([0-9]{2}[\\:][0-9]{2})"), fieldValue)) {
sdf = new SimpleDateFormat("HH:mm");
} else if (Pattern.matches(("([0-9]{2}[\\.][0-9]{2})"), fieldValue)) {
sdf = new SimpleDateFormat("HH.mm");
} else if (Pattern.matches(("([0-9]{6})"), fieldValue)) {
sdf = new SimpleDateFormat("HHmmss");
} else if (Pattern.matches(("([0-9]{4})"), fieldValue)) {
sdf = new SimpleDateFormat("HHmm");
} else {
return 3011;
}
try {
Date testTime = sdf.parse(fieldValue);
System.out.println(fieldValue + " " + testTime);
return 0;
} catch (ParseException e) {
e.printStackTrace();
return 3011;
}
Rod