2

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.

Vineet Menon
  • 728
  • 2
  • 9
  • 25
David Sykes
  • 7,131
  • 4
  • 36
  • 39
  • Could you just ask it from the user? – heikkim May 09 '12 at 07:20
  • heikkim, yes, this is our last resort option if we can't get it to work otherwise. However, this is an advertised feature of Citrix XenApp, and it works in other non-Java applications. If we don't have to ask the user, we really don't won't to. – David Sykes May 10 '12 at 00:45
  • Most users really haven't got a clue what their timezone is, and when you look at the list it's not hard to see why! – Oversteer Jul 28 '12 at 15:00

3 Answers3

0

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

TimeZone tz = Calendar.getInstance().getTimeZone();  
Vijay Shanker Dubey
  • 4,308
  • 6
  • 32
  • 49
  • Thanks for the reply. I tried this and it returns the same as TimeZone.getdDefault(). Digging a little deeper it looks like Calendar gets the time zone by using TimeZone.getDefault(). – David Sykes May 09 '12 at 11:29
0
System.getProperty("user.timezone");
Renato
  • 12,940
  • 3
  • 54
  • 85
0

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.

David Sykes
  • 7,131
  • 4
  • 36
  • 39