Ajay Yadav
Ajay Yadav

Reputation: 806

Java timezone ids are case-sensitive or platform dependent

On my mac the following code works correctly and returns a pacific timezone

TimeZone pacificTime = TimeZone.getTimeZone("america/los_angeles");

However on my ubuntu machine Java is not able to parse it and the variable pacificTime returns the GMT timezone. Can someone explain why this happens?

Upvotes: 2

Views: 772

Answers (1)

Ivaylo Toskov
Ivaylo Toskov

Reputation: 4021

My assumption is that your Ubuntu machine runs Java 8, while your mac runs Java 7.

The code works on your mac, because the method in Java 7 is not case sensitive, while in Java 8 it is. For this reason the timezone on your Ubuntu machine is simply not recognized and the method returns the default timezone, which is GMT.

In order to get the correct timezone on your Ubuntu, use the proper name of the timezone, which starts with an uppercase letter:

TimeZone pacificTime = TimeZone.getTimeZone("America/Los_Angeles");

Upvotes: 3

Related Questions