Date parsing leniency

P

Paul J. Lucas

Given:

SimpleDateFormat df = "yyyy-MM-dd";
df.setLenient( false );
String s = "2007-02-04T10:45:21-08:00";
Date d = df.parse( s );
System.out.println( d );

I get:

Sun Feb 04 00:00:00 PST 2007

I want it to parse the *whole* string successfully, or fail. How to I get it to
*not* succesfully parse the given string?

- Paul
 
M

Mark Space

Paul said:
Given:

SimpleDateFormat df = "yyyy-MM-dd";
df.setLenient( false );
String s = "2007-02-04T10:45:21-08:00";
Date d = df.parse( s );
System.out.println( d );

I get:

Sun Feb 04 00:00:00 PST 2007

I want it to parse the *whole* string successfully, or fail. How to I
get it to *not* succesfully parse the given string?

Just from reading the Java doc, I see that there are two calls that take
a ParsePosition object, which is updated to indicate the position the
parser stopped at. I suppose you could check if the ParsePosition was
the same as the length of the string.
 
J

John B. Matthews

"Paul J. Lucas said:
No, no. I want the parse to *fail* using "yyyy-MM-dd".

Well, there's nothing wrong with the date up to that point in the format
string, so I'm not sure what would fail. As Mark Space suggested, you
might look at ParsePosition to see if there are unparsed characters
remaining.
 
M

mart

John said:
No, no. I want the parse to *fail* using "yyyy-MM-dd".

- Paul

So do you want to force that dates are entered only with yyyy-MM-dd
and fail a date that has also the time?
In that case I guess you could check the length of the string
(anything longer than 10 fails immediately) before even looking at the
date, that would be one level of validation.
sorry if I am stating the obvious.
 
A

Andreas Leitgeb

mart said:
So do you want to force that dates are entered only with yyyy-MM-dd
and fail a date that has also the time?

That's not too uncommon. I had a similar need recently.

My experience (though with times, not dates) was, that if what
you need is a fix-format parsing&validation of a user's string,
you're better off not to rely on the SimpleDateFormat for vali-
dation, but do some (e.g. regexp-based) validation checks before-
hand, and only use the SDF for building the Date object from the
already syntactically verified components.

Also note, that a format-specifier like "HH" can just as
legally consume any number of digits, not just exactly two.
 
T

Tom Anderson

Just from reading the Java doc, I see that there are two calls that take
a ParsePosition object, which is updated to indicate the position the
parser stopped at. I suppose you could check if the ParsePosition was
the same as the length of the string.

This sounds like the right solution.

However, you could also consider a foetid hack:

private static final String SENTINEL = "!!!";

private static Date parseStrict(String dateStr, String pattern) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat(pattern + SENTINEL);
df.setLenient(false);
return df.parse(dateStr + SENTINEL);
}

tom
 
P

Paul J. Lucas

John said:
Well, there's nothing wrong with the date up to that point in the format
string, so I'm not sure what would fail.

I simply want it to be an all-or-nothing deal: either the string entirely
matches the format, or it doesn't. Partial matches don't count.
As Mark Space suggested, you might look at ParsePosition to see if there are
> unparsed characters remaining.

Yeah, that's what I'm doing.

- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top