Question

Our Java application uses

TimeZone tz = TimeZone.getDefault()

Are there any other methods of obtaining the machine's time zone setting?

Background

Using TimeZone.getDefault() has worked very well for us for quite some time, in all the environments where our application runs: standalone Java application, inside Tomcat 5/6/7, on Windows XP, Windows 7 and Windows Server 2003/2008, 32 and 64-bit.

However, recently we have started running the application remotely via XenApp 6.5 on top of Windows 2008 R2. In this environment, the time zone our app sees is the server time zone, and not the time zone of the client, even though XenApp has been configured to perform "time zone redirection". We are running on Java 1.6.0 update 30.

Was it helpful?

Solution 3

It appears this is a known problem with Java and Citrix XenApp. Even IBM's Java based Lotus Notes client suffers from this problem. Unfortunately I could not find a solution anywhere.

I was able to work around the problem by writing a very simple C# program that gets the time zone. This correctly reads the client's time zone. I used the NodaTime library to read the time in tz database format, which is what Java uses to identify the time zones. Here is the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NodaTime;

namespace WindowsTimeZoneReader
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DateTimeZone.GetSystemDefault() + "," + DateTime.Now);
        }
    }
}

From my Java program I execute this C# program using Runtime.exec() and the techniques described here to read this program's output.

Once I have this I can then simply compare the time zone reported by Java and that reported by my C# program:

String windowsZone = ...; // The result of executing the above C# program
String javaZone = TimeZone.getDefault().getID();
if (!javaZone.equals(windowsZone)) {
  Java.setDefault( TimeZone.getTimeZone(windowsZone) );
}

All very convoluted, yes, but it appears to work.

OTHER TIPS

You can use calender object to create and get time zone from this object. Example:

TimeZone tz = Calendar.getInstance().getTimeZone();  
System.getProperty("user.timezone");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top