[java8]java.time包(一)快速简介

前言
现在已经是2019-05-11,java8的java.time还有没了解的吗?
来一起了解下吧。

简介

  • 包位置

    package java.time;
    [java8]java.time包(一)快速简介_第1张图片

  • 看看里面有些什么
    通过idea的类结构图展示
    [java8]java.time包(一)快速简介_第2张图片
    哇塞,酷。
    直接看包里面:
    [java8]java.time包(一)快速简介_第3张图片
    接下来,将会按照上图的文件顺序一个一个往下看:

开始

  • chrono包:提供一些关于年表(Chronology)处理的默认实现和接口定义。
    [java8]java.time包(一)快速简介_第4张图片

  • format包:提供了一些时间日期格式化相关的实现和枚举类。
    [java8]java.time包(一)快速简介_第5张图片

  • temporal包:提供了一些与时间相关的对象接口定义以及一些默认实现。
    [java8]java.time包(一)快速简介_第6张图片
    例如:

  • TemporalAccessor接口:Framework-level interface defining read-only access to a temporal object,such as a date, time, offset or some combination of these.
    定义对时间对象的只读访问框架级接口,例如日期,时间,偏移或这些的某种组合。
    TemporalAccessor接口定义了5个方法:

// 检查传递的field是否支持
boolean isSupported(TemporalField field);
// 默认实现的range方法,获取field的有效值范围
default ValueRange range(TemporalField field){...}
// 默认实现的get方法,获取指定字段的int类型值
default int get(TemporalField field) {..}
// 定义接口getLong方法,获取指定字段的long类型值
long getLong(TemporalField field);
// 默认实现的query方法,查询this的日期时间
default  R query(TemporalQuery query) {...}
  • zone包:提供了一些与时区有关的抽象类(包含默认实现),涉及时区规则时区转换以及相应的异常定义。
    [java8]java.time包(一)快速简介_第7张图片

  • Clock抽象类:A clock providing access to the current instant, date and time using a time-zone。
    clock提供了使用时区去访问当前的instans,date,time。
    [java8]java.time包(一)快速简介_第8张图片
    Clock抽象类中有4个clock的不同实现,已经定义的抽象方法以及部分方法的默认实现。

  • DateTimeException异常类:Exception used to indicate a problem while calculating a date-time.
    计算日期时间时用来表明一个问题
    直接继承RuntimeException

  • DayOfWeek枚举类:A day-of-week, such as ‘Tuesday’.
    用来表示星期几,例如’星期二’。
    DayOfWeek枚举类实现了TemporalAccessor, TemporalAdjuster
    [java8]java.time包(一)快速简介_第9张图片
    可以看到,DayOfWeek枚举类中定义了周一到周日的枚举。并且实现temporal包中的TemporalAccessor, TemporalAdjuster中的方法。

  • Duration类:A time-based amount of time, such as ‘34.5 seconds’。
    基于时间的时间量,例如’34 .5秒’;也就是说该类用来计算时间差量。
    Duration类实现了TemporalAmount, Comparable, Serializable
    (里面方法比较多,大家慢慢去源码看喔)

  • Instant类:An instantaneous point on the time-line.
    时间线上的瞬时点;也就是即时时间。
    This class models a single instantaneous point on the time-line.
    This might be used to record event time-stamps in the application.
    该类在时间线上模拟单个瞬时点。这可能用于在应用程序中记录事件时间戳
    也就是说,Instant类能获取到瞬时的时间对象。

  • LocalDate类:A date without a time-zone in the ISO-8601 calendar system, such as ’ 2007-12-03’.
    在ISO-8601日历系统中没有时区的日期,例如:‘2007-12-03’.
    该类实现了Temporal, TemporalAdjuster, ChronoLocalDate, Serializable

  • LocalDateTime类:A date-time without a time-zone in the ISO-8601 calendar system, such as ‘2007-12-03T10:15:30’
    在ISO-8601日历系统中没有时区的日期时间,例如:‘2007-12-03T10:15:30’.
    该类实现了Temporal, TemporalAdjuster, ChronoLocalDateTime, Serializable
    LocalDateTime和LocalDate区别在于是否在日期date显示上显示出具体的时间time。

  • LocalTime类:A time without a time-zone in the ISO-8601 calendar system, such as ‘10:15:30’.
    在ISO-8601日历系统中没有时区的时间,例如’10:15:30’
    该类实现了Temporal, TemporalAdjuster, Comparable, Serializable
    LocalDateTime和LocalTime区别在于是否在时间time显示上显示出具体的日期date。

