| Author |
Strange behavior of java.text.DateFormat
|
Chaitanya Brahma
Greenhorn
Joined: Jun 05, 2009
Posts: 3
|
|
Hi Ranchers,
I have come across a real strange behavior of the class java.text.DateFormat. The code listed below executes properly on one Eclipse environment but gives "java.text.ParseException: Unparsable date" on another one!
Can anyone please throw some light as to what could be the possible issue here?
Thanks a ton in advance!
The code in question is as listed below -
Exception I get :
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8439
|
|
|
Try with Jun 10, 2009. Notice the Jun instead of June
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12928
|
|
I'm surprised that it works at all. The reason why this may or may not work depends on what version of Java you use, what the locale setting is and possibly other factors.
Looking at this line:
Format specifiers such as DateFormat.MEDIUM are really not meant to be used for parsing, but only for formatting dates. The Java API documentation does not even explain in detail what the format is when you use DateFormat.MEDIUM, so if you use this for parsing, you don't know what strings will be accepted and what will be rejected.
You'd better use SimpleDateFormat for parsing date strings, so that you can specify the format yourself, for example:
I don't know if this accepts all uppercase letters for the month name, such as "JUNE" in your example. See the documentation of SimpleDateFormat for more information on how to specify the format string.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Chaitanya Brahma
Greenhorn
Joined: Jun 05, 2009
Posts: 3
|
|
Hi Jesper,
Thanks a lot for your quick and important input. The problem was resolved using SimpleDateFormat.
Thanks again,
bchaitanya
|
 |
 |
|
|
subject: Strange behavior of java.text.DateFormat
|
|
|