JavaSE-日期和时间API


Java原本提供了Date和Calendar用于处理日期、时间的类,包括创建日期、时间对象,获取系统当前日期、时间等操作。但Date不仅无法实现国际化,而且它对不同属性也使用了前后矛盾的偏移量,比如月份与小时都是从0开始的,月份中的天数则是从1开始的,年又是从1900年开始的,而Calendar显得过于复杂

格林尼治时间:1970年1月1日0点0分0秒为时间原点

  • 1秒 = 1000毫秒
  • 1毫秒 = 1000微秒
  • 1微秒 = 1000纳秒
  • 1秒 = 1000000000纳秒

Date、Calendar

java.util.Date类用来处理日期、时间。Date类从JDK1.0就存在了,但正因为它历史悠久,所以它的大部分构造器、方法都已经过时,不再推荐使用了。Date类提供了6个构造器,其中4个已经Deprecated,剩下两个构造器如下

Date 构造方法
构造方法 说明
Date() 生成一个代表当前日期时间的Date对象;该构造器在底层调用System.currentTimeMillis()获得long整数作为日期参数
Date(long date) 根据指定的形参来生成Date对象;形参表示创建的Date对象和GMT1970年1月1日00:00:00之间的时间差,以毫秒为单位
Date 常用方法
常用方法 返回值类型 说明
after(Date when) boolean 测试该日期是否在指定日期之后
before(Date when) boolean 测试该日期是否在指定日期之前
getTime() long 返回改时间对应的long型整数;即GMT1970-01-01 00:00:00到该Date对象之间的时间差,以毫秒作为计时单位
setTime(long time) void 设置该Date对象的时间
from(Instant instant) static Date 从Instant对象中获取Date实例
toInstant() Instant 将此Date对象转换为Instant
Date date1 = new Date(2021, 11, 25, 8, 0, 0);
Date date2 = new Date("Dec 26 08:00:00 2021");
Date date3 = new Date("2021/12/26 08:00:00");
System.out.println(date1); // Sun Dec 25 08:00:00 CST 3921(3921=指定时间+1900)
System.out.println(date2); // Sun Dec 26 08:00:00 CST 2021(CST是中国的时区)
System.out.println(date3); // Sun Dec 26 08:00:00 CST 2021

因为Date类在设计上存在一些缺陷,所以Java提供了Calendar类来更好地处理日期和时间

JDK1.1引入Calendar类(java.util.Calendar),Calendar类虽然直译为日历,但也可以被理解为时间点。它是一个抽象类,通过getInstance()方法可以获得其子类实例,所获得子类的实例时GregorianCalendar类的对象

通过Calendar类提供的get()方法获得某个时间点的各个时间分量,get()方法的参数是整型数据,通常以Calendar类所定义的静态属性作为参数

Calendar 静态常量
静态常量 修饰符 说明 默认值
ERA static final int 0
YEAR static final int 年 1
MONTH static final int 月(从0开始) 2
WEEK_OF_YEAR static final int 当前年的第几周 3
WEEK_OF_MONTH static final int 当前月的星期数 4
DATE static final int 日(与DAY_OF_MONTH一样) 5
DAY_OF_MONTH static final int 日(与DATE一样) 5
DAY_OF_YEAR static final int 当前年的第几天 6
DAY_OF_WEEK static final int 当前星期几(用数字1-7表示星期日-星期六) 7
DAY_OF_WEEK_IN_MONTH static final int 当前月的第几个星期 8
AM_PM static final int 当前是上午还是下午(0是上午;1是下午) 9
HOUR static final int 小时(12小时制) 10
HOUR_OF_DAY static final int 小时(24小时制) 11
MINUTE static final int 分钟 12
SECOND static final int 秒 13
MILLISECOND static final int 毫秒 14
ZONE_OFFSET static final int 时区偏移量 15
DST_OFFSET static final int 16
FIELD_COUNT static final int 17
JANUARY static final int 1月 0
FEBRUARY static final int 2月 1
MARCH static final int 3月 2
APRIL static final int 4月 3
MAY static final int 5月 4
JUNE static final int 6月 5
JULY static final int 7月 6
AUGUST static final int 8月 7
SEPTEMBER static final int 9月 8
OCTOBER static final int 10月 9
NOVEMBER static final int 11月 10
DECEMBER static final int 12月 11
UNDECIMBER static final int 12
SUNDAY static final int 星期日 1
MONDAY static final int 星期一 2
TUESDAY static final int 星期二 3
WEDNESDAY static final int 星期三 4
THURSDAY static final int 星期四 5
FRIDAY static final int 星期五 6
SATURDAY static final int 星期六 7
AM static final int 上午 0
PM static final int 下午 1
SHORT static final int 1
SHORT_FORMAT static final int 1
SHORT_STANDALONE static final int
LONG static final int 2
LONG_FORMAT static final int 2
LONG_STANDALONE static final int
NARROW_FORMAT static final int 4
NARROW_STANDALONE static final int
ALL_STYLES static final int 0
isTimeSet protected boolean
areFieldsSet protected boolean
isSet protected boolean[]
fields protected int[]
time protected long
Calendar 常用方法
常用方法 返回值类型 说明
getInstance() static Calendar 获取当前日历对象(使用默认时区和语言环境)
getInstance(Locale aLocale) static Calendar 使用默认时区和指定语言环境获得一个日历对象
getInstance(TimeZone zone) static Calendar 使用指定时区和默认语言环境获得一个日历对象
getInstance(TimeZone zone,Locale aLocale) static Calendar 使用指定时区和指定语言环境获得一个日历对象
get(int field) int 获取指定字段的值
set(int field,int value) void 将给定的日历字段设置为给定值
set(int year,int month,int date) final void 设置日历对象的年月日三个字段的值
set(int year,int month,int date,int hourOfDay,int minute) final void 设置日历对象的年月日时分5个字段的值
set(int year,int month,int date,int hourOfDay,int minute,int second) final void 设置日历对象的年月日时分秒6个字段的值
getTime() final Date 返回Date对象
setTime(Date date) final void 使用给定的Date设置此日期对象的时间
getTimeInMillis() long 返回毫秒值
setTimeInMillis(long millis) void 使用给定的毫秒值来设置当前时间
getTimeZone() TimeZone 返回时区
setTimeZone(TimeZone value) void 设置时区
getFirstDayOfWeek() int 返回一星期的第一天;例如在美国是SUNDAY,而法国是MONDAY
setFirstDayOfWeek(int value) void 设置一星期的第一天是哪一个;例如美国是SUNDAY,法国是MONDAY
getMinimalDaysInFirstWeek() int 返回一年中第一个星期所需的最少天数
setMinimalDaysInFirstWeek(int value) void 设置一年中第一个星期所需的最少天数
getWeeksInWeekYear() int 返回当前时间是一年中的第几周
getWeekYear() int 获得当前年份
setWeekDate(int weekYear,int weekOfYear,int dayOfWeek) void 使用指定的整数值作为参数设置当前日期;weekYear是周年;weekOfYear是基于weekYear的星期数;dayOfWeek是星期几的值;
add(int field,int amount) abstract void 用于改变Calendar特定字段的值;如果需要增加某字段的值,则让amount为正数;如果需要减少某字段的值,则让amount为负数即可
roll(int field,int amount) void 与add方法类似,区别在于加上amount后超过了该字段所能表示的最大范围时,也不会向上一个字段进位
after(Object when) boolean 判断当前时间是否在指定的Object表示的时间之后
before(Object when) boolean 判断当前时间是否在指定的Object表示的时间之前
getAvailableCalendarTypes() static Set 返回当前运行环境中支持的所有日历类型
getAvailableLocales() static Locale[] 返回当前运行环境中全部语言环境的数组
getCalendarType() String 返回当前日历的类型
getMaximum(int field) abstract int 返回该日历对象给定日历字段的最大值
getMinimum(int field) abstract int 返回该日历对象给定日历字段的最小值
getActualMaximum(int field) int 返回日历对象指定字段可能拥有的最大值
getActualMinimum(int field) int 返回日历对象指定字段可能拥有的最小值
getGreatestMinimum(int field) abstract int 返回该日历对象给定日历字段的最高的最小值
getLeastMaximum(int field) abstract int 返回该日历对象给定日历字段的最低的最大值
setLenient(boolean lenient) void 设置Calendar的容错性
isLenient() boolean 判断Canlendar是否开启了容错性,默认开启了
isSet(int field) final boolean 检查给定日历字段是否设置了值
isWeekDateSupported() boolean 检查此日历是否支持星期日期;默认值为false
toInstant() final Instant 返回当前日历对象的Instant实例
toString() String 返回当前日历对象的字符串表示形式
equals(Object obj) boolean 判断当前时间对象与指定的Object是否相等
compareTo(Calendar anotherCalendar) int 比较两个日历对象表示的时间值
clone() Object 复制当前对象
hashCode() int 返回哈希码
computeFields() protected abstract void 将当前毫秒时间值time转换为fields[]中的日历字段值
computeTime() protected abstract void 将fields[]中的当前日历字段值转换为毫秒时间值time
getDisplayName(int field,int style,Locale locale) String 返回日历字段值在给定的风格和语言环境的字符串表示形式
getDisplayNames(int field,int style,Locale locale) Map 返回一个Map包含日历字段在给定的风格和语言环境和相应的字段值的所有名称
clear() final void 将当前对象设置为未定义
clear(int field) final void 将当前对象的指定字段设置为未定义
complete() protected void 填充日历字段中所有未设置的字段
internalGet(int field) protected final int 返回给定日历字段的值
Calendar c1 = Calendar.getInstance(); // 2021-12-26
c1.get(Calendar.YEAR); // 2021
c1.get(Calendar.MONTH); // 11
c1.get(Calendar.DATE); // 26

