Skip to main content

SimpleDateFormat does not recognize invalid YEAR

1 reply [Last post]
Joined: 2010-03-05


I have noticed a few thing about the way SimpleDateFormat tries to validate YEAR . Below is the sample example that i have used. if I repalace the year with values such as 0001, 9999 the output is true. I assume 0001, 9999 etc may be correct year if we take into consideration AD or BC associated with the year. But I hope this could be correct assumption ...

but what i dont understand is even when i pass values such as 00023, 100232 the SimpleDateFromat object does not throw exception instead it considers to be a valid YEAR. I think i am missing something while using SimpleDateFormat. I have tried GregorianCalendar object but it also fails to throw exeption for such values.

Why these classes even after setting format as ""yyyy-MM-dd" does not limit the YEAR to four values and why it such values are still valid year for them

Please share your view's on this.


public class Test {

public boolean isValidDate(String inDate) {

if (inDate == null)
return false;

//set the format to use as a constructor argument
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");


try {
//parse the inDate parameter
catch (ParseException pe) {
return false;
return true;

public static void main(String[] args) {

Test test = new Test();



Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-08-14

On the what: The spec is:

For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits.

I read this as:
a) All patterns with three or more "y"s are equivalent. yyy, yyyy, yyyyy, yyyyyy.
b) All integral decimal numbers (within the range) are valid: 0001, -3, 12345678.
I am not so sure what this spec says about "year 0".

If this is an appropriate abstraction for dates stands to reason. For example, one might argue if there is a Gregorian year like 100 AD. Or BC. Or a year 0.

For the why and how: If it is not sufficient for given case, one would have to add the validation elsewhere, but the contract of SimpleDateFormatter is not the place for changes.