LocalDate + LocalTime = LocalDateTime

  • Month枚举类:A month-of-year, such as ‘July’.
    用来表示几月,例如’7月’。
    Month枚举类实现了TemporalAccessor, TemporalAdjuster
    [java8]java.time包(一)快速简介_第10张图片
    可以看到,Month枚举类中定义了一月到十一月的枚举。并且实现temporal包中的TemporalAccessor, TemporalAdjuster中的方法。

  • MonthDay类:A month-day in the ISO-8601 calendar system, such as ‘–12-03’.
    ISO-8601日历系统中的一个月日,例如 ‘12-03’
    MonthDay类实现了TemporalAccessor, TemporalAdjuster, Comparable, Serializable 中的相关方法。

  • OffsetDateTime类:A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as ‘2007-12-03T10:15:30+01:00’.
    在ISO-8601日历系统中与UTC / Greenwich偏移的日期时间,例如“2007-12-03T10:15:30 + 01:00”。

  • OffsetTime类:A time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as ‘10:15:30+01:00’.
    在ISO-8601日历系统中与UTC / Greenwich偏移的时间,例如“10:15:30+01:00”。
    OffsetTime类和OffsetDateTime类区别在于偏移计算是否涉及日期。

  • Period类:A date-based amount of time in the ISO-8601 calendar system, such as ‘2 years, 3 months and 4 days’.
    ISO-8601日历系统中基于日期的时间量,例如“2年,3个月和4天”。
    Period类实现了ChronoPeriod, Serializable。
    Period类与Duration类区别在于一个是针对日期,一个是针对时间。

  • Ser类:The shared serialization delegate for this package.
    time包的共享序列化委托。
    Ser类实现了Externalizable。

  • Year类:A year in the ISO-8601 calendar system, such as ‘2007’.
    ISO-8601日历系统中的一年,例如’2007’。
    Year类实现了Temporal, TemporalAdjuster, Comparable, Serializable。

  • YearMonth类:A year-month in the ISO-8601 calendar system, such as ‘2007-12’.
    ISO-8601日历系统中的一年中,例如“2007-12”。
    YearMonth类实现了Temporal, TemporalAdjuster, Comparable, Serializable。

  • ZonedDateTime类:A date-time with a time-zone in the ISO-8601 calendar system, such as ‘2007-12-03T10:15:30+01:00 Europe/Paris’.
    ISO-8601日历系统中带有时区的日期时间,例如“2007-12-03T10:15:30 + 01:00 Europe/Paris”。
    ZonedDateTime类实现了Temporal, ChronoZonedDateTime, Serializable。

  • ZoneId抽象类:A time-zone ID, such as ‘Europe/Paris’.
    时区ID,例如 ‘Europe/Paris’.
    里面包括了java 8 中自带的时区。

  • ZoneOffset类:A time-zone offset from Greenwich/UTC, such as ‘+02:00’.
    与Greenwich/UTC的时区偏移,例如’+02:00’。
    ZoneOffset类继承ZoneId并实现了TemporalAccessor, TemporalAdjuster, Comparable, Serializable。
    主要用于不同时区之间的偏移计算

  • ZoneRegion类:A geographical region where the same time-zone rules apply.
    适用相同时区规则的地理区域。
    ZoneRegion类继承了ZoneId实现了Serializable序列化接口。

结束
OK,java8中的java.time包的快速简介就是以上,下一篇,将会进行相关时间API的使用与实践。[java8]java.time包(二)快速实践

你可能感兴趣的:(java8,java.time,time包,jdk8,好东西,java)