Calendar c2 = Calendar.getInstance();
c2.set(2021, 12, 26);
c2.add(Calendar.DATE, 2);
System.out.println(c2.get(Calendar.DATE)); // 28

JDK8 新增日期类总概括

由于Calendar的API还是不够给力,它的实例是易变的,并且它没有处理诸如闰秒这样的问题。所以在JavaSE8中引入了Date-Time API,它提供了一个日期和时间的综合模型。注意:JDK8新增的日期类中月份是从1-12

* 秒被捕获到纳秒精度
** 这个类不存储这些信息,但有方法在这些单元中提供时间
*** 将周期添加到ZonedDateTime时,会观察到夏令时或其他本地时差

Date-Time API 由主包 java.time 和四个子包组成:

  • java.time:表示日期和时间的API的核心。它包括日期,时间,日期和时间相结合的类别, 时区/zones,瞬间/instants,持续时间/duration和时钟/clocks。这些类基于ISO-8601中定义的日历系统, 并且不可变且线程安全
  • java.time.format:用于格式化和分析日期和时间的类
  • java.time.temporal:扩展API主要用于框架和库编写器,允许日期和时间类之间的互操作,查询和调整。字段(TemporalField和ChronoField)和单位(TemporalUnit和ChronoUnit)在此包中定义
  • java.time.zone:支持时区的类,时区的偏移和时区规则。如果使用时区,大多数开发人员只需使用ZonedDateTime和ZoneId或ZoneOffset
  • java.time.chrono:用于表示除默认ISO-8601以外的日历系统的API。您也可以定义自己的日历系统

DayOfWeek和Month枚举

DayOfWeek:一周7天的枚举;Month:一年12个月的枚举

DayOfWeek 枚举值
枚举值 说明 int值
MONDAY 星期一 1
TUESDAY 星期二 2
WEDNESDAY 星期三 3
THURSDAY 星期四 4
FRIDAY 星期五 5
SATURDAY 星期六 6
SUNDAY 星期日 7
DayOfWeek 常用方法
常用方法 返回值类型 说明
values() static DayOfWeek[] 按照声明的顺序返回包含此枚举类型的常量的数组
valueOf(String name) static DayOfWeek 返回具有指定名称的此类型的枚举常量
getValue() int 获取星期几int值
getDisplayName(TextStyle style, Locale locale) String 根据指定的文本样式和语言环境,返回星期几的文本字符串
of(int dayOfWeek) static DayOfWeek 从int值中获取DayOfWeek实例
from(TemporalAccessor temporal) static DayOfWeek 从时间对象中获取DayOfWeek实例
plus(long days) DayOfWeek 返回在此日期之后指定天数的星期几
minus(long days) DayOfWeek 返回在此日期之前指定天数的星期几
get(TemporalField field) int 获取此星期几中指定字段的值作为int
getLong(TemporalField field) long 从此星期几中获取指定字段的值作为long
isSupported(TemporalField field) boolean 检查是否支持指定的字段
query(TemporalQuery query) R 使用指定的查询策略对象查询此日期时间
range(TemporalField field) ValueRange 获取指定字段的有效值范围
adjustInto(Temporal temporal) Temporal
System.out.printf("%s%n", DayOfWeek.MONDAY.plus(3));
DayOfWeek dow = DayOfWeek.MONDAY;
// 全写:Monday
System.out.println(dow.getDisplayName(TextStyle.FULL, Locale.ENGLISH));
// 首字母:M
System.out.println(dow.getDisplayName(TextStyle.NARROW, Locale.ENGLISH));
// 缩写:Mon
System.out.println(dow.getDisplayName(TextStyle.SHORT, Locale.ENGLISH));
Month 枚举值
枚举值 说明 int值
JANUARY 1月;一月有31天 1
FEBRUARY 2月;一月有28天,闰年有29天 2
MARCH 3月;一月有31天 3
APRIL 4月;一月有30天 4
MAY 5月;一月有31天 5
JUNE 6月;一月有30天 6
JULY 7月;一月有31天 7
AUGUST 8月;一月有31天 8
SEPTEMBER 9月;一月有30天 9
OCTOBER 10月;一月有31天 10
NOVEMBER 11月;一月有30天 11
DECEMBER 12月;一月有31天 12
Month 常用方法
常用方法 返回值类型 说明
values() static Month[] 按照声明的顺序返回包含此枚举类型的常量的数组
valueOf(String name) static Month 返回具有指定名称的此类型的枚举常量
getValue() int 获取月份int值
getDisplayName(TextStyle style, Locale locale) String 根据指定的文本样式和语言环境,返回月份的文本字符串
length(boolean leapYear) int 获取本月的天数
of(int month) static Month 从int值中获取Month实例
from(TemporalAccessor temporal) static Month 从时间对象中获取Month实例
plus(long months) Month 返回在该季度之后指定季度数的月份
minus(long months) Month 返回在此月份之前指定月份数的月份
maxLength() int 获取本月的最大天数
minLength() int 获取本月的最短天数
firstDayOfYear(boolean leapYear) int 获取与本月第一天对应的年份
firstMonthOfQuarter() Month 获取本季度第一个月对应的月份
get(TemporalField field) int 从此月份中获取指定字段的值作为int
getLong(TemporalField field) long 从此月份中获取指定字段的值作为long
isSupported(TemporalField field) boolean 检查是否支持指定的字段
query(TemporalQuery query) R 使用指定的查询策略对象查询此日期时间
range(TemporalField field) ValueRange 获取指定字段的有效值范围
adjustInto(Temporal temporal) Temporal
System.out.printf("%d%n", Month.FEBRUARY.maxLength());
Month month = Month.AUGUST;
System.out.println(month.getDisplayName(TextStyle.FULL, Locale.ENGLISH));
System.out.println(month.getDisplayName(TextStyle.NARROW, Locale.ENGLISH));
System.out.println(month.getDisplayName(TextStyle.SHORT, Locale.ENGLISH));

Java8新增日期类

日期API提供四个专门处理日期信息的类,不考虑时间或时区:LocalDate,Year,YearMonth,MonthDay

一个LocalDate代表年月日的ISO日历,表示没有时间的日期是有用的。您可以使用LocalDate跟踪重大事件, 例如出生日期或结婚日期。以下示例使用of和with方法来创建LocalDate的实例

