Java: Calculating time zone difference

守給你的承諾、 提交于 2020-05-25 04:18:05

问题


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?


回答1:


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




回答2:


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



回答3:


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?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

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.




回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/15788913/java-calculating-time-zone-difference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!