• Post Reply Bookmark Topic Watch Topic
  • New Topic

Strange behavior of java.text.DateFormat  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :

 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try with Jun 10, 2009. Notice the Jun instead of June
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chaitanya Brahma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper,

Thanks a lot for your quick and important input. The problem was resolved using SimpleDateFormat.

Thanks again,
bchaitanya
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!