SimpleDateFormat Bug

R

Rajesh Patel

I have a date format of MM-dd-yyyy and pass 12-12-198x into
parse, I expect this to be invalid. The following program
will print out:
Tue Dec 12 00:00:00 CST 0198.

I expect an exception, anybody know how to work around this?


import java.io.*;
import java.text.*;
import java.util.*;
public class SimpleDate {
public static void main(String s[]) throws Exception {
SimpleDateFormat formatter = new
SimpleDateFormat("MM-dd-yyyy");
formatter.setLenient(false);

Date date = formatter.parse("12-12-198x");
System.out.println(date);

}
}
 
C

Collin VanDyck

Rajesh Patel said:
I have a date format of MM-dd-yyyy and pass 12-12-198x into
parse, I expect this to be invalid. The following program
will print out:
Tue Dec 12 00:00:00 CST 0198.

I expect an exception, anybody know how to work around this?


import java.io.*;
import java.text.*;
import java.util.*;
public class SimpleDate {
public static void main(String s[]) throws Exception {
SimpleDateFormat formatter = new
SimpleDateFormat("MM-dd-yyyy");
formatter.setLenient(false);

Date date = formatter.parse("12-12-198x");
System.out.println(date);

}
}

Rajesh,

Why don't you pass it through a regular expression to guarantee the type of
input instead of relying on the Date implementation?

You can use a pattern similar to :

Pattern p = Pattern.compile("[0-9][0-9]?-[0-9][0-9]?-[0-9][0-9][0-9][0-9]");

and then use a matcher.

Matcher m = p.matcher("12-12-198x");

and then ask the matcher if it matches.

if (!m.matches()) {
throw InvalidDateException("Your input must be in the format
##-##-####");
}

-CV
 
R

Rajesh Patel

Exactly what I was thinking of doing, but hoping I would avoid it.

Collin said:
I have a date format of MM-dd-yyyy and pass 12-12-198x into
parse, I expect this to be invalid. The following program
will print out:
Tue Dec 12 00:00:00 CST 0198.

I expect an exception, anybody know how to work around this?


import java.io.*;
import java.text.*;
import java.util.*;
public class SimpleDate {
public static void main(String s[]) throws Exception {
SimpleDateFormat formatter = new
SimpleDateFormat("MM-dd-yyyy");
formatter.setLenient(false);

Date date = formatter.parse("12-12-198x");
System.out.println(date);

}
}


Rajesh,

Why don't you pass it through a regular expression to guarantee the type of
input instead of relying on the Date implementation?

You can use a pattern similar to :

Pattern p = Pattern.compile("[0-9][0-9]?-[0-9][0-9]?-[0-9][0-9][0-9][0-9]");

and then use a matcher.

Matcher m = p.matcher("12-12-198x");

and then ask the matcher if it matches.

if (!m.matches()) {
throw InvalidDateException("Your input must be in the format
##-##-####");
}

-CV
 
A

Anton Spaans

Rajesh Patel said:
I have a date format of MM-dd-yyyy and pass 12-12-198x into
parse, I expect this to be invalid. The following program
will print out:
Tue Dec 12 00:00:00 CST 0198.

I expect an exception, anybody know how to work around this?


import java.io.*;
import java.text.*;
import java.util.*;
public class SimpleDate {
public static void main(String s[]) throws Exception {
SimpleDateFormat formatter = new
SimpleDateFormat("MM-dd-yyyy");
formatter.setLenient(false);

Date date = formatter.parse("12-12-198x");
System.out.println(date);

}
}

Instead of public Date parse(String text), use this method:
public Date parse(String text, ParsePosition pos)On return, the 'pos'
instance tells you the position where the parsing ended. If this position is
not the end of the string, parsing did not go well.-- Anton.
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top