File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes SimpleDateFormat returns 4digit month Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "SimpleDateFormat returns 4digit month" Watch "SimpleDateFormat returns 4digit month" New topic

SimpleDateFormat returns 4digit month

Sam Kumar
Ranch Hand

Joined: Nov 30, 2001
Posts: 38
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.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
How are you displaying the erroneous results? Exactly what does such a result look like?

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Sam Kumar
Ranch Hand

Joined: Nov 30, 2001
Posts: 38
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.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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)

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: SimpleDateFormat returns 4digit month
It's not a secret anymore!