How do I get the time difference from GMT for a specific date and time zone in Java?
Determining whether a specific time zone is in DST is quite straight-forward:
boolean isIsraelInDST = TimeZone.getTimeZone("Israel").inDaylightTime(new Date());
How do I get the actual time difference?
tl;dr
For the first moment of a certain date.
Avoid legacy date-time classes
The other Answers are outmoded, as the
TimeZone
class is now legacy. This and other troublesome old date-time classes are supplanted by the java.time time classes.java.time
Now we use
ZoneId
,ZoneOffset
, andZoneRules
instead of the legacyTimeZone
class.Specify a proper time zone name in the format of
continent/region
, such asAmerica/Montreal
,Africa/Casablanca
, orPacific/Auckland
. Never use the 3-4 letter abbreviation such asEST
orIST
as they are not true time zones, not standardized, and not even unique(!).Fetch the rules for that zone.
Ask the rules if Daylight Saving Time (DST) is in effect at a certain moment. Specify the moment as an
Instant
. TheInstant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).Not sure what you mean, but I will guess you are asking for the offset-from-UTC in effect at that moment for than zone. An offset is a number of hours, minutes, and seconds displacement from UTC. We represent an offset using
ZoneOffset
class. A time zone is a history of past, present, and future changes in offset used by the people of a particular region. We represent a time zone usingZoneId
class.Because the offset may vary over time for a region, we must pass a moment when asking for an offset.
Generate a String representing that offset in ISO 8601 standard format.
You can ask for the offset as a total number of seconds.
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.
Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor java.sql.* classes.
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.Use TimeZone.getRawOffset(): Returns the amount of time in milliseconds to add to UTC to get standard time in this time zone. Because this value is not affected by daylight saving time, it is called raw offset.
If you want the offset including DST then you use TimeZone.getOffset(long date). You should provide the concrete date to the method, eg now - System.currentTimeMillis()
I see this is an old thread - but adding this since I had a similar requirement recently - This gives the actual difference in millis based on the CURRENT time(millis from epoch).
I suggest to add summer/winter time offset to getRawOffset: