Determine Whether Daylight Savings Time (DST) is Active in Java for a Specified Date

前端 未结 5 1035
生来不讨喜
生来不讨喜 2020-11-29 04:29

I have a Java class that takes in the latitude/longitude of a location and returns the GMT offset when daylight savings time is on and off. I am looking for an easy way to

5条回答
  •  萌比男神i
    2020-11-29 04:44

    tl;dr

    ZoneId.of( "America/Montreal" )  // Represent a specific time zone, the history of past, present, and future changes to the offset-from-UTC used by the people of a certain region.  
          .getRules()                // Obtain the list of those changes in offset. 
          .isDaylightSavings(        // See if the people of this region are observing Daylight Saving Time at a specific moment.
              Instant.now()          // Specify the moment. Here we capture the current moment at runtime. 
          )                          // Returns a boolean.
    

    java.time

    Here is the modern java.time (see Tutorial) version of the correct Answer by mamboking.

    • A ZoneId represents a time zone. The class knows the rules that tell if DST applies to a particular time zone.
    • The ZoneRules class models all the historic and future transitions for a time-zone.
    • An Instant is a moment on the timeline in UTC.
    • A ZonedDateTime is the result of applying a ZoneId to an Instant.

    Example code:

    ZonedDateTime now = ZonedDateTime.now( ZoneId.of( "America/Montreal" ) );
    …
    ZoneId z = now.getZone();
    ZoneRules zoneRules = z.getRules();
    Boolean isDst = zoneRules.isDaylightSavings( now.toInstant() );
    

    Note how in the last line we had to extract an Instant object from our ZonedDateTime object with a simple call to toInstant.

    Table of date-time types in Java, both modern and legacy.


    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.

    You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - 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
      • Most 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 (<26), 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.

提交回复
热议问题