I have totally strange problem on one of my machines. I’ve written a program which is monitoring events from our servers and displays them on a monitoring pc. However, each event status message also contains a TIMESTAMP which is being retrieved by the following calls:
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
with
public static final String DATE_FORMAT_NOW = “yyyy-MM-dd HH:mm:ss”;
I’m expecting this call to return the local time, which is and was working great until today. But now my program shows me the local time +3 hours on every incoming event. A restart of the jar file does not help at all and the System Time is also set up correctly. This program is running on a machine which is synchronizing its time with an central corporate clock.
Can anyone explain me the mentioned behavior and/or state a possible solution which is not “restart the machine and try again” 🙂 ?
Cliffs:
– Retrival of Time is working on my- or any other machine when I start the Program
– The target Machine is running 24/7 and is only a monitoring machine, so no one is changing any options there
– It worked fine until today.
– All Time/Timezone settings on the Hostsystem (Windows XP) are correct. (checked via CMD -> “date” and also over the System Preferences)
I appreciate any kind of answer to this issue. Thank you for your time!
the Question is answered since the 25 already. I’ve implemented a explicit call of Timezone.setDefault() to make sure that the proper timezone is being shown the whole time. I’ll close this Question now.
Thank you all for your answers!
This code:
Returns a
Dateobject. ADateobject is simply a wrapper for the UTC time as along. In the code you showed us, it is just as correct to dosdf.format(new Date());What is the format
DATE_FORMAT_NOW? I assume you specify this yourself. Do you specify the timezone in this format?EDIT As you mentioned in your comment, DATE_FORMAT_NOW does not specify a timezone. You should do both
Timezone.getDefault()andsdf.getTimezone()to see if the value has changed to your locale + 3 hours.EDIT2 I found this forum post about the time sporadically changing. In this case it was caused by an Oracle driver calling
Timezone.setDefault(...)in the middle of a method, then setting it back afterwards.