LocalDate 常用方法
常用方法 返回值类型 说明
now() static LocalDate 从默认时区的系统时钟获取当前日期
now(ZoneId zone) static LocalDate 指定时区的系统时钟获取当前日期
now(Clock clock) static LocalDate 从指定的时钟获取当前日期
of(int year, Month month, int dayOfMonth) static LocalDate 根据指定的年月日获得LocalDate实例;日期必须对年月有效否则抛出异常
of(int year, int month, int dayOfMonth) static LocalDate 根据指定的年月日获得LocalDate实例;日期必须对年月有效否则抛出异常
ofYearDay(int year, int dayOfYear) static LocalDate 根据指定的年和具体的天数获得LocalDate实例
ofInstant(Instant instant, ZoneId zone) static LocalDate
ofEpochDay(long epochDay) static LocalDate 从1970-01-01开始获取指定天数的LocalDate实例
toEpochSecond(LocalTime time, ZoneOffset offset) long 将此日期转换为大纪元日(1970-01-01)
with(TemporalAdjuster adjuster) LocalDate 根据指定的调整策略对象,将调整后的此日期副本返回
with(TemporalField field, long newValue) LocalDate 根据指定字段,将调整后的此日期副本返回
withYear(int year) LocalDate 返回更改年份后的LocalDate副本
withMonth(int month) LocalDate 返回更改月份后的LocalDate副本
withDayOfYear(int dayOfYear) LocalDate 返回更改日期(从1到365-366)后的LocalDate副本
withDayOfMonth(int dayOfMonth) LocalDate 返回更改月份日期(从1到28-31)后的LocalDate副本
from(TemporalAccessor temporal) static LocalDate 从时间对象中获取LocalDate实例
atStartOfDay() LocalDateTime 获取一天的开始时间
atStartOfDay(ZoneId zone) ZonedDateTime 从该日期在最早的有效时间返回一个分区的日期时间
parse(CharSequence text) static LocalDate 从文本字符串中获取LocalDate实例
parse(CharSequence text, DateTimeFormatter formatter) static LocalDate 使用特定的格式化程序从文本字符串中获取LocalDate的实例
format(DateTimeFormatter formatter) String 使用指定的格式化程序格式化此日期
getYear() int 获取年份字段
getMonth() Month 使用Month枚举获取月份字段
getMonthValue() int 获取从1到12的月份字段
getDayOfYear() int 获取一年中的第几天
getDayOfMonth() int 获取月份中的第几天
getDayOfWeek() DayOfWeek 使用DayOfWeek枚举获取星期字段
getChronology() IsoChronology 获取此日期的日历系统
getEra() IsoEra 获取适用于该日期的时代
isLeapYear() boolean 检查年份是否为闰年
isBefore(ChronoLocalDate other) boolean 检查此日期是否早于指定日期
isAfter(ChronoLocalDate other) int 检查此日期是否在指定日期之后
isEqual(ChronoLocalDate other) boolean 检查此日期是否等于指定日期
lengthOfYear() int 返回一年的长度(以天为单位),即365或366
lengthOfMonth() int 返回一个月的天数
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 将此日期和参数时间结合以创建LocalDateTime
atTime(OffsetTime time) OffsetDateTime 将此日期和参数偏移时间结合以创建OffsetDateTime
plus(TemporalAmount amountToAdd) LocalDate 返回添加了指定数量的此日期的副本
plus(long amountToAdd, TemporalUnit unit) LocalDate 返回添加了指定数量的此日期的副本
plusYears(long yearsToAdd) LocalDate 添加了指定年数LocalDate副本
plusMonths(long monthsToAdd) LocalDate 添加了指定月数LocalDate副本
plusWeeks(long weeksToAdd) LocalDate 添加了指定周数LocalDate副本
plusDays(long daysToAdd) LocalDate 添加了指定天数LocalDate副本
minus(TemporalAmount amountToSubtract) LocalDate 返回该日期减去指定数量的副本
minus(long amountToSubtract, TemporalUnit unit) LocalDate 返回该日期减去指定数量的副本
minusYears(long yearsToSubtract) LocalDate 减去指定年数后LocalDate的副本
minusMonths(long monthsToSubtract) LocalDate 减去指定月数后LocalDate的副本
minusWeeks(long weeksToSubtract) LocalDate 减去指定周数后LocalDate的副本
minusDays(long daysToSubtract) LocalDate 减去指定日数后LocalDate的副本
until(Temporal endExclusive, TemporalUnit unit) long 根据指定的单位计算距另一个日期的时间量
until(ChronoLocalDate endDateExclusive) Period 将此日期与另一个日期之间的时间段计算为Period
datesUntil(LocalDate endExclusive) Stream
datesUntil(LocalDate endExclusive, Period step) Stream
compareTo(ChronoLocalDate other) int 将此日期与另一个日期进行比较
equals(Object obj) boolean 检查此日期是否等于另一个日期
hashCode() int 返回此日期的哈希码
toString() String 将此日期输出为String
get(TemporalField field) int 从该日期获取指定字段的值作为int
getLong(TemporalField field) long 从此日期获取指定字段的值作为long
isSupported(TemporalField field) boolean 检查是否支持指定的字段
isSupported(TemporalUnit unit) boolean 检查是否支持指定的单位
query(TemporalQuery query) R 使用指定的查询查询此日期
range(TemporalField field) ValueRange 获取指定字段的有效值范围
adjustInto(Temporal temporal) Temporal 将指定的时间对象调整为与此对象具有相同的日期
Year代表年,常用方法
常用方法 返回值类型 说明
now() static Year 从默认时区的系统时钟获取当前年份
now(ZoneId zone) static Year 从指定时区的系统时钟获取当前年份
now(Clock clock) static Year 从指定的时钟获取当前年份
of(int isoYear) static Year 根据指定的年获得Year实例
from(TemporalAccessor temporal) static Year 从时间对象中获取Year实例
with(TemporalAdjuster adjuster) Year 返回今年的调整副本
with(TemporalField field, long newValue) Year 返回这一年的副本,并将指定字段设置为新值
parse(CharSequence text) static Year 从文本字符串中获取Year实例
parse(CharSequence text, DateTimeFormatter formatter) static Year 使用特定的格式化程序从文本字符串中获取Year实例
format(DateTimeFormatter formatter) String 使用指定的格式化程序格式化今年
getValue() int 获取年份值
length() int 以天为单位获取今年的长度
isBefore(Year other) boolean 检查今年是否在指定年份之前
isAfter(Year other) boolean 检查今年是否在指定年份之后
isLeap() boolean 检查年份是否为闰年
isLeap(long year) static boolean 检查年份是否为闰年
isValidMonthDay(MonthDay monthDay) boolean 检查月日对今年是否有效
atMonthDay(MonthDay monthDay) LocalDate 将今年与月日结合起来创建一个LocalDate
atMonth(Month month) YearMonth 将今年与一个月结合起来创建一个YearMonth
atMonth(int month) YearMonth 将今年与一个月结合起来创建一个YearMonth
atDay(int dayOfYear) LocalDate 将今年与一年中的某一天结合起来创建一个LocalDate
plus(TemporalAmount amountToAdd) Year 返回添加了指定数量的今年的副本
plus(long amountToAdd, TemporalUnit unit) Year 返回添加了指定数量的今年的副本
plusYears(long yearsToAdd) Year 返回添加了指定年数的副本
minus(TemporalAmount amountToSubtract) Year 返回减去指定金额的今年的副本
minus(long amountToSubtract, TemporalUnit unit) Year 返回减去指定金额的今年的副本
minusYears(long yearsToSubtract) Year 返回Year减去指定年数后的副本
until(Temporal endExclusive, TemporalUnit unit) long 根据指定的单位计算距另一年的时间量
compareTo(Year other) int 将今年与另一年进行比较
equals(Object obj) boolean 检查今年是否等于另一年
hashCode() int 今年的哈希码
toString() String 今年的输出为String
get(TemporalField field) int 获取今年指定字段的值作为int
getLong(TemporalField field) long 获取今年指定字段的值作为long
isSupported(TemporalField field) boolean 检查是否支持指定的字段
isSupported(TemporalUnit unit) boolean 检查是否支持指定的单位
query(TemporalQuery query) R 使用指定查询的今年查询
range(TemporalField field) ValueRange 获取指定字段的有效值范围
adjustInto(Temporal temporal) Temporal 将指定的时间对象调整为今年
YearMonth代表年月,常用方法
常用方法 返回值类型 说明
now() static YearMonth 从默认时区的系统时钟获取当前年月
now(ZoneId zone) static YearMonth 从指定时区的系统时钟获取当前年月
now(Clock clock) static YearMonth 从指定的时钟获取当前的年月
of(int year, Month month) static YearMonth 从指定的年月获取YearMonth实例
of(int year, int month) static YearMonth 从指定的年月获取YearMonth实例
from(TemporalAccessor temporal) static YearMonth 从时间对象中获取YearMonth实例
with(TemporalAdjuster adjuster) YearMonth 根据指定的调整策略对象,将调整后的此日期副本返回
with(TemporalField field, long newValue) YearMonth 根据指定字段,将调整后的此日期副本返回
withYear(int year) YearMonth 返回更改年份后的YearMonth副本
withMonth(int month) YearMonth 返回更改月份后的YearMonth副本
parse(CharSequence text) static YearMonth 从文本字符串中获取YearMonth实例
parse(CharSequence text, DateTimeFormatter formatter) static YearMonth 使用特定的格式化程序从文本字符串中获取YearMonth的实例
format(DateTimeFormatter formatter) String 使用指定的格式化程序格式化此日期
getYear() int 获取年份字段
getMonth() Month 使用Month枚举获取月份字段
getMonthValue() int 获取从1到12的月份字段
isLeapYear() boolean 检查年份是否为闰年
isBefore(YearMonth other) boolean 检查此日期是否早于指定日期
isAfter(YearMonth other) boolean 检查此日期是否在指定日期之后
isValidDay(int dayOfMonth) boolean 检查月份中的某一天是否对今年的月份有效
lengthOfYear() int 返回一年的长度(以天为单位),即365或366
lengthOfMonth() int 返回一个月的天数
atDay(int dayOfMonth) LocalDate 将此年月与月中的某一天相结合以创建一个LocalDate
atEndOfMonth() LocalDate 返回一个LocalDate,这个日期设置为该月的最后一个有效日期
plus(TemporalAmount amountToAdd) YearMonth 返回添加了指定数量的此日期的副本
plus(long amountToAdd, TemporalUnit unit) YearMonth 返回添加了指定数量的此日期的副本
plusYears(long yearsToAdd) YearMonth 添加了指定年数YearMonth副本
plusMonths(long monthsToAdd) YearMonth 添加了指定月数YearMonth副本
minus(TemporalAmount amountToSubtract) YearMonth 返回该日期减去指定数量的副本
minus(long amountToSubtract, TemporalUnit unit) YearMonth 返回该日期减去指定数量的副本
minusYears(long yearsToSubtract) YearMonth 减去指定年数后YearMonth的副本
minusMonths(long monthsToSubtract) YearMonth 减去指定月数后YearMonth的副本
until(Temporal endExclusive, TemporalUnit unit) long 根据指定的单位计算距另一个日期的时间量
compareTo(YearMonth other) int 将此日期与另一个日期进行比较
equals(Object obj) boolean 检查此日期是否等于另一个日期
hashCode() int 返回此日期的哈希码
toString() String 将此日期输出为String
get(TemporalField field) int 从该日期获取指定字段的值作为int
getLong(TemporalField field) long 从此日期获取指定字段的值作为long
isSupported(TemporalField field) boolean 检查是否支持指定的字段
isSupported(TemporalUnit unit) boolean 检查是否支持指定的单位
query(TemporalQuery query) R 使用指定的查询查询此日期
range(TemporalField field) ValueRange 获取指定字段的有效值范围
adjustInto(Temporal temporal) Temporal 将指定的时间对象调整为与此对象具有相同的日期
MonthDay代表月日,如--12-03,常用方法
常用方法 返回值类型 说明
now() static MonthDay 从默认时区的系统时钟获取当前月日
now(ZoneId zone) static MonthDay 从指定时区的系统时钟获取当前月日
now(Clock clock) static MonthDay 从指定的时钟获取当前的月日
of(int month, int dayOfMonth) static MonthDay 从指定的月日获取MonthDay实例
of(Month month, int dayOfMonth) static MonthDay 从指定的月日获取MonthDay实例
from(TemporalAccessor temporal) static MonthDay 从时间对象中获取MonthDay实例
with(Month month) MonthDay 返回更改月份后的MonthDay副本
withMonth(int month) MonthDay 返回更改月份后的MonthDay副本
withDayOfMonth(int dayOfMonth) MonthDay 返回更改日期后的MonthDay副本
parse(CharSequence text) static MonthDay 从文本字符串中获取MonthDay实例
parse(CharSequence text, DateTimeFormatter formatter) static MonthDay 使用特定的格式化程序从文本字符串中获取MonthDay的实例
format(DateTimeFormatter formatter) String 使用指定的格式化程序格式化此日期
getMonth() Month 使用Month枚举获取月份字段
getMonthValue() int 获取从1到12的月份字段
getDayOfMonth() int 获取日期字段(从1到31)
isBefore(MonthDay other) boolean 检查此日期是否早于指定日期
isAfter(MonthDay other) boolean 检查此日期是否在指定日期之后
isValidYear(int year) boolean 检查年份是否对该月日有效
atYear(int year) LocalDate 将这个月日与指定年份结合起来创建一个LocalDate
compareTo(MonthDay other) int 将此日期与另一个日期进行比较
equals(Object obj) boolean 检查此日期是否等于另一个日期
hashCode() int 返回此日期的哈希码
toString() String 将此日期输出为String
get(TemporalField field) int 从该日期获取指定字段的值作为int
getLong(TemporalField field) long 从此日期获取指定字段的值作为long
isSupported(TemporalField field) boolean 检查是否支持指定的字段
query(TemporalQuery query) R 使用指定的查询查询此日期
range(TemporalField field) ValueRange 获取指定字段的有效值范围
adjustInto(Temporal temporal) Temporal 将指定的时间对象调整为与此对象具有相同的日期

