开发者

How do I get timezone information on macOS with Java?

开发者 https://www.devze.com 2023-03-30 17:30 出处:网络
How do I get timezone information on macOS (formerly known as 开发者_高级运维Mac OS X and OS X), using Java?The TimeZone class is used for time zones in Java. To get the system\'s default time zone, y

How do I get timezone information on macOS (formerly known as 开发者_高级运维Mac OS X and OS X), using Java?


The TimeZone class is used for time zones in Java. To get the system's default time zone, you can use the static getDefault method.


tl;dr

ZoneId
.of( "Africa/Tunis" ) 
.getRules()
.isDaylightSavings​( 
    Instant.now() 
)

java.time

The modern solution uses the java.time classes that supplanted the terrible legacy date-time classes: ZoneId and ZoneOffset and ZoneRules.

You asked:

How do I get timezone information on Mac OS X, using Java?

Get time zone info through Java on a Mac the same way as on any other host OS.

ZoneId

Define which time zone is of interest. Specify a proper time zone name in the format of Continent/Region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 2-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( "America/Montreal" ) ;

ZoneRules

Get the rules for that zone. Those rules include the history of past, present, and future changes to the offset used by the people of that particular region.

ZoneRules rules = z.getRules() ;

Interrogate for the rules in effect for any particular moment. Let's use the current moment for this demo.

Instant instant = Instant.now() ;  // Capture the current moment.

ZoneOffset

Get the offset in use at this moment in that zone. An offset is merely a number of hours-minutes-seconds ahead or behind UTC (the prime meridian).

ZoneOffset offset = rules.getOffset( instant ) ;

Daylight Saving Time (DST)

Ask if Daylight Saving Time (DST) is in effect at that moment in that zone.

boolean isDst = rules.isDaylightSavings​( instant ) ;

You can also ask for the previous and next transitions in that zone, and more.

tzdata

Be aware that macOS has its own copy of the tzdata data defining the time zones. Apple's OS updates include fresh copies. Be sure yours is up-to-date if a zone of interest to you is changing. This macOS copy affects most apps that are not Java-based.

A Java Virtual Machine (JVM) includes its own tzdata. Again, if a time zone of interest to you is changing, be sure any and all JVMs in your usage are updated. Java updates include fresh tzdata. But politicians really like changing time zones, often with little or no forewarning. So you may need to manually update your JVM(s)’ tzdata file. And be sure to update any JVMs you have installed as well as any JVMs bundled with any Java-based apps you are using.


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.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

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. Hibernate 5 & JPA 2.2 support java.time.

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 brought 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 (26+) bundle implementations of the java.time classes.
    • For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
      • If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….
0

精彩评论

暂无评论...
验证码 换一张
取 消