• Post Reply Bookmark Topic Watch Topic
  • New Topic

problems with SimpleDateFormat  RSS feed

 
verduka fox
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very confused. I have used SimpleDateFormat in the past and never had any problems. Now I am. I send a GregorianCalendar to a static method to format the date. Inconsistently, it will sometimes return the date as 0012/0023/1986 -- it puts unnecessary zeroes in the date. I cannot determine a pattern or a reason for this. I do not understand what is happening. The following code is from a class in my utility package to format dates. Your comments and suggestions are GREATLY appreciated.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works fine for me.
Can you show an example of using this that does not work correctly?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just ran this test, and I could not replicate your experience.It never says "It happened!".
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose that you could check the result for sequences of zeros and remove them as necessary before it's displayed. It seems a shame to do that though, as one would hope the date formatter to take care to format things properly.
I wonder if there is a chance that something is unusually about your JRE installation. Which one are you running?
 
verduka fox
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Thanks for your replies. I'm using WebSphere Application Server 4.0.3 on AIX. I can't show you an example of when this works or doesn't work: it's sporadic. Most of the time it works correctly, but then sometimes it will display the leading zeroes. There is not one case that is guaranteed to reproduce these results.
Another problem is that I use this code to format two different dates per record. When I display the dates, sometimes the date that is displayed is not the date at all. Sometimes the two dates seems intermixed (month and year of date 1 are displayed with the year of date 2). Sometimes the dates are nowhere near what they are supposed to be.
All in all, I'm receiving really strange results. I don't understand what is happening. As I mentioned earlier, this code is in our utility package. My co-worker also uses this utility package and this exact code in his apps, and he has never seen either of these problems occur in his apps. We use similar code. The only differences are:
I retrieve dates from the database using getTimestamp:

with Converter.convertDateToGregorianCalendar() (also in our utility package) as:

------------------------------------
I have 2 methods that call my stored procedure in different ways. Both of these methods extract the data from the resultset by accessing a private method:

Is there perhaps a problem with this code? I asked in the JDBC forum of JavaRanch if it was "ok" to handle the resultset in a different method as the callable statement code and was told this was not a problem. If anyone can shed a light on this, I would really appreciate it. This problem is delaying our production release. Your comments and suggestions are welcomed!
Thanks in advance.
 
verduka fox
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still trying to figure this out. I opened a case with IBM and they believe that the bad date formats are caused by threading issues:

The testcase can easily be adapted to reproduce the problem - simply by making it multi-threaded, and launching a reasonable number of threads on a
multi-processor system, I can make it fail in the manner you describe.
This is not an issue with either the JVM or the class libraries - both are working as designed and intended. The issue is your use of a static
SimpleDateFormat object in a multi-threaded context without any synchronization being applied. The SimpleDateFormat class is not "thread-safe" (its methods are not synchronized and have no provision for simultaneous use by multiple threads) and it is therefore necessary to either a) add the "synchronized" clause to your formatDate() method, or b) arrange that threads have private copies (perhaps using clone()) of the
SimpleDateFormat you wish to use.

I have done some reading on the use of synchronized, but I'm not entirely clear on this topic. Can anyone provide me with an explanation of when synchronization needs to be used? I need to avoid this problem in the future.
Please comment. Thank you.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!