// 2000年11月20日 星期一
LocalDate date = LocalDate.of(2000, Month.NOVEMBER, 20);
// 当前指定日期的下一个 星期三
LocalDate nextWed = date.with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));
// 当前指定日期的下一个 星期一
LocalDate nextMond = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));

// 获取LocalDate时间分量
LocalDate date = LocalDate.of(2021, 12, 26);
System.out.println(date.getYear()); // 年:2021
System.out.println(date.get(ChronoField.YEAR)); // 年:2021
System.out.println(date.range(ChronoField.YEAR)); // 年份的范围:-999999999 - 999999999
System.out.println(date.range(ChronoField.DAY_OF_MONTH)); // 每月天数范围:1 - 31
System.out.println(date.getMonth()); // 月份枚举:DECEMBER
System.out.println(date.getMonth().getValue()); // 月份数字:12
System.out.println(date.getMonthValue()); // 月份数字:12
System.out.println(date.getDayOfMonth()); // 日期:26
System.out.println(date.getDayOfWeek()); // 星期:SUNDAY
System.out.println(date.getDayOfWeek().getValue()); // 星期:7
System.out.println(date.getDayOfYear()); // 一年中的第几天:360
System.out.println(date.lengthOfMonth()); // 一个月有几天:31
System.out.println(date.lengthOfYear()); // 一年有几天:265
System.out.println(date.isLeapYear()); // 判断是否是闰年:false
System.out.println(date.getEra()); // CE是公元后的年份;BCE是公元前的年份
System.out.println(date.toEpochDay()); // 与1970-1-1相差的天数:18987
System.out.println(date.getChronology()); // 获取各种历法的纪年方式:ISO 公历纪年法
System.out.println(date.getLong(ChronoField.EPOCH_DAY)); // 返回是long类型
System.out.println(date.isSupported(ChronoField.MILLI_OF_DAY)); // 是否支持该常量:false


// 修改LocalDate时间分量
LocalDate date = LocalDate.of(2021, 12, 26);
LocalDate with = date.withMonth(3); // 把月份修改为指定日期
System.out.println(with); // 2021-03-26
LocalDate with1 = date.with(ChronoField.MONTH_OF_YEAR, 2);
System.out.println(with1); // 2021-02-26
LocalDate date2 = LocalDate.of(2021, 10, 1);
LocalDate with2 = date.with(date2);
System.out.println(with2); // 2021-10-01

// 增加、减少时间分量
LocalDate date = LocalDate.of(2021, 12, 26);
LocalDate plusYears = date.plusYears(1);
System.out.println(plusYears); // 2022-12-26
LocalDate plus1 = date.plus(1, ChronoUnit.DAYS);
System.out.println(plus1); // 2021-12-27
Period period = Period.of(1, 2, 3);
LocalDate plus2 = date.plus(period);
System.out.println(plus2); // 2023-03-01

// 日期的比较
LocalDate date1 = LocalDate.of(2021, 11, 26);
LocalDate date2 = LocalDate.of(2021, 12, 26);
System.out.println(date1.equals(date2)); // false;equals比较的必须是LocalDate对象
HijrahDate hDate = HijrahDate.of(1400, 6, 27);
System.out.println(date1.isEqual(hDate)); // false;isEqual可比较任意对象
System.out.println(date1.isBefore(date2)); // true
System.out.println(date1.isAfter(date2)); // false
System.out.println(date1.compareTo(date2)); // -1

// 计算日期差
LocalDate date1 = LocalDate.of(2021, 11, 26);
LocalDate date2 = LocalDate.of(2021, 12, 26);
System.out.println(date1.toEpochDay() - date2.toEpochDay()); // -30;两个日期之间相差多少天
System.out.println(date2.until(date1, ChronoUnit.DAYS)); // -30;date2到date1之间相差多少个星期
System.out.println(date2.until(date1)); // P-1M;返回的是Period对象:P表示Period对象,Y表示年,M表示月,D表示天
Stream dates1 = date1.datesUntil(date2); // 获取两个日期之间的每一天(注意:不包含最后一天)
Object[] array1 = dates1.toArray();
for (int i = 0; i < array1.length; i++) {
    System.out.println((LocalDate) array1[i]);
}
Stream dates2 = date1.datesUntil(date2, Period.ofDays(2)); // 获取两个日期之间指定步长的天数(不包含最后一天)
Object[] array2 = dates2.toArray();
for (int i = 0; i < array2.length; i++) {
    System.out.println((LocalDate) array2[i]);
}

// 判断特殊日期
LocalDate date1 = LocalDate.of(2021, 12, 26);
LocalDate date2 = LocalDate.of(2021, 11, 26);
MonthDay birthDay = MonthDay.of(date1.getMonth(), date1.getDayOfMonth());
MonthDay today = MonthDay.from(date2);
System.out.println(birthDay.equals(today));

Java8新增时间类

LocalTime 常用方法
常用方法 返回值类型 说明
now() static LocalTime 从默认时区的系统时钟中获取当前时间
now(ZoneId zone) static LocalTime 从指定时区的系统时钟中获取当前时间
now(Clock clock) static LocalTime 从指定的时钟获取当前时间
of(int hour, int minute) static LocalTime 按照指定的参数返回一个LocalTime实例
of(int hour, int minute, int second) static LocalTime 按照指定的参数返回一个LocalTime实例
of(int hour, int minute, int second, int nanoOfSecond) static LocalTime 按照指定的参数返回一个LocalTime实例
ofSecondOfDay(long secondOfDay) static LocalTime 根据指定的秒返回LocalTime实例
ofNanoOfDay(long nanoOfDay) static LocalTime 根据指定的纳秒返回LocalTime实例
with(TemporalAdjuster adjuster) LocalTime 返回此时间的调整副本
with(TemporalField field, long newValue) LocalTime 返回此时间的副本,并将指定字段设置为新值
withHour(int hour) LocalTime 返回更改了小时的LocalTime副本
withMinute(int minute) LocalTime 返回更改了分钟的LocalTime副本
withSecond(int second) LocalTime 返回更改了秒的LocalTime副本
withNano(int nanoOfSecond) LocalTime 返回更改了纳秒的LocalTime副本
from(TemporalAccessor temporal) static LocalTime 从时间对象中获取LocalTime实例
atDate(LocalDate date) LocalDateTime 将此时间与日期结合以创建LocalDateTime
atOffset(ZoneOffset offset) OffsetTime 将此时间与偏移量相结合以创建OffsetTime
truncatedTo(TemporalUnit unit) LocalTime 返回截断了指定单位的LocalTime副本
parse(CharSequence text) static LocalTime 从文本字符串中获取LocalTime的实例
parse(CharSequence text, DateTimeFormatter formatter) static LocalTime 使用特定的格式化程序从文本字符串中获取LocalTime实例
format(DateTimeFormatter formatter) String 使用指定的格式化程序进行格式化
getHour() int 获取小时字段
getMinute() int 获取分钟字段
getSecond() int 获取秒的分钟字段
getNano() int 获取纳秒级字段
toSecondOfDay() int 返回当前时间的秒数
toNanoOfDay() long 返回当前时间的纳秒数
isBefore(LocalTime other) boolean 检查此时间是否早于指定时间
isAfter(LocalTime other) boolean 检查此时间是否在指定时间之后
plus(TemporalAmount amountToAdd) LocalTime 返回添加指定的数量后的LocalTime副本
plus(long amountToAdd, TemporalUnit unit) LocalTime 返回添加指定的数量的LocalTime副本
plusHours(long hoursToAdd) LocalTime 添加了指定小时数的LocalTime副本
plusMinutes(long minutesToAdd) LocalTime 添加了指定分钟数的LocalTime副本
plusSeconds(long secondstoAdd) LocalTime 添加了指定秒数的LocalTime副本
plusNanos(long nanosToAdd) LocalTime 添加了指定纳秒数的LocalTime副本
minus(TemporalAmount amountToSubtract) LocalTime 返回减去指定的数量后的LocalTime副本
minus(long amountToSubtract, TemporalUnit unit) LocalTime 返回减去指定的数量的LocalTime副本
minusHours(long hoursToSubtract) LocalTime 减去指定小时数的LocalTime副本
minusMinutes(long minutesToSubtract) LocalTime 减去指定分钟数的LocalTime副本
minusSeconds(long secondsToSubtract) LocalTime 减去指定秒数的LocalTime副本
minusNanos(long nanosToSubtract) LocalTime 减去指定纳秒数的LocalTime副本
until(Temporal endExclusive, TemporalUnit unit) long 根据指定的单位计算到另一个时间的时间量
compareTo(LocalTime other) int 将这次与另一次进行比较
equals(Object obj) boolean 检查这个时间是否等于另一个时间
hashCode() int 返回哈希码
toString() String 这次输出为当前时间的String
get(TemporalField field) int 从此时间获取指定字段的值作为int
getLong(TemporalField field) long 从此时间获取指定字段的值作为long
isSupported(TemporalField field) boolean 检查是否支持指定的字段
isSupported(TemporalUnit unit) boolean 检查是否支持指定的单位
query(TemporalQuery query) R 使用指定的查询进行查询
range(TemporalField field) ValueRange 获取指定字段的有效值范围
adjustInto(Temporal temporal) Temporal 将指定的时间对象调整为与此对象具有相同的时间

