Wednesday, July 19, 2006

Calendar bug in Java

Hi all,
While extending a FileAppender class in Log4j, for some reason, I just did not want to use timestamp [%d] given by Log4j while logging the messages.

In my appender, I declared one Calendar instance which was an instance variable for the appender class. I used "Calender.getTime()" method to get current time for the logging message.

Compiled code, put the war file in container, started the application.
Things went fine and log messages were getting written to the file properly. Only problem I faced was about "DATE" in my log messages.
All the messages were showing same timestamp as a log time. :O

Went to appender and saw my code where I was putting CurrentTimestamp in the message before writing it to the file.
Went to check the documentation for Calendar.getTime() and documentation says the right thing as well.
Gets this Calendar's current time.
I was lost.. what could be the problem??

Was looking for some help/hint/thread but nothing was found.
Finally, somebody told me about the bug with Id #4479408. Sun claims that they have fixed this bug, but it doesn't look like that.
You can have a look at it by accessing following link.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4479408

So, for now, I am making a new instance of Calendar everytime I want to have get a Current Timestamp.
I am not very sure about this approach, how efficient is that, but we all know: "Deadline is sole responsible for bad software quality" and hence I had to put it in my code.

I hope this time Sun not only claim that they have fixed this bug, but really look into the issue and test the fix. :)

Thanks.