• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Date Format + Exception  RSS feed

 
mitesh vyas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using Simple Date Format to parse the string to Date.

It's working fine, but sometimes it give exception given below:

java.lang.NumberFormatException: For input string: "E152"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:394)
at java.lang.Long.parseLong(Long.java:452)
at java.text.DigitList.getLong(DigitList.java:149)
at java.text.DecimalFormat.parse(DecimalFormat.java:1068)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1705)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1156)
at java.text.DateFormat.parse(DateFormat.java:333)


My piece of code that parses string


public static final SimpleDateFormat DATE_FORMATER2 = new SimpleDateFormat("yyyy-MM-dd");

Date dtDocExpFrom=null;
String ldt1="2007-10-03";
Date dtDocExpFrom=sdf.parse(ldt1);

I have read some blogs, is this issue due to synchronization of SimpleDateFormat?


Please guide.
Thanks
Mitesh
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[mitesh]:

My piece of code that parses string


public static final SimpleDateFormat DATE_FORMATER2 = new SimpleDateFormat("yyyy-MM-dd");

Date dtDocExpFrom=null;
String ldt1="2007-10-03";
Date dtDocExpFrom=sdf.parse(ldt1);


That can't be your actual code, as it doesn't compile. Also sdf is never defined - was there more you wanted to show us?

[mitesh]: I have read some blogs, is this issue due to synchronization of SimpleDateFormat?

It might possibly be because SimpleDateFormat is not synchronized. If you are using multiple threads, and these threads can access a single SimpleDateFormat at the same time, then you might possibly get strange results like this.


If you run the code several times in a row, does it always error? Or are the errors seemingly random and unpredictable? Does the message always say

For input string: "E152"

or does the string change each time? If the problems are because of threading and synchronization issues, you will almost certainly see different results each time.
 
mitesh vyas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Yingst,

Thank for reply.

1. The piece of code is incomplete,i have posted to get idea of issues.

2. Regarding Synchronization, the applicaiton is used by multiple user and hence concurrency issue may exist, At present SimpleDateFormat object is creted and being utilized while parsing, I am thinking of making it local to the method where it is being called. I fee that should resolve my issue.

Any suggenstion will be welcomed.

Thank again.

Mitesh
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's a problem with multiple threads accessing the SimpleDateFormat, then yes, creating the SDF locally should fix it. There are a few other possible solutions (add syncronization, or use a ThreadLocal) but this is the simplest.
 
mitesh vyas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,


I am goind ahead with the local creation of SimpleDateFormat.In case i find error in live environment, will revert.

Thanks

Mitesh
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!