// 创建LocalTime对象
LocalTime now1 = LocalTime.now();
LocalTime now2 = LocalTime.of(12, 30);
LocalTime now3 = LocalTime.ofSecondOfDay(60);
LocalTime now4 = LocalTime.NOON;
System.out.println(now1); // 17:46:42.489430500
System.out.println(now2); // 12:30
System.out.println(now3); // 00:01
System.out.println(now4); // 12:00
LocalDateTime 常用方法
常用方法 返回值类型 说明
now() static LocalDateTime 从默认时区的系统时钟获取当前日期时间
now(ZoneId zone) static LocalDateTime 从指定时区的系统时钟获取当前日期时间
now(Clock clock) static LocalDateTime 从指定的时钟获取当前日期时间
of(int year, Month month, int dayOfMonth, int hour, int minute) static LocalDateTime 按照指定的参数返回一个LocalDateTime实例
of(int year, Month month, int dayOfMonth, int hour, int minute, int second) static LocalDateTime 按照指定的参数返回一个LocalDateTime实例
of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) static LocalDateTime 按照指定的参数返回一个LocalDateTime实例
of(int year, int month, int dayOfMonth, int hour, int minute) static LocalDateTime 按照指定的参数返回一个LocalDateTime实例
of(int year, int month, int dayOfMonth, int hour, int minute, int second) static LocalDateTime 按照指定的参数返回一个LocalDateTime实例
of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) static LocalDateTime 按照指定的参数返回一个LocalDateTime实例
of(LocalDate date, LocalTime time) static LocalDateTime 按照指定的参数返回一个LocalDateTime实例
ofInstant(Instant instant, ZoneId zone) static LocalDateTime 按照指定的参数返回一个LocalDateTime实例
ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) static LocalDateTime 从1970-01-01T00:00:00Z的纪元获取LocalDateTime实例
from(TemporalAccessor temporal) static LocalDateTime 这将根据指定的时间获得本地日期时间
with(TemporalAdjuster adjuster) LocalDateTime 返回此日期时间的调整副本
with(TemporalField field, long newValue) LocalDateTime 返回此日期时间的副本,并将指定字段设置为新值
withYear(int year) LocalDateTime 返回更改年份的LocalDateTime副本
withMonth(int month) LocalDateTime 返回更改了月份的LocalDateTime副本
withDayOfMonth(int dayOfMonth) LocalDateTime 返回更改了月份日期的LocalDateTime副本
withDayOfYear(int dayOfYear) LocalDateTime 返回更改了日期的LocalDateTime副本
withHour(int hour) LocalDateTime 返回更改了一天中的小时的LocalDateTime副本
withMinute(int minute) LocalDateTime 返回更改了分钟的LocalDateTime副本
withSecond(int second) LocalDateTime 返回更改了秒的LocalDateTime副本
withNano(int nanoOfSecond) LocalDateTime 返回一个更改了纳秒级的LocalDateTime副本
atOffset(ZoneOffset offset) OffsetDateTime 将此日期时间与偏移量组合以创建OffsetDateTime
atZone(ZoneId zone) ZonedDateTime 将此日期时间与时区组合以创建ZonedDateTime
toLocalDate() LocalDate 获取此日期的LocalDate部分
toLocalTime() LocalTime 获取此日期的LocalTime部分
truncatedTo(TemporalUnit unit) LocalDateTime 返回截断时间的LocalDateTime副本
parse(CharSequence text) LocalDateTime 从文本字符串中获取LocalDateTime实例
parse(CharSequence text, DateTimeFormatter formatter) LocalDateTime 使用特定的格式化程序从文本字符串中获取LocalDateTime实例
format(DateTimeFormatter formatter) String 使用指定的格式化程序格式化此日期时间
getYear() int 获取年份字段
getMonth() Month 使用Month枚举获取月份字段
getMonthValue() int 获取从1到12的月份字段
getDayOfYear() int 获取一年中的第几天
getDayOfMonth() int 获取一个月中的第几天
getDayOfWeek() DayOfWeek 获取星期几字段
getHour() int 获取小时字段
getMinute() int 获取分钟字段
getSecond() int 获取秒的分钟字段
getNano() int 获取纳秒级字段
isBefore(ChronoLocalDateTime other) boolean 检查此日期时间是否在指定的日期时间之前
isAfter(ChronoLocalDateTime other) boolean 检查此日期时间是否在指定的日期时间之后
isEqual(ChronoLocalDateTime other) boolean 检查此日期时间是否等于指定的日期时间
plus(TemporalAmount amountToAdd) LocalDateTime 返回添加了指定数量的此日期时间的副本
plus(long amountToAdd, TemporalUnit unit) LocalDateTime 返回添加了指定数量的此日期时间的副本
plusYears(long years) LocalDateTime 返回添加了指定年数的LocalDateTime副本
plusMonths(long months) LocalDateTime 返回添加了指定月数的LocalDateTime副本
plusWeeks(long weeks) LocalDateTime 返回添加了指定周数的LocalDateTime副本
plusDays(long days) LocalDateTime 返回添加了指定天数的LocalDateTime副本
plusHours(long hours) LocalDateTime 返回添加了指定小时数的LocalDateTime副本
plusMinutes(long minutes) LocalDateTime 返回添加了指定分钟数的LocalDateTime副本
plusSeconds(long seconds) LocalDateTime 返回添加了指定秒数的LocalDateTime副本
plusNanos(long nanos) LocalDateTime 返回添加了指定纳秒数的LocalDateTime副本
minus(TemporalAmount amountToSubtract) LocalDateTime 返回此日期时间的副本,减去指定的数量
minus(long amountToSubtract, TemporalUnit unit) LocalDateTime 返回此日期时间的副本,减去指定的数量
minusYears(long years) LocalDateTime 返回减去指定年数后的LocalDateTime副本
minusMonths(long months) LocalDateTime 返回减去指定月数后的LocalDateTime副本
minusWeeks(long weeks) LocalDateTime 返回减去指定周数后的LocalDateTime副本
minusDays(long days) LocalDateTime 返回减去指定天数后的LocalDateTime副本
minusHours(long hours) LocalDateTime 返回减去指定小时数后的LocalDateTime副本
minusMinutes(long minutes) LocalDateTime 返回减去指定分钟数后的LocalDateTime副本
minusSeconds(long seconds) LocalDateTime 返回减去指定秒数后的LocalDateTime副本
minusNanos(long nanos) LocalDateTime 返回减去指定纳秒数的LocalDateTime副本
until(Temporal endExclusive, TemporalUnit unit) long 根据指定的单位计算距另一个日期时间的时间量
compareTo(ChronoLocalDateTime other) int 将此日期时间与另一个日期时间进行比较
equals(Object obj) boolean 检查此日期时间是否等于另一个日期时间
hashCode() int 此日期时间的哈希码
toString() String 将此日期时间输出为String
get(TemporalField field) int 从此日期时间中获取指定字段的值作为int
getLong(TemporalField field) long 从此日期时间中获取指定字段的值作为long
isSupported(TemporalField field) boolean 检查是否支持指定的字段
isSupported(TemporalUnit unit) boolean 检查是否支持指定的单位
query(TemporalQuery query) R 使用指定的查询查询此日期时间
range(TemporalField field) ValueRange 获取指定字段的有效值范围
adjustInto(Temporal temporal) Temporal 将指定的时间对象调整为与此对象具有相同的日期和时间
LocalDateTime date = LocalDateTime.of(2021, 12, 26, 18, 30);
System.out.println(date); // 2021-12-26T18:30(大写字母T是英文单词Time的缩写)

周期和持续时间

Period表示:一段时间的区间,用来度量年月日和几天之间的时间值

  • 构造方式:通过ofXXX()方法,并传递参数来获得;这种方式得到的Period对象,年月日3个计数器上的数值不会自动进位转换
    • 注意:Period不能自动进位处理,但可通过手动调用normalized方法来进位处理日期(这个方法只能规范年和月之间的进位,关于日和月之间没有进位)
  • 计算方式:计算两个日期之间的时间差来获得

ChronoUnit:可以测量任意一个单位的间隔时间;也提供了各种单位常量

