The Answer by Jon Skeet is correct and should be accepted. You cannot reliably determine a time zone from an offset-from-UTC as many zones may share an offset coincidentally. Also, the offset for a zone likely varies over time.
Here are some code samples addressing the issue using more modern classes than that seen in the Question.
java.time
You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.
As others stated, if you know the intended time zone, always use that zone in preference to a mere offset-from-UTC. An offset is simply a number of hours, minutes, and seconds ahead of, or behind, UTC. A time zone is a history of changes to the offset used by the people of a region. A time zone knows the past, present, and (tentatively) the future of such changes in offset.
The ZoneId
and ZoneOffset
classes replace TimeZone
.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Los_Angeles" ) ; // Or "Africa/Tunis", "Pacific/Auckland", etc.
Get the current moment as seen through the lens of that zone.
ZonedDateTime zdt = ZonedDateTime.now( z ) ; // Fetch current moment for that zone.
Extract that same moment but adjusted into UTC.
Instant instant = zdt.toInstant() ; // Extract the same moment but in UTC.
Adjust that some moment into another zone.
ZonedDateTime zdtKolkata = instant.atZone( ZoneId.of( "Asia/Kolkata" ) ) ; // Determine same moment, same point on timeline, but in another time zone.
All three of these objects represent the very same simultaneous point on the timeline, but viewed with a different wall-clock time.
See the offset used by the people of that region at that moment.
ZoneOffset offset = z.getRules().getOffset( instant ) ; // Get the offset in place at that moment for that time zone.
You will find that for 2018, in part of that year the offset for America/Los_Angeles
will be -07:00
(sever hours behind UTC). In the other part of the year, the offset will be -08:00
(eight hours behind UTC). This change in offset is due to politicians’ decision to observe Daylight Saving Time (DST).
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
TimeZone.getTimeZone(String)
depends upon the current time is incorrect. – Andy TurnerGMT-07:00
is an offset (difference from UTC), whileAmerica/Los_Angeles
is a timezone (set of all offsets a region uses during history), read more about the difference here. As already said, there's more than 1 timezone that uses the same offset, so only by having GMT-7, you can't say which timezone is – user7605325