前言
ZoneOffset,LocalDateTime,LocalTime, YearMonth, Year, MonthDay,它们代表与上下文相结合的本地日期/时间。这些类主要用于不需要在上下文中明确指定时区的情况,他们是线程安全的
类 信息类别 输出案例
ZoneOffset 时区偏移量 +01:00
ZoneId 时区 Asia/Shanghai
ZonedDateTime 2020-05-19T14:51:10.826+08:00[Asia/Shanghai]
OffsetDateTime 2020-05-19T14:49:31.506+08:00
OffsetTime 14:49:01.581+08:00
一、ZoneOffset
public final class ZoneOffset extends ZoneId implements TemporalAccessor, TemporalAdjuster, Comparable, Serializable
1、获取
static ZoneOffset of(String offsetId)
static ZoneOffset ofHours(int hours)
static ZoneOffset ofHoursMinutes(int hours, int minutes)
static ZoneOffset ofHoursMinutesSeconds(int hours, int minutes, int seconds)
static ZoneOffset ofTotalSeconds(int totalSeconds)//获取 ZoneOffset的实例, ZoneOffset总偏移量(以秒为单位)
static ZoneOffset from(TemporalAccessor temporal)//从时间对象获取一个 ZoneOffset的实例。
2、信息获取
int compareTo(ZoneOffset other)。。将此偏移量与其他偏移量按降序进行比较。
boolean equals(Object obj)。。检查这个偏移量是否等于另一个偏移量。
int get(TemporalField field)。。从该偏移量获取指定字段的值作为 int 。
String getId()//获取标准化区域偏移ID。
long getLong(TemporalField field)//从该偏移量获取指定字段的值作为 long 。
ZoneRules getRules()//获取相关的时区规则。
int getTotalSeconds()//获取总区域偏移量(以秒为单位)。
int hashCode()//这个偏移的哈希码。
boolean isSupported(TemporalField field)//检查指定的字段是否受支持。
<R> R query(TemporalQuery<R> query)//使用指定的查询查询此偏移量。
ValueRange range(TemporalField field)//获取指定字段的有效值的范围。
String toString()//输出该偏移为 String ,使用归一化ID。
二、ZoneId
public abstract class ZoneId extends Object implements Serializable
1、获取
static ZoneId of(String zoneId)//从ID获取一个 ZoneId的实例,确保该ID有效并且可供使用。
static ZoneId of(String zoneId, Map<String,String> aliasMap)//获取 ZoneId的实例,使用其ID使用别名映射来补充标准区域ID。
static ZoneId ofOffset(String prefix, ZoneOffset offset)//获得一个封装偏移量的 ZoneId的实例。
static ZoneId systemDefault()
static ZoneId from(TemporalAccessor temporal)//从时间对象获取一个 ZoneId的实例。
三、ZonedDateTime
public final class ZonedDateTime extends Object implements Temporal, ChronoZonedDateTime, Serializable
1、对象的创建
static ZonedDateTime now()
static ZonedDateTime now(Clock clock)
static ZonedDateTime now(ZoneId zone)
static ZonedDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone)
static ZonedDateTime of(LocalDate date, LocalTime time, ZoneId zone)
static ZonedDateTime of(LocalDateTime localDateTime, ZoneId zone)
static ZonedDateTime ofInstant(Instant instant, ZoneId zone)
static ZonedDateTime ofInstant(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone)
static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset)
static ZonedDateTime ofStrict(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone)
static ZonedDateTime parse(CharSequence text)
static ZonedDateTime parse(CharSequence text, DateTimeFormatter formatter)
static ZonedDateTime from(TemporalAccessor temporal)
2、时间计算
//加
ZonedDateTime plus(long amountToAdd, TemporalUnit unit)
ZonedDateTime plus(TemporalAmount amountToAdd)
ZonedDateTime plusDays(long days)
ZonedDateTime plusHours(long hours)
ZonedDateTime plusMinutes(long minutes)
ZonedDateTime plusMonths(long months)
ZonedDateTime plusNanos(long nanos)
ZonedDateTime plusSeconds(long seconds)
ZonedDateTime plusWeeks(long weeks)
ZonedDateTime plusYears(long years)
//减
ZonedDateTime minus(long amountToSubtract, TemporalUnit unit)
ZonedDateTime minus(TemporalAmount amountToSubtract)
ZonedDateTime minusDays(long days)
ZonedDateTime minusHours(long hours)
ZonedDateTime minusMinutes(long minutes)
ZonedDateTime minusMonths(long months)
ZonedDateTime minusNanos(long nanos)
ZonedDateTime minusSeconds(long seconds)
ZonedDateTime minusWeeks(long weeks)
ZonedDateTime minusYears(long years)
//自定义
ZonedDateTime with(TemporalAdjuster adjuster)
ZonedDateTime with(TemporalField field, long newValue)
ZonedDateTime withDayOfMonth(int dayOfMonth)
ZonedDateTime withDayOfYear(int dayOfYear)
ZonedDateTime withEarlierOffsetAtOverlap()
ZonedDateTime withFixedOffsetZone() //去除时区返回
ZonedDateTime withHour(int hour)
ZonedDateTime withLaterOffsetAtOverlap()
ZonedDateTime withMinute(int minute)
ZonedDateTime withMonth(int month)
ZonedDateTime withNano(int nanoOfSecond)
ZonedDateTime withSecond(int second)
ZonedDateTime withYear(int year)
ZonedDateTime withZoneSameInstant(ZoneId zone)
ZonedDateTime withZoneSameLocal(ZoneId zone)
3、类型转换
LocalDate toLocalDate()
LocalDateTime toLocalDateTime()
LocalTime toLocalTime()
OffsetDateTime toOffsetDateTime()
ZonedDateTime truncatedTo(TemporalUnit unit)
4、对象信息获取
int get(TemporalField field)
int getDayOfMonth()
int getDayOfYear()
int getHour()
long getLong(TemporalField field)
int getMinute()
Month getMonth()
int getMonthValue()
int getNano()
int getSecond()
int getYear()
ZoneId getZone()
ZoneOffset getOffset()
DayOfWeek getDayOfWeek()
5、格式化
String format(DateTimeFormatter formatter)
6、时间距离
long until(Temporal endExclusive, TemporalUnit unit)
7、对象信息判断
boolean isSupported(TemporalField field)
boolean isSupported(TemporalUnit unit)
8、其他方法
<R> R query(TemporalQuery<R> query)
ValueRange range(TemporalField field)
三、OffsetDateTime
1、静态方法,获取对象
static OffsetDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset)
static OffsetDateTime of(LocalDate date, LocalTime time, ZoneOffset offset)
static OffsetDateTime of(LocalDateTime dateTime, ZoneOffset offset)
static OffsetDateTime ofInstant(Instant instant, ZoneId zone)
static OffsetDateTime now()
static OffsetDateTime now(Clock clock
static OffsetDateTime now(ZoneId zone)
static OffsetDateTime parse(CharSequence text)
static OffsetDateTime parse(CharSequence text, DateTimeFormatter formatter)
static OffsetDateTime from(TemporalAccessor temporal
2、格式化
String format(DateTimeFormatter formatter)
3、获取对象信息
int get(TemporalField field)
int getDayOfMonth()
DayOfWeek getDayOfWeek()
int getDayOfYear()
int getHour()
long getLong(TemporalField field)
int getMinute()
Month getMonth()
int getMonthValue()
int getNano()
ZoneOffset getOffset()
int getSecond()
int getYear()
4、对象判断
boolean isAfter(OffsetDateTime other)
boolean isBefore(OffsetDateTime other)
boolean isEqual(OffsetDateTime other)
boolean isSupported(TemporalField field)
boolean isSupported(TemporalUnit unit)
5、对象计算
//加
OffsetDateTime plus(long amountToAdd, TemporalUnit unit)
OffsetDateTime plus(TemporalAmount amountToAdd)
OffsetDateTime plusDays(long days)
OffsetDateTime plusHours(long hours)
OffsetDateTime plusMinutes(long minutes)
OffsetDateTime plusMonths(long months)
OffsetDateTime plusNanos(long nanos)
OffsetDateTime plusSeconds(long seconds)
OffsetDateTime plusWeeks(long weeks)
OffsetDateTime plusYears(long years)
//减
OffsetDateTime minus(long amountToSubtract, TemporalUnit unit)
OffsetDateTime minus(TemporalAmount amountToSubtract)
OffsetDateTime minusDays(long days)
OffsetDateTime minusHours(long hours)
OffsetDateTime minusMinutes(long minutes)
OffsetDateTime minusMonths(long months)
OffsetDateTime minusNanos(long nanos)
OffsetDateTime minusSeconds(long seconds)
OffsetDateTime minusWeeks(long weeks)
OffsetDateTime minusYears(long years)
//自定义
OffsetDateTime with(TemporalAdjuster adjuster)
OffsetDateTime with(TemporalField field, long newValue)
OffsetDateTime withDayOfMonth(int dayOfMonth)
OffsetDateTime withDayOfYear(int dayOfYear)
OffsetDateTime withHour(int hour)
OffsetDateTime withMinute(int minute)
OffsetDateTime withMonth(int month)
OffsetDateTime withNano(int nanoOfSecond)
OffsetDateTime withOffsetSameInstant(ZoneOffset offset)
OffsetDateTime withOffsetSameLocal(ZoneOffset offset)
OffsetDateTime withSecond(int second)
OffsetDateTime withYear(int year)
6、对象改变
Instant toInstant()
LocalDate toLocalDate()
LocalDateTime toLocalDateTime()
LocalTime toLocalTime()
OffsetTime toOffsetTime()
ZonedDateTime toZonedDateTime()
7、对象统计
long toEpochSecond()//将此日期时间的秒数从1970-01-01t00:00:00z时代
8、对象改变
Temporal adjustInto(Temporal temporal)
ZonedDateTime atZoneSameInstant(ZoneId zone)
ZonedDateTime atZoneSimilarLocal(ZoneId zone)
9、时间距离
long until(Temporal endExclusive, TemporalUnit unit)
10、其他方法
<R> R query(TemporalQuery<R> query)
static Comparator<OffsetDateTime> timeLineOrder()
ValueRange range(TemporalField field)
四、OffsetTime
1、静态方法。获取对象
static OffsetTime of(int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset)
static OffsetTime of(LocalTime time, ZoneOffset offset)
static OffsetTime ofInstant(Instant instant, ZoneId zone)
static OffsetTime now()
static OffsetTime now(Clock clock)
static OffsetTime now(ZoneId zone)
static OffsetTime from(TemporalAccessor temporal)
static OffsetTime parse(CharSequence text)
static OffsetTime parse(CharSequence text, DateTimeFormatter formatter)
2、对象格式化
String format(DateTimeFormatter formatter)
3、对象信息获取
int get(TemporalField field)
int getHour()
long getLong(TemporalField field)
int getMinute()
int getNano()
ZoneOffset getOffset()
int getSecond()
4、对象信息判断
boolean isAfter(OffsetTime other)
boolean isBefore(OffsetTime other)
boolean isEqual(OffsetTime other)
boolean isSupported(TemporalField field)
boolean isSupported(TemporalUnit unit)
5、对象计算
//加
OffsetTime plus(long amountToAdd, TemporalUnit unit)
OffsetTime plus(TemporalAmount amountToAdd)
OffsetTime plusHours(long hours)
OffsetTime plusMinutes(long minutes)
OffsetTime plusNanos(long nanos)
OffsetTime plusSeconds(long seconds)
//减
OffsetTime minus(long amountToSubtract, TemporalUnit unit)
OffsetTime minus(TemporalAmount amountToSubtract)
OffsetTime minusHours(long hours)
OffsetTime minusMinutes(long minutes)
OffsetTime minusNanos(long nanos)
OffsetTime minusSeconds(long seconds)
//自定义
OffsetTime with(TemporalAdjuster adjuster)
OffsetTime with(TemporalField field, long newValue)
OffsetTime withHour(int hour)
OffsetTime withMinute(int minute)
OffsetTime withNano(int nanoOfSecond)
OffsetTime withOffsetSameInstant(ZoneOffset offset)
OffsetTime withOffsetSameLocal(ZoneOffset offset)
OffsetTime withSecond(int second)
6、时间距离
long until(Temporal endExclusive, TemporalUnit unit)
7、对象改变
LocalTime toLocalTime()
Temporal adjustInto(Temporal temporal)
OffsetDateTime atDate(LocalDate date)
8、其他方法
<R> R query(TemporalQuery<R> query)
ValueRange range(TemporalField field)