File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Strange behavior of java.text.DateFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Strange behavior of java.text.DateFormat" Watch "Strange behavior of java.text.DateFormat" New topic
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
    
    3

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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Strange behavior of java.text.DateFormat
 
Similar Threads
Problems with custom xsd:dateTime conversion
Date parsing issue
Validation check for the day using util.
Strange behaviour of java.text.DateFormat
Need to change Calendar to dd/mm/yyyy format