Period 常用方法
常用方法 返回值类型 说明
of(int years, int months, int days) static Period 获得代表年、月和日的Period
ofYears(int years) static Period 获得代表年数的Period
ofMonths(int months) static Period 获得代表月份的Period
ofDays(int days) static Period 获取代表天数的Period
ofWeeks(int weeks) static Period 获得代表周数的Period
between(LocalDate startDateInclusive, LocalDate endDateExclusive) static Period 获得由两个日期之间的年数、月数和天数组成的Period
withYears(int years) Period 返回具有指定年数的此期间的副本
withMonths(int months) Period 返回具有指定月数的此期间的副本
withDays(int days) Period 返回具有指定天数的此期间的副本
from(TemporalAmount amount) static Period 从时间量中获取Period实例
parse(CharSequence text) static Period 从文本字符串中获取Period实例
negated() Period 返回一个新实例,该时间段内的每个金额都被否定
normalized() Period 返回此期间的副本,其中年和月已标准化
get(TemporalUnit unit) long 获取请求单位的值
getChronology() IsoChronology 获取这一时期的年表,即ISO日历系统
getYears() int 获取此期间的年数
getMonths() int 获取此期间的月数
getDays() int 获取此期间的天数
getUnits() List
isZero() boolean 检查此期间的所有三个单位是否都为零
isNegative() boolean 检查此期间的三个单位中的任何一个是否为负
toTotalMonths() long 获取此期间的总月数
addTo(Temporal temporal) Temporal 将此时间段添加到指定的时间对象
subtractFrom(Temporal temporal) Temporal 从指定的时间对象中减去此时间段
plus(TemporalAmount amountToAdd) Period 返回添加了指定期间的此期间的副本
plusYears(long yearsToAdd) Period 返回添加了指定年份的此期间的副本
plusMonths(long monthsToAdd) Period 返回此期间的副本,其中添加了指定的月份
plusDays(long daysToAdd) Period 返回添加了指定天数的此期间的副本
minus(TemporalAmount amountToSubtract) Period 返回此期间减去指定期间的副本
minusYears(long yearsToSubtract) Period 返回此期间减去指定年份的副本
minusMonths(long monthsToSubtract) Period 返回此期间减去指定月份的副本
minusDays(long daysToSubtract) Period 返回此期间减去指定天数的副本
multipliedBy(int scalar) Period 返回一个新实例,该实例中的每个元素都乘以指定的标量
equals(Object obj) boolean 检查此期间是否等于另一个期间
hashCode() int 这个时期的哈希码
toString() String 将此时间段输出为String,例如P6Y3M1D
// 创建Period对象
Period p = Period.of(1, 2, 3);
System.out.println(p); // P1Y2M3D
LocalDate ld1 = LocalDate.of(2021, 12, 26);
LocalDate ld2 = LocalDate.of(2021, 11, 26);
Period period = Period.between(ld1, ld2);
System.out.println(period); // P-1M

Duration:表示时间的区间,用来度量秒和纳秒之间的时间值(Duration有明确的进位关系,所以可以正确进位)

  • Duration类中内置了秒和纳秒两个计数器,其中纳秒计数器的合法数值范围是0-999999999,不能为负数
  • 虽然Duration类只有秒和纳秒两个计数器,但其对象被打印出来却是以“时分秒”的格式来展现的
  • 虽然纳秒计数器上的值不能为负数,但是再使用of()方法创建Duration对象的时候,却可以给表示纳秒的参数传递负数,在实际创建对象过程中,虚拟机会从秒计数器是哪个借来1秒修正纳秒计数器上的值为正值
  • Duration输出格式:PT83H19M59.999998S
Duration 常用方法
常用方法 返回值 说明
of(long amount, TemporalUnit unit) static Duration 获取以指定单位表示的Duration
ofDays(long days) static Duration 获取代表标准24小时天数的Duration
ofHours(long hours) static Duration 获取代表标准小时数的Duration
ofMinutes(long minutes) static Duration 获取代表标准分钟数的Duration
ofSeconds(long seconds) static Duration 获取代表秒数的Duration
ofSeconds(long seconds, long nanoAdjustment) static Duration 获取代表秒数和以纳秒为单位的调整Duration
ofMillis(long millis) static Duration 获取代表毫秒数的Duration
ofNanos(long nanos) static Duration 获得代表纳秒数的Duration
between(Temporal startInclusive, Temporal endExclusive) static Duration 获得一个表示两个时间对象之间的持续时间Duration
withSeconds(long seconds) Duration 以指定的秒数返回此持续时间的副本
withNanos(int nanoOfSecond) Duration 使用指定的纳秒返回此持续时间的副本
from(TemporalAmount amount) static Duration 从时间量中获取Duration
parse(CharSequence text) static Duration 从文本字符串中获取Duration
truncatedTo(TemporalUnit unit) Duration
abs() Duration 返回此持续时间的副本,长度为正
negated() Duration 返回此持续时间的副本,长度取反
isZero() boolean 检查此持续时间是否为零长度
isNegative() boolean 检查此持续时间是否为负,不包括零
get(TemporalUnit unit) long 获取请求单位的值
getSeconds() long 获取此持续时间内的秒数
getNano() int 获取此持续时间内秒内的纳秒数
getUnits() List 获取此持续时间支持的单位集
toDays() long 获取此持续时间的天数
toHours() long 获取此持续时间的小时数
toMinutes() long 获取此持续时间的分钟数
toSeconds() long 获取此持续时间的秒数
toMillis() long 将此持续时间转换为以毫秒为单位的总长度
toNanos() long 将此持续时间转换为以纳秒表示的总长度long
addTo(Temporal temporal) Temporal 将此持续时间添加到指定的时间对象
subtractFrom(Temporal temporal) Temporal 从指定的时间对象中减去此持续时间
plus(long amountToAdd, TemporalUnit unit) Duration 返回此持续时间的副本,并添加了指定的持续时间
plus(Duration duration) Duration 返回此持续时间的副本,并添加了指定的持续时间
plusDays(long daysToAdd) Duration 返回此持续时间的副本,并添加了标准 24 小时天中的指定持续时间
plusHours(long hoursToAdd) Duration 返回此持续时间的副本,并添加指定的持续时间(以小时为单位)
plusMinutes(long minutesToAdd) Duration 返回此持续时间的副本,并添加指定的持续时间(以分钟为单位)
plusSeconds(long secondsToAdd) Duration 返回此持续时间的副本,并添加指定的持续时间(以秒为单位)
plusMillis(long millisToAdd) Duration 返回此持续时间的副本,并添加指定的持续时间(以毫秒为单位)
plusNanos(long nanosToAdd) Duration 返回此持续时间的副本,并添加指定的纳秒持续时间
minus(long amountToSubtract, TemporalUnit unit) Duration 返回此持续时间减去指定持续时间的副本
minus(Duration duration) Duration 返回此持续时间减去指定持续时间的副本
minusDays(long daysToSubtract) Duration 返回此持续时间的副本,减去标准 24 小时天中的指定持续时间
minusHours(long hoursToSubtract) Duration 返回此持续时间的副本,并减去指定的持续时间(以小时为单位)
minusMinutes(long minutesToSubtract) Duration 返回此持续时间的副本,并减去指定的持续时间(以分钟为单位)
minusSeconds(long secondsToSubtract) Duration 返回此持续时间的副本,并减去指定的持续时间(以秒为单位)
minusMillis(long millisToSubtract) Duration 返回此持续时间的副本,减去指定的持续时间(以毫秒为单位)
minusNanos(long nanosToSubtract) Duration 返回此持续时间的副本,减去指定的纳秒持续时间
multipliedBy(long multiplicand) Duration 返回此持续时间乘以标量的副本
dividedBy(long divisor) Duration 返回此持续时间除以指定值的副本
dividedBy(Duration divisor) long 返回此持续时间除以指定值的副本
compareTo(Duration otherDuration) int 将此持续时间与指定的Duration进行比较
equals(Object other) boolean 检查此持续时间是否等于指定的Duration
hashCode() int 此持续时间的哈希码
toString() String 使用基于 ISO-8601 秒的表示形式的此持续时间的字符串表示形式,例如PT8H6M12.345S

时区偏移类

ZoneId:是一个抽象类,而他被设计出来是为了表示两个概念:地区和时差。ZoneID有两个子类,分别是ZoneRegion和ZoneOffset

  • ZoneRegion表示城市或地区,并以城市或地区反映“时区”
  • ZoneOffset表示时差,特指与格林尼治时间的偏差

GMT简介:GMT,意为格林尼治时间,它是格林尼治天文台所在的那个时区的时间;还是由格林尼治天文台观测地球自转所确定的时间系统

UTC简介:UTC又称世界统一时间(为了协调世界时间),UTC是以原子时秒长为基础,在时刻上尽量接近于世界时的一种时间计量系统

在JDK8新日期时间系统中,GMT和UTC并无数值上的差别

ZoneId 常用方法
常用方法 返回值类型 说明
systemDefault() static ZoneId 获取系统默认时区
of(String zoneId) static ZoneId 从指定参数获取ZoneId实例
of(String zoneId, Map aliasMap) static ZoneId 从指定参数获取ZoneId实例
ofOffset(String prefix, ZoneOffset offset) static ZoneId 从指定参数获取ZoneId实例
from(TemporalAccessor temporal) static ZoneId 从时间对象中获取ZoneId实例
getId() abstract String 获取唯一的时区 ID
getAvailableZoneIds() static Set 获取一组可用的区域 ID
getDisplayName(TextStyle style, Locale locale) String 获取区域的文本表示,例如“英国时间”或“+02:00”
getRules() abstract ZoneRules 获取此 ID 的时区规则,允许执行计算
normalized() ZoneId 标准化时区ID,在可能的情况下返回一个ZoneOffset
equals(Object obj) boolean 检查此时区 ID 是否等于另一个时区 ID
hashCode() int 此时区 ID 的哈希码
toString() String 使用ID将此区域输出为String

瞬时和时钟类

Instant代表一个具体的时刻(时间戳),可以精确到纳秒

