Java: time zone difference calculation

How to get time difference from GMT for a specific date and time zone in Java?

Determining if a particular time zone in DST is pretty straightforward:

boolean isIsraelInDST = TimeZone.getTimeZone("Israel").inDaylightTime(new Date()); 

How to get the actual time difference?

+6
source share
5 answers

Use TimeZone.getRawOffset (): Returns the amount of time in milliseconds to add in UTC to get the standard time in this time zone. Since this value is independent of daylight saving time, it is called the raw offset.

If you need an offset, including DST, you use TimeZone.getOffset (long date). You must specify a specific date for the method, for example now, System.currentTimeMillis ()

+14
source

I see that this is an old thread, but adding this, since I had a similar requirement recently - This gives a real difference in milliseconds depending on the current time (in milliseconds from the era).

  TimeZone.getTimeZone("America/New_York").getOffset(Calendar.getInstance().getTimeInMillis()) 
+7
source

I suggest adding a summer / winter offset to getRawOffset:

  TimeZone tz1 = TimeZone.getTimeZone("GMT"); TimeZone tz2 = TimeZone.getTimeZone("America/New_York"); long timeDifference = tz1.getRawOffset() - tz2.getRawOffset() + tz1.getDSTSavings() - tz2.getDSTSavings(); 
+7
source
 public String timeZone(Date date) { TimeZone timeZone = TimeZone.getTimeZone("America/New_York"); long millis = timeZone.getRawOffset() + (timeZone.inDaylightTime(date) ? timeZone.getDSTSavings() : 0); return String.format("%s%s:%s", millis < 0 ? "-" : "+", String.format("%02d", Math.abs(TimeUnit.MILLISECONDS.toHours(millis))), String.format("%02d", Math.abs(TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)))) ); } 
+2
source

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 .

0
source

All Articles