3

If run this code in windows machine, its works properly:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date()));

It shows date & time same as clock, but the same code if I run on Windows Server it's showing -2hrs of clock time.

I tried by the setting the timezone as,

TimeZone timeZone = TimeZone.getTimeZone("Europe/Vienna");
sdf.setTimeZone(timeZone);
Then, it shows same as clock time.

My doubt is why is it not taking the clock time zone by default in Windows Server.

Jesse
  • 8,605
  • 7
  • 47
  • 57
user2053989
  • 55
  • 3
  • 6

4 Answers4

3

It should always take the time zone of the running machine but in some case it fails as reported here and here. If you are sure that your server time zone is correct but Java uses a different one, you can force a default time zone of your choice from the command line:

-Duser.timezone="YOUR_TIME_ZONE_HERE"

or by code. You can also try with the Timezone Updater Tool by Oracle.

To see the time zone used by Java:

System.out.println(TimeZone.getDefault());
Pino
  • 7,468
  • 6
  • 50
  • 69
  • Even i tried with this, in command prompt,systeminfo | findstr /C:"Time Zone . It shows the same time time zone as clock i,e Time Zone: (UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna. But for java code to show correct time i need to set the time zone explicity. That is problem i m facing. – user2053989 Apr 30 '13 at 14:50
  • Thanks alot for the reply, i tried changing the Timezones of the system clock setting, its didn't work. I am not not able to change the time as i donot have admin right. I donot want to give specific time zone by hard coding or argument, it has to take the system time. Because requiremet is to record the log files. They always check the timing against system. I will check Timezone updater tool. – user2053989 Apr 30 '13 at 15:25
2

Time zone settings have no effect on a time value as the computer sees it. See my answer here for details. So long as the system time is set properly (regardless of time zone) and you don't need to display the time as text or parse the time from text, java will have no trouble and its calculations will be correct. If you do need to display/parse a time value, setting the default time zone is safe and it won't effect how times are stored, except of course Java will use that time zone to parse text representations of time that don't explicitly specify the time zone (like if you used the SimpleDateFormat in your example to parse, since the format string doesn't include the time zone).

As for why Java has this problem on your server box but not your desktop box, what version of Windows Server is it, and what version Java is running on the server? My guess is that the JVM can't unambiguously identify the OS version and therefore can't know for sure how to get the local time zone, so uses UTC as a fallback. Try updating your java version to the latest.

Another possibly is that the time zone on the server is set on a per-user basis. Verify that the time zone for the user/process that is running the app on the server is set properly.

Community
  • 1
  • 1
Alvin Thompson
  • 5,388
  • 3
  • 26
  • 39
  • In my desktop, timezone setting shows as GMT, but in windows server it UTC. And for System.out.println(TimeZone.getDefault()) this line on server machine displays as Default Timezone is : sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]. Id is GMT, will that could be a problem?? – user2053989 May 02 '13 at 09:11
  • Its windows server 2008 R2 standard.java version jdk1.5, desktop java version is 6. – user2053989 May 02 '13 at 10:48
  • Ah! JDK 1.5 came out 9 years ago, so possibly doesn't know about windows server 2008. What's the full `java -version` output, including build number? Although really it doesn't matter; you still won't have any problems unless converting to/from text. How and where is that handled for the app? – Alvin Thompson May 03 '13 at 14:06
  • Thanks a lot Alvin. I used java version 1.6 and it is working. Thanks a lot for your input. – user2053989 May 06 '13 at 11:21
  • But java 6 also previous version of windows server. but its working with java 6 version. – user2053989 May 06 '13 at 11:36
  • You're welcome! I'm sure one of the updates to JDK 6 added knowledge of WS 2008. – Alvin Thompson May 06 '13 at 20:39
1

There is a bug open at Oracle about that problem: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7044727. Basically it's because Java pulls the timezone out of the registry. And Windows saves it in HKLocalMachine, which is not the Client time but the time of the machine.

Calon
  • 4,174
  • 1
  • 19
  • 30
1

I had this issue after upgrading from 2003 server to 2008 server. Java keeps using GMT timezone instead of Windows Timezone setting. The solution is simple, go to Windows control panel and change to a different Timezone(whatever timezone is fine), then change it back. Took me hours to figure it out but it worked as charm.

My thought: the change triggerred something in Java, which correct the issue.