Instant 常用方法
常用方法 返回值类型 说明
now() static Instant 从系统时钟获取当前时刻
now(Clock clock) static Instant 从指定时钟获取当前时刻
ofEpochMilli(long epochMilli) static Instant 从1970-01-01T00:00:00Z的纪元获取使用毫秒的Instant实例
ofEpochSecond(long epochSecond) static Instant 从1970-01-01T00:00:00Z的纪元获取使用秒的Instant实例
ofEpochSecond(long epochSecond,long nanoAdjustment) static Instant 获取使用1970-01-01T00:00:00Z纪元的秒和秒的纳秒分数的Instant实例
with(TemporalAdjuster adjuster) Instant 返回此瞬间的调整副本
with(TemporalField field,long newValue) Instant 返回此瞬间的副本,并将指定字段设置为新值
atOffset(ZoneOffset offset) OffsetDateTime 将此瞬间与偏移量相结合以创建OffsetDateTime
atZone(ZoneId zone) ZonedDateTime 将此瞬间与时区结合以创建ZonedDateTime
from(TemporalAccessor temporal) static Instant 从时间对象中获取Instant
parse(CharSequence text) static Instant 从文本字符串中获取Instant
until(Temporal endExclusive,TemporalUnit unit) long 根据指定的单位计算到另一个瞬间的时间量
truncatedTo(TemporalUnit unit) Instant 返回此Instant截断为指定单位的副本
getNano() int 获取从秒开始沿时间线稍后的纳秒数
getEpochSecond() long 获取 Java 纪元 1970-01-01T00:00:00Z 的秒数
toEpochMilli() long 将此瞬间转换为自 1970-01-01T00:00:00Z 纪元以来的毫秒数
isBefore(Instant otherInstant) boolean 检查此时刻是否在指定时刻之前
isAfter(Instant otherInstant) boolean 检查此时刻是否在指定的时刻之后
plus(long amountToAdd,TemporalUnit unit) Instant 返回添加了指定数量的此瞬间的副本
plus(TemporalAmount amountToAdd) Instant 返回添加了指定数量的此瞬间的副本
plusSeconds(long secondsToAdd) Instant 返回此瞬间的副本,并添加指定的持续时间(以秒为单位)
plusMillis(long millisToAdd) Instant 返回此瞬间的副本,并添加指定的持续时间(以毫秒为单位)
plusNanos(long nanosToAdd) Instant 返回此瞬间的副本,并添加指定的持续时间(以纳秒为单位)
minus(long amountToSubtract,TemporalUnit unit) Instant 返回此瞬间的副本,减去指定的数量
minus(TemporalAmount amountToSubtract) Instant 返回此瞬间的副本,减去指定的数量
minusSeconds(long secondsToSubtract) Instant 返回此瞬间的副本,并减去指定的持续时间(以秒为单位)
minusMillis(long millisToSubtract) Instant 返回此瞬间的副本,减去指定的持续时间(以毫秒为单位)
minusNanos(long nanosToSubtract) Instant 返回此瞬间的副本,并减去指定的纳秒持续时间
compareTo(Instant otherInstant) int 将此瞬间与指定的瞬间进行比较
equals(Object other) boolean 检查此时刻是否等于指定的时刻
hashCode() int 返回此瞬间的哈希码
toString() String 使用 ISO-8601 表示的此瞬间的字符串表示
get(TemporalField field) int 从该时刻获取指定字段的值作为int
getLong(TemporalField field) long 从这一时刻获取指定字段的值作为long
isSupported(TemporalField field) boolean 检查是否支持指定的字段
isSupported(TemporalUnit unit) boolean 检查是否支持指定的单位
query(TemporalQuery query) R 使用指定的查询查询此瞬间
range(TemporalField field) ValueRange 获取指定字段的有效值范围
adjustInto(Temporal temporal) Temporal 调整指定的时间对象以获得这个瞬间

Clock表示当前时刻

Clock 常用方法
常用方法 返回值类型 说明
systemUTC() static Clock 获取使用最佳可用系统时钟返回当前时刻的时钟,使用UTC时区转换为日期和时间
systemDefaultZone() static Clock 获取使用最佳可用系统时钟返回当前时刻的时钟,使用默认时区转换为日期和时间
system(ZoneId zone) static Clock 获取使用最佳可用系统时钟返回当前时刻的时钟
tick(Clock baseClock, Duration tickDuration) static Clock 获取一个时钟,该时钟返回从被截断到指定持续时间最近出现的指定时钟的瞬间
tickMinutes(ZoneId zone) static Clock 获取一个时钟,该时钟使用最佳可用系统时钟在整分钟内返回当前即时滴答声
tickSeconds(ZoneId zone) static Clock 获取一个时钟,该时钟使用最佳可用系统时钟在整秒内返回当前即时滴答声
tickMillis(ZoneId zone) static Clock 获取一个时钟,该时钟使用最佳可用系统时钟在整毫秒内返回当前即时滴答声
fixed(Instant fixedInstant, ZoneId zone) static Clock 获取始终返回同一时刻的时钟
offset(Clock baseClock, Duration offsetDuration) static Clock 获取一个时钟,该时钟从指定的时钟返回瞬间并添加指定的持续时间
getZone() abstract ZoneId 获取用于创建日期和时间的时区
withZone(ZoneId zone) abstract Clock 返回具有不同时区的此时钟的副本
millis() long 获取时钟的当前毫秒时刻
instant() abstract Instant 获取时钟的当前时刻
equals(Object obj) boolean 检查此时钟是否等于另一个时钟
hashCode() int 该时钟的哈希码

时区偏移时间类

  • ZonedDateTime:使用格林威治/UTC的时区偏移量处理具有相应时区的日期和时间
  • OffsetDateTime:使用格林威治/UTC的相应时区偏移量处理日期和时间,但不包含时区ID
  • OffsetTime:使用格林威治/UTC的相应时区偏移量处理时间,但不包含时区ID
ZonedDateTime 常用方法
常用方法 返回值类型 说明
now() static ZonedDateTime 从默认时区的系统时钟获取当前日期时间
now(ZoneId zone) static ZonedDateTime 从指定时区的系统时钟获取当前日期时间
now(Clock clock) 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 从本地日期和时间获取ZonedDateTime
of(LocalDateTime localDateTime, ZoneId zone) static ZonedDateTime 从本地日期时间获取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 获取严格验证本地日期时间、偏移量和区域ID组合的ZonedDateTime
with(TemporalAdjuster adjuster) ZonedDateTime 返回此日期时间的调整副本
with(TemporalField field, long newValue) ZonedDateTime 返回此日期时间的副本,并将指定字段设置为新值
withYear(int year) ZonedDateTime 返回更改年份的ZonedDateTime副本
withMonth(int month) ZonedDateTime 返回更改了月份的ZonedDateTime副本
withDayOfMonth(int dayOfMonth) ZonedDateTime 返回更改了月份日期的ZonedDateTime副本
withDayOfYear(int dayOfYear) ZonedDateTime 返回更改了日期的ZonedDateTime副本
withHour(int hour) ZonedDateTime 返回更改了一天中的小时的ZonedDateTime副本
withMinute(int minute) ZonedDateTime 返回更改了分钟的ZonedDateTime副本
withSecond(int second) ZonedDateTime 返回更改了秒的ZonedDateTime副本
withNano(int nanoOfSecond) ZonedDateTime 返回一个更改了纳秒级的ZonedDateTime副本
withEarlierOffsetAtOverlap() ZonedDateTime 返回此日期时间的副本,将区域偏移更改为本地时间线重叠处的两个有效偏移中较早的一个
withLaterOffsetAtOverlap() ZonedDateTime 返回此日期时间的副本,将区域偏移更改为本地时间线重叠处的两个有效偏移中的较晚
withZoneSameLocal(ZoneId zone) ZonedDateTime 返回具有不同时区的此日期时间的副本,如果可能,保留本地日期时间
withZoneSameInstant(ZoneId zone) ZonedDateTime 返回具有不同时区的此日期时间的副本,保留瞬间
withFixedOffsetZone() ZonedDateTime 返回此日期时间的副本,并将区域 ID 设置为偏移量
toLocalDate() LocalDate 获取此日期时间的LocalDate一部分
toLocalTime() LocalTime 获取此日期时间的LocalTime一部分
toLocalDateTime() LocalDateTime 获取此日期时间的LocalDateTime一部分
toOffsetDateTime() OffsetDateTime 将此日期时间转换为OffsetDateTime
until(Temporal endExclusive, TemporalUnit unit) long 根据指定的单位计算距另一个日期时间的时间量
truncatedTo(TemporalUnit unit) ZonedDateTime 返回截断时间的ZonedDateTime副本
from(TemporalAccessor temporal) static ZonedDateTime 从时间对象中获取ZonedDateTime
parse(CharSequence text) static ZonedDateTime 从文本字符串中获取ZonedDateTime实例
parse(CharSequence text, DateTimeFormatter formatter) static ZonedDateTime 使用特定的格式化程序从文本字符串中获取ZonedDateTime
format(DateTimeFormatter formatter) String 使用指定的格式化程序格式化此日期时间
getYear() int 获取年份字段
getMonth() Month 使用Month枚举获取月份字段
getMonthValue() int 获取从 1 到 12 的月份字段
getDayOfMonth() int 获取日期字段
getDayOfYear() int 获取一年中的天数
getDayOfWeek() DayOfWeek 获取星期几字段
getHour() int 获取小时字段
getMinute() int 获取分钟字段
getSecond() int 获取秒的分钟字段
getNano() int 获取纳秒级字段
getZone() ZoneId 获取时区,例如“欧洲/巴黎”
getOffset() ZoneOffset 获取区域偏移量,例如“+01:00”
plus(long amountToAdd, TemporalUnit unit) ZonedDateTime 返回添加了指定数量的此日期时间的副本
plus(TemporalAmount amountToAdd) ZonedDateTime 返回添加了指定数量的此日期时间的副本
plusYears(long years) ZonedDateTime 返回添加了指定年数的ZonedDateTime副本
plusMonths(long months) ZonedDateTime 返回添加了指定月数的ZonedDateTime副本
plusDays(long days) ZonedDateTime 返回添加了指定天数的ZonedDateTime副本
plusWeeks(long weeks) ZonedDateTime 返回添加了指定周数的ZonedDateTime副本
plusHours(long hours) ZonedDateTime 返回添加了指定小时数的ZonedDateTime副本
plusMinutes(long minutes) ZonedDateTime 返回添加了指定分钟数的ZonedDateTime副本
plusSeconds(long seconds) ZonedDateTime 返回添加了指定秒数的ZonedDateTime副本
plusNanos(long nanos) ZonedDateTime 返回添加了指定纳秒数的ZonedDateTime副本
minus(long amountToSubtract, TemporalUnit unit) ZonedDateTime 返回此日期时间的副本,减去指定的数量
minus(TemporalAmount amountToSubtract) ZonedDateTime 返回此日期时间的副本,减去指定的数量
minusYears(long years) ZonedDateTime 返回减去指定年数后的ZonedDateTime副本
minusMonths(long months) ZonedDateTime 返回减去指定月数后的ZonedDateTime副本
minusDays(long days) ZonedDateTime 返回减去指定天数后的ZonedDateTime副本
minusWeeks(long weeks) ZonedDateTime 返回减去指定周数后的ZonedDateTime副本
minusHours(long hours) ZonedDateTime 返回减去指定小时数后的ZonedDateTime副本
minusMinutes(long minutes) ZonedDateTime 返回减去指定分钟数后的ZonedDateTime副本
minusSeconds(long seconds) ZonedDateTime 返回减去指定秒数后的ZonedDateTime副本
minusNanos(long nanos) ZonedDateTime 返回减去指定纳秒数的ZonedDateTime副本
equals(Object obj) boolean 检查此日期时间是否等于另一个日期时间
hashCode() int 此日期时间的哈希码
toString() String 将此日期时间输出为String
get(TemporalField field) int 从此日期时间中获取指定字段的值作为int
getLong(TemporalField field) long 从此日期时间中获取指定字段的值作为long
isSupported(TemporalField field) boolean 检查是否支持指定的字段
isSupported(TemporalUnit unit) boolean 检查是否支持指定的单位
query(TemporalQuery query) R 使用指定的查询查询此日期时间
range(TemporalField field) ValueRange 获取指定字段的有效值范围

