Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SimpleDateFormat returns 4digit month

 
Sam Kumar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am seeing very strange problem with SimpleDateFormat. I am formatting java.util.Date into a timestamp value, here is the snippet:
SimpleDateFormat formatter =
new SimpleDateFormat("MM/dd/yyyy hh:mm:ss z");
formatter.format(new java.util.Date());
I saw twice 4digit month but it is not happening every time.
0001/31/2003 12:24:56 GMT+00:00
Any thoughts!
Thanks in adance.
SK
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you displaying the erroneous results? Exactly what does such a result look like?
 
Sam Kumar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am storing formatted timestamp value in static String variable which is initialized in a servlet on first access. I saw following resuly twice:
"0001/31/2003 12:24:56 GMT+00:00"
note the month as "0001" as oppose to "01". This is not happening always.
Thanks.
SK
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you need this value to be set once only on servlet initialization? If so, make the variable into a constant with final, and initializing it in a static initializer block.
This is just an idea that I would try myself (meaning it might not work)
-Barry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic