TL; DR
ZoneId.of( "Pacific/Auckland" ) // Specify a time zone. .getRules() // Get the object representing the rules for all the past, present, and future changes in offset used by the people in the region of that zone. .getOffset( Instant.now() ) // Get a `ZoneOffset` object representing the number of hours, minutes, and seconds displaced from UTC. Here we ask for the offset in effect right now. .toString() // Generate a String in standard ISO 8601 format.
+13: 00
At the first moment of a certain date.
ZoneId.of( "Pacific/Auckland" ) .getRules() .getOffset( LocalDate.of( 2018 , Month.AUGUST , 23 ) // Specify a certain date. Has no concept of time zone or offset. .atStartOfDay( ZoneId.of( "Pacific/Auckland" ) ) // Determine the first moment of the day on that date in that region. Not always `00:00:00` because of anomalies such as Daylight Saving Time. .toInstant() // Adjust to UTC by extracting an `Instant`. ) .toString()
12:00
Avoid obsolete time classes
Other answers are deprecated since the TimeZone class is now deprecated. This and other nasty old time classes are being superseded by the java.time time classes.
java.time
Now we use ZoneId , ZoneOffset and ZoneRules instead of the old TimeZone class.
Specify the time zone name in continent/region format, such as America/Montreal , Africa/Casablanca or Pacific/Auckland . Never use the abbreviation 3-4 letters, for example EST or IST , as they are not real time zones, and are not standardized and not even unique (!).
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
Get the rules for this zone.
ZoneRules rules = z.getRules() ;
Set the rules if Daylight Saving Time (DST) is in effect at a specific time. Specify the moment as Instant . The Instant class represents a moment on the UTC timeline with a nanosecond resolution (up to nine (9) decimal digits).
Instant instant = Instant.now() ; // Capture current moment in UTC. boolean isDst = rules.isDaylightSavings( instant ) ;
How to get the actual time difference?
Not sure what you mean, but I guess you are asking for offset-from-UTC at the moment for the zone. Offset is the number of hours, minutes, and seconds of offset from UTC . We represent the offset using the ZoneOffset class. A time zone is a story of past, present, and future changes in the displacement of people in a particular region. We present the time zone using the ZoneId class.
Since the offset can change over time for the region, we must convey the moment when we request the offset.
ZoneOffset offset = rules.getOffset( instant ) ;
Generate a string representing this offset in the standard ISO 8601 format .
String output output = offset.toString() ;
You can request the offset as the total number of seconds.
int offsetInSeconds = offset.getTotalSeconds() ;
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .
The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .
Using the JDBC driver compatible with JDBC 4.2 or later, you can exchange java.time objects directly with your database. No need for strings or java.sql. * Classes.
Where to get java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .