Make SimpleDateFormat.parse() fail on invalid dates (e.g. month is greater than 12)
You need to make it non-lenient. Thus,
format.setLenient(false);
should do it.
Why does SimpleDateFormat parse incorrect date?
You should use DateFormat.setLenient(false)
:
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
df.setLenient(false);
df.parse("03/88/2013"); // Throws an exception
I'm not sure that will catch everything you want - I seem to remember that even with setLenient(false)
it's more lenient than you might expect - but it should catch invalid month numbers for example.
I don't think it will catch trailing text, e.g. "03/01/2013 sjsjsj". You could potentially use the overload of parse
which accepts a ParsePosition
, then check the current parse index after parsing has completed:
ParsePosition position = new ParsePosition(0);
Date date = dateFormat.parse(text, position);
if (position.getIndex() != text.length()) {
// Throw an exception or whatever else you want to do
}
You should also look at the Joda Time API which may well allow for a stricter interpretation - and is a generally cleaner date/time API anyway.
Make SimpleDateFormat.parse() fail on invalid dates (e.g. month is greater than 12)
You need to make it non-lenient. Thus,
format.setLenient(false);
should do it.
Weird date parsing in Java
Try this (before parsing the actual date):
sdf.setLenient(false);
See also:
- Make SimpleDateFormat.parse() fail on invalid dates (e.g. month is greater than 12)
Incorrect date parsing using SimpleDateFormat, Java
Have you tried calling setLenient(false)
on your SimpleDateFormat
?
import java.util.*;
import java.text.*;
public class Test {
public static void main(String[] args) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setLenient(false);
Date date = format.parse("2011-06-1211"); // Throws...
System.out.println(date);
}
}
Note that I'd also suggest setting the time zone and locale of your SimpleDateFormat
. (Alternatively, use Joda Time instead...)
validating a date using dateformat
It's by default lenient. You need to set it non-lenient before parsing.
ft.setLenient(false);
This will result in a ParseException
when an invalid day and/or month is been supplied as compared to the year. Note that this doesn't validate the year itself as basically any number of years is valid. Depending on the concrete functional requirement, you might want to do an additional check on the year, e.g. split string on /
and then check if the length of the 1st part is 4.
Why does SimpleDateFormat.parse accept an invalid date string?
From a glance of the javadocs, it seems to be using a lenient
mode of parsing in which it accepts poorly formatted input:
Adding this line
format.setLenient(false);
after the initialization of your DateFormat object appears to fix it.
Related Topics
How to Specify the Default Jvm Arguments for Programs I Run from Eclipse
How to Concatenate Characters in Java
Convert Xml to Java Object Using Jaxb (Unmarshal)
Java 8 Localdatetime Is Parsing Invalid Date
How to Convert Words to a Number
How to Inject Module Declaration into Jar
Making Spring-Data-Mongodb Multi-Tenant
Often Big Numbers Become Negative
Why Does Getrealpath() Return Null When Deployed with a .War File
Spring Security's Securitycontextholder: Session or Request Bound
Why Doesn't Java.Util.Collection Implement the New Stream Interface
When to Use a List Over an Array in Java
N-Gram Generation from a Sentence
Why Is 09 "Too Large" of an Integer Number
How Does the Enhanced for Statement Work for Arrays, and How to Get an Iterator for an Array
How to Ignore Pkix Path Building Failed: Sun.Security.Provider.Certpath.Suncertpathbuilderexception