前言
LocalDate,LocalDateTime,LocalTime, YearMonth, Year, MonthDay,它们代表与上下文相结合的本地日期/时间。这些类主要用于不需要在上下文中明确指定时区的情况,他们是线程安全的
类 输出案例
LocalDate 2020-05-18
LocalDateTime 2020-05-18T00:05:11.608
LocalTime 00:05:11.608
YearMonth 2020-05
Year 2020
MonthDay --05-18
一、LocalDate
public final class LocalDate extends Object implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable
1、静态方法,获取对象
static LocalDate from(TemporalAccessor temporal)
static LocalDate now()
static LocalDate now(Clock clock)
static LocalDate now(ZoneId zone)
static LocalDate parse(CharSequence text)
static LocalDate parse(CharSequence text, DateTimeFormatter formatter)
static LocalDate of(int year, int month, int dayOfMonth)
static LocalDate of(int year, Month month, int dayOfMonth)
//ofEpochDay是从纪元日(1970-1-1)计数中获取LocalDate的实例
static LocalDate ofEpochDay(long epochDay)
//ofYearDay从年份和年份获取LocalDate的实例
static LocalDate ofYearDay(int year, int dayOfYear)
案例
System.out.println( LocalDate.ofEpochDay(100));//1970-04-11
System.out.println( LocalDate.ofYearDay(2017,38));//2017-02-07
2、对象计算
//加
LocalDate plus(long amountToAdd, TemporalUnit unit)
LocalDate plus(TemporalAmount amountToAdd)
LocalDate plusDays(long daysToAdd)
LocalDate plusMonths(long monthsToAdd)
LocalDate plusWeeks(long weeksToAdd)
LocalDate plusYears(long yearsToAdd)
//减
LocalDate minus(long amountToSubtract, TemporalUnit unit)
LocalDate minus(TemporalAmount amountToSubtract)
LocalDate minusDays(long daysToSubtract)
LocalDate minusMonths(long monthsToSubtract)
LocalDate minusWeeks(long weeksToSubtract)
LocalDate minusYears(long yearsToSubtract)
//自定义
LocalDate with(TemporalField field, long newValue)
LocalDate withDayOfMonth(int dayOfMonth)
LocalDate withDayOfYear(int dayOfYear)
LocalDate withMonth(int month)
LocalDate withYear(int year)
LocalDate with(TemporalAdjuster adjuster)
3、对象的格式化
String format(DateTimeFormatter formatter)
4、信息获取
int get(TemporalField field)
IsoChronology getChronology()
int getDayOfMonth()
DayOfWeek getDayOfWeek()
int getDayOfYear()
Era getEra()
long getLong(TemporalField field)
Month getMonth()
int getMonthValue()
int getYear()
int lengthOfMonth()
int lengthOfYear()
long toEpochDay()
5、对象信息判断
boolean isAfter(ChronoLocalDate other)//检查此日期是否在指定日期之后。
boolean isBefore(ChronoLocalDate other)//检查此日期是否在指定日期之前。
boolean isEqual(ChronoLocalDate other)//检查此日期是否等于指定的日期。
boolean isLeapYear()//根据ISO培训日历系统规则,检查年份是否是闰年。
boolean isSupported(TemporalField field)//检查指定的字段是否受支持。
boolean isSupported(TemporalUnit unit)//检查指定的单位是否受支持。
6、对象改变
LocalDateTime atTime(int hour, int minute)//结合此日期与时间创建一个 LocalDateTime 。
LocalDateTime atTime(int hour, int minute, int second)//结合此日期与时间创建一个 LocalDateTime 。
LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond)//结合此日期与时间创建一个 LocalDateTime 。
LocalDateTime atTime(LocalTime time)
LocalDateTime atStartOfDay()//将此日期与午夜时间结合 LocalDateTime ,在此日期开始时创建一个 LocalDateTime
ZonedDateTime atStartOfDay(ZoneId zone)//根据时区中的规则,在最早的有效时间内从此日期返回划分的日期时间
OffsetDateTime atTime(OffsetTime time)///将此日期与偏移时间相结合以创建 OffsetDateTime
Temporal adjustInto(Temporal temporal)
7、此对象时间到另一个时间对象的距离
Period until(ChronoLocalDate endDateExclusive)
long until(Temporal endExclusive, TemporalUnit unit)
LocalDate localDate1 = LocalDate.of(2011,1,1);
LocalDate localDate2 = LocalDate.of(2011,2,5);
System.out.println(localDate1.until(localDate2,ChronoUnit.DAYS));//35
8、查询方法
<R> R query(TemporalQuery<R> query)
二、LocalDateTime
1、静态方法,获取对象
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)
static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute)
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)
static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second)
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
static LocalDateTime of(LocalDate date, LocalTime time)
static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset)
static LocalDateTime ofInstant(Instant instant, ZoneId zone)
static LocalDateTime from(TemporalAccessor temporal)
static LocalDateTime now()
static LocalDateTime now(Clock clock)
static LocalDateTime now(ZoneId zone)
static LocalDateTime parse(CharSequence text)
static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)
2、对象计算
//加
LocalDateTime plus(long amountToAdd, TemporalUnit unit)
LocalDateTime plus(TemporalAmount amountToAdd)
LocalDateTime plusDays(long days)
LocalDateTime plusHours(long hours)
LocalDateTime plusMinutes(long minutes)
LocalDateTime plusMonths(long months)
LocalDateTime plusNanos(long nanos)
LocalDateTime plusSeconds(long seconds)
LocalDateTime plusWeeks(long weeks)
LocalDateTime plusYears(long years)
//减
LocalDateTime minus(long amountToSubtract, TemporalUnit unit)
LocalDateTime minus(TemporalAmount amountToSubtract)
LocalDateTime minusDays(long days)
LocalDateTime minusHours(long hours)
LocalDateTime minusMinutes(long minutes)
LocalDateTime minusMonths(long months)
LocalDateTime minusNanos(long nanos)
LocalDateTime minusSeconds(long seconds)
LocalDateTime minusWeeks(long weeks)
LocalDateTime minusYears(long years)
//自定义
LocalDateTime with(TemporalAdjuster adjuster)
LocalDateTime with(TemporalField field, long newValue)
LocalDateTime withDayOfMonth(int dayOfMonth)
LocalDateTime withDayOfYear(int dayOfYear)
LocalDateTime withHour(int hour)
LocalDateTime withMinute(int minute)
LocalDateTime withMonth(int month)
LocalDateTime withNano(int nanoOfSecond)
LocalDateTime withSecond(int second)
LocalDateTime withYear(int year)
3、对象的格式化
String format(DateTimeFormatter formatter)
4、信息获取
int get(TemporalField field)
int getDayOfMonth()
DayOfWeek getDayOfWeek()
int getDayOfYear()
int getHour()
long getLong(TemporalField field)
int getMinute()
Month getMonth()
int getMonthValue()
int getNano()
int getSecond()
int getYear()
5、对象信息判断
boolean isAfter(ChronoLocalDateTime<?> other)//检查这个日期时间是否在指定的日期之后。
boolean isBefore(ChronoLocalDateTime<?> other)//检查此日期时间是否在指定的日期时间之前。
boolean isEqual(ChronoLocalDateTime<?> other)//检查此日期时间是否等于指定的日期时间。
boolean isSupported(TemporalField field)//检查指定的字段是否受支持。
boolean isSupported(TemporalUnit unit)//检查指定的单位是否受支持。
6、对象改变
Temporal adjustInto(Temporal temporal)//调整指定的时间对象与此对象具有相同的日期和时间。
OffsetDateTime atOffset(ZoneOffset offset)//将此日期时间与偏移量相结合以创建 OffsetDateTime 。
ZonedDateTime atZone(ZoneId zone)//将此日期时间与时区相结合以创建 ZonedDateTime 。
LocalDate toLocalDate()//获得这个日期时间的 LocalDate一部分。
LocalTime toLocalTime()//获得这个日期时间的 LocalTime一部分。
7、此对象时间到另一个时间对象的距离
long until(Temporal endExclusive, TemporalUnit unit)
8、查询方法
<R> R query(TemporalQuery<R> query)//使用指定的查询查询此日期时间。
ValueRange range(TemporalField field)//获取指定字段的有效值的范围。
三、LocalTime
1、静态方法,获取对象
static LocalTime of(int hour, int minute)
static LocalTime of(int hour, int minute, int second)
static LocalTime of(int hour, int minute, int second, int nanoOfSecond)
static LocalTime ofNanoOfDay(long nanoOfDay)
static LocalTime ofSecondOfDay(long secondOfDay)//ofNanoOfDay是一天所过的纳秒值获取实例
static LocalTime now()
static LocalTime now(Clock clock)
static LocalTime now(ZoneId zone)
static LocalTime parse(CharSequence text)
static LocalTime parse(CharSequence text, DateTimeFormatter formatter)
static LocalTime from(TemporalAccessor temporal)
2、对象计算
//加
LocalTime plus(long amountToAdd, TemporalUnit unit)
LocalTime plus(TemporalAmount amountToAdd)
LocalTime plusHours(long hoursToAdd)
LocalTime plusMinutes(long minutesToAdd)
LocalTime plusNanos(long nanosToAdd)
LocalTime plusSeconds(long secondstoAdd)
//减
LocalTime minus(long amountToSubtract, TemporalUnit unit)
LocalTime minus(TemporalAmount amountToSubtract)
LocalTime minusHours(long hoursToSubtract)
LocalTime minusMinutes(long minutesToSubtract)
LocalTime minusNanos(long nanosToSubtract)
LocalTime minusSeconds(long secondsToSubtract)
//自定义
LocalTime with(TemporalAdjuster adjuster)
LocalTime with(TemporalField field, long newValue)
LocalTime withHour(int hour)
LocalTime withMinute(int minute)
LocalTime withNano(int nanoOfSecond)
LocalTime withSecond(int second)
3、对象的格式化
String format(DateTimeFormatter formatter)
4、信息获取
int get(TemporalField field)
int getHour()
long getLong(TemporalField field)
int getMinute()
int getNano()
int getSecond()
long toNanoOfDay()//返回当天已经过的纳米数
int toSecondOfDay()//返回当天已经过的秒数
5、对象信息判断
boolean isAfter(LocalTime other)//检查 LocalTime是否在指定的时间之后。
boolean isBefore(LocalTime other)//检查这个 LocalTime是否在指定的时间之前。
boolean isSupported(TemporalField field)//检查指定的字段是否受支持。
boolean isSupported(TemporalUnit unit)//检查指定的单位是否受支持。
6、对象改变
Temporal adjustInto(Temporal temporal)//调整指定的时间对象与此对象具有相同的时间。
LocalDateTime atDate(LocalDate date)//结合这个时间与创建一个 LocalDateTime的日期。
OffsetTime atOffset(ZoneOffset offset)//结合这个时间与偏移创建一个 OffsetTime
7、此对象时间到另一个时间对象的距离
long until(Temporal endExclusive, TemporalUnit unit)
8、查询方法
<R> R query(TemporalQuery<R> query)
此时使用指定的查询进行查询。
ValueRange range(TemporalField field)
获取指定字段的有效值的范围。
四、YearMonth
1、静态工厂方法,获取日期类对象
static YearMonth of(int year, int month)
static YearMonth of(int year, Month month)
static YearMonth from(TemporalAccessor temporal)
static YearMonth now(Clock clock)
static YearMonth now(ZoneId zone)
static YearMonth parse(CharSequence text)
static YearMonth parse(CharSequence text, DateTimeFormatter formatter)
2、、对象计算
//减
YearMonth minus(long amountToSubtract, TemporalUnit unit)
YearMonth minusMonths(long monthsToSubtract)
YearMonth minusYears(long yearsToSubtract)
YearMonth minus(TemporalAmount amountToSubtract)
//加
YearMonth plus(long amountToAdd, TemporalUnit unit)
YearMonth plus(TemporalAmount amountToAdd)
YearMonth plusMonths(long monthsToAdd)
YearMonth plusYears(long yearsToAdd)
//自定义
YearMonth with(TemporalAdjuster adjuster)
YearMonth with(TemporalField field, long newValue)
YearMonth withMonth(int month)
YearMonth withYear(int year)
3、对象的格式化
String format(DateTimeFormatter formatter)
4、信息获取
int getMonthValue()
int getYear()
int get(TemporalField field)
long getLong(TemporalField field)
Month getMonth()
int lengthOfMonth()//当前月的有多少天
int lengthOfYear()//当前年有多少天
5、信息判断
boolean isAfter(YearMonth other)//是指定年月后的今年
boolean isBefore(YearMonth other)//是指定年月前的一个月。
boolean isLeapYear()//根据ISO培训日历系统规则,检查年份是否是闰年。
boolean isSupported(TemporalField field)//检查指定的字段是否受支持
boolean isSupported(TemporalUnit unit)//检查指定的单位是否受支持
boolean isValidDay(int dayOfMonth)//检查月日是否在今年有效
6、对象改变
Temporal adjustInto(Temporal temporal)//将指定的时间对象调整为今年。
LocalDate atDay(int dayOfMonth)//结合今年的月份,创造一个 LocalDate 。
LocalDate atEndOfMonth()//在本月底返回 LocalDate 。
7、此对象时间到另一个时间对象的距离
long until(Temporal endExclusive, TemporalUnit unit)
8、查询方法
<R> R query(TemporalQuery<R> query)
五、Year
1、静态工厂方法,获取日期类对象
static Year of(int isoYear)
static Year from(TemporalAccessor temporal)
static Year now()
static Year now(Clock clock)
static Year now(ZoneId zone)
static Year parse(CharSequence text)
static Year parse(CharSequence text, DateTimeFormatter formatter)
2、对象计算
//减
Year minus(long amountToSubtract, TemporalUnit unit)
Year minus(TemporalAmount amountToSubtract)
Year minusYears(long yearsToSubtract)
//加
Year plus(long amountToAdd, TemporalUnit unit)
Year plus(TemporalAmount amountToAdd)
Year plusYears(long yearsToAdd)
//自定义
Year with(TemporalAdjuster adjuster)
Year with(TemporalField field, long newValue)
3、对象的格式化
String format(DateTimeFormatter formatter)
4、信息获取
int get(TemporalField field)
long getLong(TemporalField field)
int getValue()
int length()
5、对象信息判断
boolean isAfter(Year other)
boolean isBefore(Year other)
boolean isLeap()//检查年份是否是闰年
static boolean isLeap(long year)//检查年份是否是闰年
boolean isSupported(TemporalField field)//检查指定的字段是否受支持。
boolean isSupported(TemporalUnit unit)//检查指定的单位是否受支持。
boolean isValidMonthDay(MonthDay monthDay)//检查今年的月日是否有效。
6、对象改变
Temporal adjustInto(Temporal temporal)//调整指定的时间对象到今年。
LocalDate atDay(int dayOfYear)//今年与今年的合并创建了一个 LocalDate 。
YearMonth atMonth(int month)//今年结合一个月创建一个 YearMonth 。
YearMonth atMonth(Month month)//今年结合一个月创建一个 YearMonth 。
LocalDate atMonthDay(MonthDay monthDay)//今年结合了一个月的一个月,创造了一个 LocalDate 。
7、此对象时间到另一个时间对象的距离
long until(Temporal endExclusive, TemporalUnit unit)
8、查询方法
<R> R query(TemporalQuery<R> query)//今年查询使用指定的查询。
ValueRange range(TemporalField field)//获取指定字段的有效值的范围。
六、MonthDay
1、静态方法,获取对象
static MonthDay of(int month, int dayOfMonth)
static MonthDay of(Month month, int dayOfMonth)
static MonthDay from(TemporalAccessor temporal)
static MonthDay now(ZoneId zone)
static MonthDay now(Clock clock)
static MonthDay now()
static MonthDay parse(CharSequence text, DateTimeFormatter formatter)
static MonthDay parse(CharSequence text)
2、对象计算
MonthDay with(Month month)方法返回此MonthDay的副本,其中月份已更改
MonthDay withMonth(int month)方法返回此MonthDay的副本,其中月份已更改
MonthDay withDayOfMonth(int dayOfMonth)方法返回此MonthDay的副本,其中天数已更改
3、对象的格式化
String format(DateTimeFormatter formatter)
4、信息获取
int get(TemporalField field)
int getDayOfMonth()
long getLong(TemporalField field)
Month getMonth()
int getMonthValue()
5、对象信息判断
boolean isAfter(MonthDay other)//指定的月日之后的这个月份。
boolean isBefore(MonthDay other)//是在指定的月日前的这个月前一天。
boolean isSupported(TemporalField field)//检查指定的字段是否受支持。
boolean isValidYear(int year)//检查年份是否适用于本月。
6、对象改变
Temporal adjustInto(Temporal temporal)//将指定的时间对象调整为具有这个月日。
LocalDate atYear(int year)//结合本月与一年创建 LocalDate 。
7、查询方法
<R> R query(TemporalQuery<R> query)
ValueRange range(TemporalField field)//获取指定字段的有效值的范围。