解析与格式化

java.text.Format是一个抽象类,它有三个子类:NumberFormat、DateFormat、MessageFormat,分别用以实现数值/日期/国际化的格式化

NumberFormat 常用方法
常用方法 返回值类型 说明
format(double number) final String
format(double number, StringBuffer toAppendTo, FieldPosition pos) abstract StringBuffer
format(long number) final String
format(long number, StringBuffer toAppendTo, FieldPosition pos) abstract StringBuffer
format(Object number, StringBuffer toAppendTo, FieldPosition pos) StringBuffer
parse(String source) Number
parse(String source, ParsePosition parsePosition) abstract Number
parseObject(String source, ParsePosition pos) final Object
getAvailableLocales() static Locale[]
getCompactNumberInstance() static NumberFormat
getCompactNumberInstance(Locale locale, NumberFormat.Style formatStyle) static NumberFormat
getCurrency() Currency
getCurrencyInstance() static final NumberFormat
getCurrencyInstance(Locale inLocale) static NumberFormat
getInstance() static final NumberFormat
getInstance(Locale inLocale) static NumberFormat
getIntegerInstance() static final NumberFormat
getIntegerInstance(Locale inLocale) static NumberFormat
getMaximumFractionDigits() int
getMaximumIntegerDigits() int
getMinimumFractionDigits() int
getMinimumIntegerDigits() int
getNumberInstance() static final NumberFormat
getNumberInstance(Locale inLocale) static NumberFormat
getPercentInstance() static final NumberFormat
getPercentInstance(Locale inLocale) static NumberFormat
getRoundingMode() RoundingMode
setCurrency(Currency currency) void
setGroupingUsed(boolean newValue) void
setMaximumFractionDigits(int newValue) void
setMaximumIntegerDigits(int newValue) void
setMinimumFractionDigits(int newValue) void
setMinimumIntegerDigits(int newValue) void
setParseIntegerOnly(boolean value) void
setRoundingMode(RoundingMode roundingMode) void
isGroupingUsed() boolean
isParseIntegerOnly() boolean
clone() Object
equals(Object obj) boolean
hashCode() int
DateFormat 静态常量
静态常量 说明
AM_PM_FIELD
calendar
DATE_FIELD
DAY_OF_WEEK_FIELD
DAY_OF_WEEK_IN_MONTH_FIELD
DAY_OF_YEAR_FIELD
DEFAULT
ERA_FIELD
FULL
HOUR_OF_DAY0_FIELD
HOUR_OF_DAY1_FIELD
HOUR0_FIELD
HOUR1_FIELD
LONG
MEDIUM
MILLISECOND_FIELD
MINUTE_FIELD
MONTH_FIELD
numberFormat
SECOND_FIELD
SHORT
TIMEZONE_FIELD
WEEK_OF_MONTH_FIELD
WEEK_OF_YEAR_FIELD
YEAR_FIELD
DateFormat 常用方法
常用方法 返回值类型 说明
format(Object obj, StringBuffer toAppendTo, FieldPosition fieldPosition) final StringBuffer
format(Date date) final String
format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) abstract StringBuffer
parse(String source) Date
parse(String source, ParsePosition pos) abstract Date
parseObject(String source, ParsePosition pos) Object
getAvailableLocales() static Locale[]
getCalendar() Calendar
getDateInstance() static final DateFormat
getDateInstance(int style) static final DateFormat
getDateInstance(int style, Locale aLocale) static final DateFormat
getDateTimeInstance() static final DateFormat
getDateTimeInstance(int dateStyle, int timeStyle) static final DateFormat
getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) static final DateFormat
getInstance() static final DateFormat
getNumberFormat() NumberFormat
getTimeInstance() static final DateFormat
getTimeInstance(int style) static final DateFormat
getTimeInstance(int style, Locale aLocale) static final DateFormat
getTimeZone() TimeZone
setCalendar(Calendar newCalendar) void
setLenient(boolean lenient) void
setNumberFormat(NumberFormat newNumberFormat) void
setTimeZone(TimeZone zone) void
isLenient() boolean
clone() Object
equals(Object obj) boolean
hashCode() int
SimpleDateFormat 构造方法
构造方法 说明
SimpleDateFormat()
SimpleDateFormat(String pattern)
SimpleDateFormat(String pattern,DateFormatSymbols formatSymbols)
SimpleDateFormat(String pattern,Locale locale)
SimpleDateFormat 常用方法
常用方法 返回值类型 说明
format(Date date,StringBuffer toAppendTo,FieldPosition pos) StringBuffer
formatToCharacterIterator(Object obj) AttributedCharacterIterator
parse(String text,ParsePosition pos) Date
get2DigitYearStart() Date
getDateFormatSymbols() DateFormatSymbols
set2DigitYearStart(Date startDate) void
setDateFormatSymbols(DateFormatSymbols newFormatSymbols) void
toLocalizedPattern() String
toPattern() String
applyLocalizedPattern(String pattern) void
applyPattern(String pattern) void
clone() Object
equals(Object obj) boolean
hashCode() int
DateTimeFormatter 静态常量
静态常量 说明
BASIC_ISO_DATE
ISO_DATE
ISO_DATE_TIME
ISO_INSTANT
ISO_LOCAL_DATE
ISO_LOCAL_DATE_TIME
ISO_LOCAL_TIME
ISO_OFFSET_DATE
ISO_OFFSET_DATE_TIME
ISO_OFFSET_TIME
ISO_ORDINAL_DATE
ISO_TIME
ISO_WEEK_DATE
ISO_ZONED_DATE_TIME
RFC_1123_DATE_TIME
DateTimeFormatter 常用方法
常用方法 返回值类型 说明
ofLocalizedDate(FormatStyle dateStyle) static DateTimeFormatter
ofLocalizedDateTime(FormatStyle dateTimeStyle) static DateTimeFormatter
ofLocalizedDateTime(FormatStyle dateStyle, FormatStyle timeStyle) static DateTimeFormatter
ofLocalizedTime(FormatStyle timeStyle) static DateTimeFormatter
ofPattern(String pattern) static DateTimeFormatter
ofPattern(String pattern, Locale locale) static DateTimeFormatter
withChronology(Chronology chrono) DateTimeFormatter
withDecimalStyle(DecimalStyle decimalStyle) DateTimeFormatter
withLocale(Locale locale) DateTimeFormatter
withResolverFields(TemporalField... resolverFields) DateTimeFormatter
withResolverFields(Set resolverFields) DateTimeFormatter
withResolverStyle(ResolverStyle resolverStyle) DateTimeFormatter
withZone(ZoneId zone) DateTimeFormatter
format(TemporalAccessor temporal) String
formatTo(TemporalAccessor temporal, Appendable appendable) void
parse(CharSequence text) TemporalAccessor
parse(CharSequence text, ParsePosition position) TemporalAccessor
parse(CharSequence text, TemporalQuery query) T
parseBest(CharSequence text, TemporalQuery queries) TemporalAccessor
parsedExcessDays() static final TemporalQuery
parsedLeapSecond() static final TemporalQuery
parseUnresolved(CharSequence text, ParsePosition position) TemporalAccessor
getChronology() Chronology
getDecimalStyle() DecimalStyle
getLocale() Locale
getResolverFields() Set
getResolverStyle() ResolverStyle
getZone() ZoneId
toFormat() Format
toFormat(TemporalQuery parseQuery) Format
localizedBy(Locale locale) DateTimeFormatter
toString() String

……待续


参考资料:日期时间API