Java8日期时间时区之 Temporal接口 笔记


前言

Temporal 是java8日期时间框架的一个接口 ,
Instant , LocalDate , LocalTime , LocalDateTime , ZonedDateTime ,OffsetDateTime都implements了它,

Temporal 的一些实现类

public final class Instant
        implements Temporal, TemporalAdjuster, Comparable<Instant>, Serializable {
public final class LocalDate
        implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable {
public final class LocalTime
        implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable {
public final class LocalDateTime
        implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable {
public final class ZonedDateTime
        implements Temporal, ChronoZonedDateTime<LocalDate>, Serializable {
public final class OffsetDateTime
        implements Temporal, TemporalAdjuster, Comparable<OffsetDateTime>, Serializable {
public final class OffsetTime
        implements Temporal, TemporalAdjuster, Comparable<OffsetTime>, Serializable {
public final class Year
        implements Temporal, TemporalAdjuster, Comparable<Year>, Serializable {
public final class YearMonth
        implements Temporal, TemporalAdjuster, Comparable<YearMonth>, Serializable {

它继承自 TemporalAccessor 接口

public interface Temporal extends TemporalAccessor {

接口说明及翻译

/**
 * Framework-level interface defining read-write access to a temporal object,
 * such as a date, time, offset or some combination of these.
 * <p>
 * This is the base interface type for date, time and offset objects that
 * are complete enough to be manipulated using plus and minus.
 * It is implemented by those classes that can provide and manipulate information
 * as {@linkplain TemporalField fields} or {@linkplain TemporalQuery queries}.
 * See {@link TemporalAccessor} for the read-only version of this interface.
 * <p>
 * Most date and time information can be represented as a number.
 * These are modeled using {@code TemporalField} with the number held using
 * a {@code long} to handle large values. Year, month and day-of-month are
 * simple examples of fields, but they also include instant and offsets.
 * See {@link ChronoField} for the standard set of fields.
 * <p>
 * Two pieces of date/time information cannot be represented by numbers,
 * the {@linkplain java.time.chrono.Chronology chronology} and the
 * {@linkplain java.time.ZoneId time-zone}.
 * These can be accessed via {@link #query(TemporalQuery) queries} using
 * the static methods defined on {@link TemporalQuery}.
 * <p>
 * This interface is a framework-level interface that should not be widely
 * used in application code. Instead, applications should create and pass
 * around instances of concrete types, such as {@code LocalDate}.
 * There are many reasons for this, part of which is that implementations
 * of this interface may be in calendar systems other than ISO.
 * See {@link java.time.chrono.ChronoLocalDate} for a fuller discussion of the issues.
 *
 * <h2>When to implement</h2>
 * <p>
 * A class should implement this interface if it meets three criteria:
 * <ul>
 * <li>it provides access to date/time/offset information, as per {@code TemporalAccessor}
 * <li>the set of fields are contiguous from the largest to the smallest
 * <li>the set of fields are complete, such that no other field is needed to define the
 *  valid range of values for the fields that are represented
 * </ul>
 * <p>
 * Four examples make this clear:
 * <ul>
 * <li>{@code LocalDate} implements this interface as it represents a set of fields
 *  that are contiguous from days to forever and require no external information to determine
 *  the validity of each date. It is therefore able to implement plus/minus correctly.
 * <li>{@code LocalTime} implements this interface as it represents a set of fields
 *  that are contiguous from nanos to within days and require no external information to determine
 *  validity. It is able to implement plus/minus correctly, by wrapping around the day.
 * <li>{@code MonthDay}, the combination of month-of-year and day-of-month, does not implement
 *  this interface.  While the combination is contiguous, from days to months within years,
 *  the combination does not have sufficient information to define the valid range of values
 *  for day-of-month.  As such, it is unable to implement plus/minus correctly.
 * <li>The combination day-of-week and day-of-month ("Friday the 13th") should not implement
 *  this interface. It does not represent a contiguous set of fields, as days to weeks overlaps
 *  days to months.
 * </ul>
 *
 * @implSpec
 * This interface places no restrictions on the mutability of implementations,
 * however immutability is strongly recommended.
 * All implementations must be {@link Comparable}.
 *
 * @since 1.8
 */
public interface Temporal extends TemporalAccessor {

框架级别的接口,定义对时间对象(例如日期,时间,偏移量或它们的某种组合)的读写访问。

这是日期,时间和偏移量对象的基本接口类型,这些对象足够完整以至于可以使用加号和减号进行操作。它是由可以以字段或查询形式提供和操作信息的类实现的。有关此版本的只读版本,请参见TemporalAccessor界面。

大多数日期和时间信息都可以用数字表示。这些信息是使用TemporalField建模的,其中数字使用长号来处理较大的值。年,月和月中的日期是字段的简单示例,但它们还包含即时和偏移量。有关标准字段集,请参阅ChronoField。

日期/时间信息不能用数字来表示,日期和时区这两个信息可以通过使用TemporalQuery定义的静态方法通过查询来访问。

此接口是框架级别的接口,不应在应用程序代码中广泛使用。相反,应用程序应该创建并绕过具体类型的实例,例如LocalDate。这有很多原因,部分原因是该接口的实现可能在ISO以外的日历系统中。有关java.time.chrono.ChronoLocalDate的信息,请参见java.time.chrono.ChronoLocalDate对问题进行更充分的讨论。

何时实施

如果一个类满足以下三个条件,则应实现此接口:
•根据TemporalAccessor,它提供对日期/时间/偏移信息的访问
•一组字段从最大到最小是连续的
•一组字段是完整的,因此不需要其他字段来定义表示的字段的有效值范围

有四个例子可以说明这一点:
•LocalDate实现此接口,因为它表示从天到永远都是连续的字段集,不需要外部信息即可确定每个日期的有效性。因此,它能够正确实现加/减。
•LocalTime实现此接口,因为它表示从纳秒到数天之内连续的一组字段,不需要外部信息即可确定有效性。通过绕一整天,它能够正确地实现加/减。
•MonthDay(年月和月日的组合)未实现此接口。虽然组合是连续的,但在几年中从几天到几个月不等,所以该组合没有足够的信息来定义月中某天的值的有效范围。因此,它无法正确实现加/减。
•周日和月日(“ 13日星期五”)的组合不应实现此接口。它不代表连续的字段集,因为几天到几周重叠几天到几月。


有 with plus minus until 方法

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kfepiza

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值