Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet82/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   78   79   80   81   82   83   84   85   ...   245
Bog'liq
Java 17 Recipes

 How It Works
Time zones add yet another challenge for developers, and the Java Date-Time API 
provides an easy facet for working with them. The Date-Time API includes a java.time 
.zone package, which contains several classes that can assist in working with time zone 
data. These classes support time zone rules, data, and resulting gaps and overlaps in the 
local timeline that are typically the result of daylight savings conversions. The classes 
that make up the zone package are outlined in Table 
4-2
.
Table 4-2. Time Zone Classes
Class Name
Description
ZoneId
specifies zone identifier and is used for conversions
ZoneOffset
specifies a time zone offset from Greenwich/utC time
ZonedDateTime
a date-time object that also handles the time zone data
with time zone offset from Greenwich/utC time
ZoneRules
rules defining how a zone offset varies for a specified time zone
ZoneRulesProvider
provider of time zone rules to a particular system
ZoneOffsetTransition
transition between two offsets by a discontinuity in the
local timeline
ZoneOffsetTransitionRule rules expressing how to create a transition
Starting with the most fundamental time zone class, ZoneId, each time zone contains 
a particular time zone identifier. This identifier can be useful for assigning a particular 
time zone to a date-time. In the solution, the ZoneId class calculates any differences 
between two time zones. It identifies the rules that should be used for converting, based 
on a particular offset, either fixed or geographical region-based.
Chapter 4 Numbers aNd dates


136
ZonedDateTime is an immutable class that works with date-time and time zone data 
together. This class represents an object, much like LocalDateTime, which includes 
ZoneId. It expresses all facets of a date, including year, month, day, hours, minutes, 
seconds, nanoseconds, and time zone. The class contains a bevy of useful methods for 
performing calculations, conversions, and so on.
ZoneOffset specifies a time zone offset from Greenwich/UTC time. You can find the 
offset for a particular time zone by invoking the ZonedDateTime.getOffset() method. 
The ZoneOffset class includes methods that make it easy to break down an offset into 
different time units. For instance, the getTotalSeconds() method returns the total 
hours, minutes, and seconds fields as a single offset that can be added to a time.
Many rules can be defined for determining how zone offset varies for a single time 
zone. The ZoneRules class defines these rules for a zone. For instance, ZoneRules can 
be called on to specify or determine if daylight savings time is a factor. An Instant or 
LocalDateTime can also be passed to ZoneRules methods such as getOffset() and 
getTransition() to return ZoneOffset or ZoneOffsetTransition.
Another time zone class that is used often is ZoneOffsetTransition. This class 
models the transition between the spring and autumn offsets due to daylight savings 
time. It determines if there is a gap between transitions, obtaining the duration of a 
transition, and so on.
ZoneRulesProvider, ZoneOffsetTransitionRule, and other classes are typically not 
utilized as often as others for working with dates and time zones. These classes are useful 
for managing the configuration of time zone rules and transitions.
Note the classes within the java.time.zone package are significant, in that 
there are a multitude of methods that can be invoked on each class. this recipe 
provides a primer for getting started, with only the basics of time zone usage. For 
more detailed information, see the online documentation.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   78   79   80   81   82   83   84   85   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling