Skip to the content.

首页

version: jdk17


java.util.Date

时间戳,为可变对象,只建议作为PO类的属性使用,对应数据库的TIMESTAMP和DATETIME类型。


Temporal Object(时间对象)

java.time包下所有日期时间类均实现了Temporal、TemporalAdjuster、Comparable和Serializable,均是不可变的,也即线程安全的,这也意味着所有修改操作均会返回新对象,所以要尽量合并修改操作以减少不必要的对象创建。

TemporalField

时间字段接口,实现为ChronoField枚举,表示以何种方式对时间对象进行描述,如month-of-year、minute-of-hour等。

TemporalAccessor

时间对象只读访问接口。

// 是否支持给定TemporalField
boolean isSupported(TemporalField field);c

// 返回使用给定TemporalField描述的值
int get(TemporalField field); // 默认使用getLong方法实现
long getLong(TemporalField field);

TemporalUnit

时间单位接口,实现为ChronoUnit枚举,表示以何种方式对事件对象进行测量,如年、月、日、小时、分、秒。

Temporal extends TemporalAccessor

时间对象读写访问接口。

// 是否支持给定TemporalUnit
boolean isSupported(TemporalUnit unit);

// 将按给定TemporalField描述的值调整为给定值,不是修改原对象而是返回新对象。
Temporal with(TemporalField field, long newValue);

// 使用给定的TemporalAdjuster调整当前时间
default Temporal with(TemporalAdjuster adjuster) {
    return adjuster.adjustInto(this);
}

// 按给定TemporalUnit计算直到另一个时间(不包含)的时间量,正数则表示是之后的时间。
long until(Temporal endExclusive, TemporalUnit unit);

TemporalAdjuster

表示调整时间的策略,所有时间类都实现了该接口,即时间对象自身也可以作为时间调整策略

// 调整给定的时间,谨慎使用,如果字段不支持会抛出异常。
Temporal adjustInto(Temporal temporal);

// 实现类都是用with方法实现,故都是返回新对象。
default Temporal adjustInto(Temporal temporal) { // ChronoLocalDate
    return temporal.with(EPOCH_DAY, toEpochDay());
}

TemporalAdjusters

时间调整策略工具类,返回公共的常用TemporalAdjuster对象。

TemporalAmount

时间量接口,表示一段以特定时间单位测量的时间量,如‘6小时’、‘8天’、‘两年零三个月’等。

// 当前时间量以给定时间单位测量出的值
long get(TemporalUnit unit);

// 当前时间量使用的所有时间单位
List<TemporalUnit> getUnits();

final class Duration implements TemporalAmount

以秒和纳秒为单位测算的时间量。

public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    // 使用Temporal接口的until方法
    try {
        // 首先使用纳秒单位计算
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        // 抛出异常后再使用秒单位计算
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos; // 使用TemporalAccessor接口的getLong方法获取纳秒值
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            ...
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos); // 构造Duration对象
    }
}

返回两个时间对象之间以Duration表示的时间量。

final class Period implements ChronoPeriod

以年月日为单位测算的时间量。

public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive) {
    return startDateInclusive.until(endDateExclusive);
}

返回两个LocalDate之间以Period表示的时间量。


abstract class Clock

特定时区的不可变的时钟对象,用于获取Instant,需要通过静态工厂方法创建。


Instant

精确到纳秒的瞬间,用于替代TimeStamp,是java.util.Date与java.time包日期时间类之间转换的桥梁(toInstant方法和ofInstant方法)。


time-zone

abstract class ZoneId implements Serializable

时区信息,抽象父类。

final class ZoneRegion extends ZoneId

非公共类,包含地区信息以及支持夏令时调整,只能通过ZoneId的静态方法返回。

final class ZoneOffset extends ZoneId implements TemporalAccessor, TemporalAdjuster, Comparable, Serializable

与UTC的时区偏移,如+02:00,可以指定任意的偏移量(-18:00 ~ +18:00)。

OffsetDateTime & OffsetTime

可以指定偏移量的DateTime和Time,应该用于日期存储或网络通信

// OffsetDateTime
private final LocalDateTime dateTime;
private final ZoneOffset offset;

public static OffsetDateTime of(LocalDateTime dateTime, ZoneOffset offset) {
    return new OffsetDateTime(dateTime, offset);
}

public static OffsetDateTime of(LocalDate date, LocalTime time, ZoneOffset offset) {
    LocalDateTime dt = LocalDateTime.of(date, time);
    return new OffsetDateTime(dt, offset);
}
// OffsetTime
private final LocalTime time;
private final ZoneOffset offset;

public static OffsetTime of(LocalTime time, ZoneOffset offset) {
    return new OffsetTime(time, offset);
}

final class ZonedDateTime implements Temporal, ChronoZonedDateTime, Serializable

ZonedDateTime拥有时区信息并支持夏令时调整,偏移量是由时区信息控制,适合用于用户显示日期。

private final LocalDateTime dateTime; // 本地时间
private final ZoneOffset offset; // 由时区控制的偏移量
private final ZoneId zone; // 时区信息

local

LocalDate包含年、月、日;LocalTime包含时、分、秒、纳秒;LocalDateTime包含LocalDate和LocalTime。

// LocalDate转LocalDateTime
public LocalDateTime atStartOfDay() {
    return LocalDateTime.of(this, LocalTime.MIDNIGHT);
}

// LocalDate转ZonedDateTime
public ZonedDateTime atStartOfDay(ZoneId zone) {
    LocalDateTime ldt = atTime(LocalTime.MIDNIGHT);
    ...
    return ZonedDateTime.of(ldt, zone);
}

// LocalDateTime转ZonedDateTime
public ZonedDateTime atZone(ZoneId zone) {
    return ZonedDateTime.of(this, zone);
}

final class DateTimeFormatter

ISO_LOCAL_DATE; // 【2011-12-03】LocalDate默认
ISO_LOCAL_TIME; // 【10:15:30】LocalTime默认
ISO_LOCAL_DATE_TIME; // 【2011-12-03T10:15:30】LocalDateTime默认
BASIC_ISO_DATE; // 【20111203】