Raghavendar T S
Raghavendar T S

Reputation: 11

Java Day Light Saving (TimeZone/ZoneId) Support for Future Dates/Years

In Java there are two options which supports day light saving functionality. 1. TimeZone 2. ZoneId

Which class is better to use? The DST will differ for each year. How does Java classes maintain those configurations for search time zone and for each year? What is the max date or year for which the DST (zoneId.getRules().isDaylightSavings(instant)) will work?

Upvotes: 1

Views: 382

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 339342

java.time.ZoneId

Use ZoneId & ZoneRules, part of the modern java.time classes.

Avoid TimeZone, part of the terrible legacy date-time classes made obsolete by the adoption of JSR 310.

tzdata

Time zone information is defined in the tzdata stored in your JVM. Be sure to update if a zone you care about is changing.

Zones change

What is the max date or year for which the DST (zoneId.getRules().isDaylightSavings(instant)) will work?

Asking for the max date of future Daylight Saving Time (DST) is misguided.

The future changes in the offset used by the people of a particular region, such as Daylight Saving Time (DST) will be calculated indefinitely into the future according to the currently known rules.

The trick is that politicians around the world have shown a proclivity for redefining their respective time zones. This happens more often than you might think. So the rules by which we may want to plan our future scheduling are not actually knowable, as they are defined by capricious humans and not by nature.

This is why you should not book future appointments as a moment. Instead use the indefinite LocalDateTime class with a separately stored ZoneId. When calculating a schedule, apply the ZoneId to the LocalDateTime to get a ZonedDateTime object which is a specific moment. This has been covered many times already on Stack Overflow, so search for more info and code examples.


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.

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: 3

Related Questions