Date Parsing Bug???

B

b@zU

Can anybody explain me why the following code does NOT throws exceptios??

Date d=(new SimpleDateFormat("dd/MM/yyyy")).parse("01/01/200z");


look at the "z"
 
A

Ann

b@zU said:
Can anybody explain me why the following code does NOT throws exceptios??

Date d=(new SimpleDateFormat("dd/MM/yyyy")).parse("01/01/200z");


look at the "z"

NOT A BUG

Read the documentation, it says that only the "first part" of
the string may be used, and that the entire string does not
need to be used. In this case the parsing stops at the 'z'
and the result obtained is "Tue Jan 01 00:00:00 CST 0200"
(in Chicago time zone) Note that the year is 200 as the string
specifies.
HTH
Ann
 
S

Steve W. Jackson

Ann said:
::> Can anybody explain me why the following code does NOT throws exceptios??
:>
:> Date d=(new SimpleDateFormat("dd/MM/yyyy")).parse("01/01/200z");
:>
:>
:> look at the "z"
:
:NOT A BUG
:
:Read the documentation, it says that only the "first part" of
:the string may be used, and that the entire string does not
:need to be used. In this case the parsing stops at the 'z'
:and the result obtained is "Tue Jan 01 00:00:00 CST 0200"
:(in Chicago time zone) Note that the year is 200 as the string
:specifies.
:HTH
:Ann

The OP multi-posted and has received replies in another group.

But just to make myself feel better, CST isn't the "Chicago" time zone
but rather the "Central" time zone. Not that I have anything against
Chicago, but I don't live anywhere near it -- I'm way down South. :)

= Steve =
 
G

Grant Wagner

b@zU said:
Can anybody explain me why the following code does NOT throws
exceptios??

Date d=(new SimpleDateFormat("dd/MM/yyyy")).parse("01/01/200z");


look at the "z"

Simple:

<url:
http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html#parse(java.lang.String)
/>

Throws:
ParseException - if the beginning of the specified string cannot be
parsed.

Emphasis "_beginning_".

You aren't calling SimpleDateFormat#parse() (because there is only one
parameter), so NullPointerException can't be thrown. It could if you
used the overridden two parameter SimpleDateFormat#parse().

Anyway, the beginning of the string you are passing to
DateFormat#parse() can be parsed and produce a valid date, so Java
parses it until it reaches something which can not be parsed and it
returns January 1 of the year TWO HUNDRED. Move the "z" to the beginning
(...parse("z1/01/2000");), or even the beginning of the year
(...parse("01/01/z000")) and you'll get your exception.

Note that ...parse("01/01/2zzzzzzz") is also a valid date. It's January
1 of the year TWO.


So I guess the moral of the story is, after parsing, verify the Date is
in the range you want.
 

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,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top