Adam Matan
Adam Matan

Reputation: 136141

Java: Calculating time zone difference

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?

Upvotes: 16

Views: 34880

Answers (5)

Basil Bourque
Basil Bourque

Reputation: 338211

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

For 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 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, and ZoneRules instead of the legacy TimeZone class.

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( "Africa/Tunis" ) ;  

Fetch the rules for that zone.

ZoneRules rules = z.getRules() ;

Ask the rules if Daylight Saving Time (DST) is in effect at a certain moment. Specify the moment as an Instant. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.now() ;  // Capture current moment in UTC.
boolean isDst = rules.isDaylightSavings( instant ) ;

How do I get the actual time difference?

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 using ZoneId class.

Because the offset may vary over time for a region, we must pass a moment when asking for an offset.

ZoneOffset offset = rules.getOffset( instant ) ;

Generate a String representing that offset in ISO 8601 standard format.

String output output = offset.toString() ;

You can ask for the offset as a total number of seconds.

int offsetInSeconds = offset.getTotalSeconds() ;

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.

Upvotes: 19

yogeshd
yogeshd

Reputation: 31

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))))
    );
}

Upvotes: 3

Andy Malakov
Andy Malakov

Reputation: 837

I suggest to add summer/winter time 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();

Upvotes: 14

Vinay Rao
Vinay Rao

Reputation: 1284

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).

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

Upvotes: 8

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

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()

Upvotes: 24

Related Questions