Deepak Chaudhry
Deepak Chaudhry

Reputation: 219

In java getting different value for TimeZone.getDefault().getID() on different system

I am getting different value for TimeZone.getDefault().getID() on different systems. For example, in case of Indian standard time, On one of the system we are getting "GMT+5:30": while on another we are getting "Asia/Calcutta". We are expected to get "Asia/calcutta" strings on all machines.

Why is there an inconsistency for such behavior?

Is there any way to get consistent behavior across different systems windows/MAC?

What is the best way to get client time zone programmatically using Java?

Upvotes: 2

Views: 2085

Answers (2)

Camusensei
Camusensei

Reputation: 1563

In case someone has the same problem on Linux and can't find an answer:

  • Two linux machines set to UTC
  • one JVM reporting incorrect timezone (ex CET)
  • the other reporting correctly UTC

The problem could be:

  • The faulty machine has this symlink: /etc/localtime -> /usr/share/zoneinfo/Europe/Paris
  • /usr/share/zoneinfo/Europe/Paris contains UTC information

The cause for this could have been a simple cp /usr/share/zoneinfo/UTC /etc/localtime replacing the contents of /usr/share/zoneinfo/Europe/Paris instead of overwriting /etc/localtime which was a symlink instead of a file!

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500515

It sounds like the two machines are configured differently - it's as simple as that. For example, if your first system is Windows you may have unticked the box saying "Automatically adjust for Daylight Saving Time".

If you know you need Asia/Calcutta for all systems, then use that explicitly. I try to avoid using the default time zone wherever possible, to be honest. Even when I do use it, I try to use it explicitly so that it's obvious from the code that I'm trying to use the system time zone, rather than it just being accidental.

Upvotes: 3

Related Questions