位于 java.time 包中,用来解决旧版API设计差、非线程安全、时区处理麻烦的问题。对象的修改不会改变原有值而是生成新的副本。
【类】 |
LocalDate:只包含日期,比如:2016-10-20 DateTimeFormatter:时间格式化 Instant:时间戳,相当于java.util的Date ZoneOffset:时区偏移量,比如:+8:00 |
【方法】 |
of:静态工厂方法(用类名去调用)。 parse:静态工厂方法,关注于解析(用类名去调用)。 now: 静态工厂方法,用当前时间创建实例(用类名去调用) get:获取某些东西的值。 is:检查某些东西的是否是true。 with:返回一个部分状态改变了的时间日期对象拷贝(单独一个with方法,参数为TemporalAdjusters类型)。 plus:返回一个时间增加了的、时间日期对象拷贝(如果参数是负数也能够有minus方法的效果)。 minus:返回一个时间减少了的、时间日期对象拷贝。 to:把当前时间日期对象转换成另外一个,可能会损失部分状态。 at:把这个对象与另一个对象组合起来,例如: date.atTime(time)。 format :根据某一个DateTimeFormatter格式化为字符串。 |
LocalDate 日期
包含:年月日
格式:2021-09-27
获取实例 | 方法 |
LocalDate.now () 获取当前日期 LocalDate.of (年,月,日) 获取指日期(数字是一位数可以不加0) LocalDate.parse (“2021-09-27”) 通过字符串获取指定日期(数字是一位数要加0) | getYear () 获取年份 getMonthValue () 获取月份(数字) getMonth () 获取月份(英文,是枚举) |
equals () 比较两个日期是否相同 isBefore () 该日期是否在传入日期之前 iaAfter () 该日期是否在传入日期之后 isLeapYear () 是否是闰年 | |
pluse (数字,单位) 日期加上多少,单位为ChronoUnit的枚举 plus (Period period) 日期加上日期段 plusDays () 日期加上多少天 plusMonths () 日期加上多少月 plusYears () 日期加上多少年 minus(数字,单位)日期减去多少,单位为ChronoUnit的枚举 minus (Period period) 日期减去日期段 minusDays () 日期减去多少天 minusMonths () 日期减去多少月 minusYears () 日期减去多少年 withYear () 修改年 whtiMonth () 修改月 withDayOfMonth () 修改日 withDayOfYear () 修改当年过了多少天(月和日一起) | |
getDayOfYear () 获取当年的第几天 getDayOfMonth () 获取当月的第几天 getDayOfWeek () 获取星期几(英文,是枚举) getDayOfWeek ().getValue () 获取星期几(数字) |
//【LocalDate】日期
LocalDate date1 = LocalDate.now(); //2021-10-25
LocalDate date2 = LocalDate.of(2021, 9, 27); //2021-09-27
LocalDate date3 = LocalDate.parse("2021-10-25"); //2021-10-25
//年
int year = date1.getYear(); //2021
//月
int month1 = date1.getMonthValue(); //10
Month month2 = date1.getMonth(); //OCTOBER
int month3 = date1.getMonth().getValue(); //10
//日
int day1 = date1.getDayOfYear(); //298
int day2 = date1.getDayOfMonth(); //25
DayOfWeek day3 = date1.getDayOfWeek(); //MONDAY
int day4 = date1.getDayOfWeek().getValue(); //1
//比较
boolean equalsDate = date1.equals(date2); //false
boolean beforeDate = date1.isBefore(date2); //false
boolean afterDate = date1.isAfter(date2); //true
boolean leapYear = date1.isLeapYear(); //false
//加减同理
LocalDate plusDays = date1.plusDays(1); //2021-10-26
LocalDate plusWeeks = date1.plusWeeks(1); //2021-11-01
LocalDate plusMonths = date1.plusMonths(1); //2021-11-25
LocalDate plusYears = date1.plusYears(1); //2022-10-25
LocalDate plus1 = date1.plus(1, ChronoUnit.DAYS); //2021-10-26
LocalTime 时间
包含:时分秒
格式:11:07:13.123 (小数点后面是纳秒)
获取实例 | 方法 |
LocalTime.now () 获取当前时间 LocalTime.of (时,分,秒,纳秒) 获取指定时间(数字是一位数可以不加0) LocalTime.parse ("06:04:06.4") 通过字符串获取指定时间(数字是一位数要加0) | getHour () 获取小时字段 getMinute () 获取分钟字段 getSecond () 获取秒钟字段 getNano () 获取纳秒字段 |
equals () 比较两个时间是否相同 isBefore () 该时间是否在传入时间之前 iaAfter () 该时间是否在传入时间之后 | |
pluse (数字,单位) 时间加上多少,单位为ChronoUnit的枚举 plus ((Period period)) 时间加上时间段 plusHours () 时间加上多少小时 plusMinutes () 时间加上多少分钟 plusSeconds () 时间加上多少秒 plusNanos () 时间加上多少纳秒 minus(数字,单位)时间减去多少,单位为ChronoUnit的枚举 minus ((Period period)) 时间减去时间段 minusHours () 时间减去多少小时 minusMinutes () 时间减去多少分钟 minusSeconds () 时间减去多少秒 minusNanos () 时间减去多少纳秒 withHour () 修改时 withMinute () 修改分 withSecond () 修改秒 withNano () 修改纳秒 | |
toSecondOfDay () 该时间有多少秒 toNanoOfDay () 该时间有多少纳秒 LocalTime.ofSecondOfDay (1234) 多少秒是什么时间(取值范围 0-86399,+1为24小时整) LocalTime.ofNanoOdDay (12345678L) 多少纳秒是什么时间(取值范围0 - 86399999999999,+1为24小时整,数字写大点就要加L了) |
//【LocalTime】时间
LocalTime time1 = LocalTime.now(); //01:24:44.484
LocalTime time2 = LocalTime.of(6, 4, 6, 4); //06:04:06.000000004
LocalTime time3 = LocalTime.parse("06:04:06.4"); //06:04:06.400
//时
int hour = time2.getHour(); //6
//分
int minute = time2.getMinute(); //4
//秒
int second = time2.getSecond(); //6
//纳秒
int nano = time2.getNano(); //4
//比较
boolean equalsTime = time1.equals(time2); //false
boolean beforeTime = time1.isBefore(time2); //ftrue
boolean afterTime = time1.isAfter(time2); //false
//加减同理
LocalTime plusHours = time2.plusHours(1); //07:04:06.000000004
LocalTime plusMinutes = time2.plusMinutes(1); //06:05:06.000000004
LocalTime plusSeconds = time2.plusSeconds(1); //06:04:07.000000004
LocalTime plusNanos = time2.plusNanos(1); //06:04:06.000000005
//换算
LocalTime time4 = LocalTime.ofSecondOfDay(1234L); //00:20:34
LocalTime time5 = LocalTime.ofNanoOfDay(12345678910L); //00:00:12.345678910
int toSecondOfDay = time4.toSecondOfDay(); //1234
long toNanoOfDay = time5.toNanoOfDay(); //12345678910
LocalDateTime 日期时间
包含:年月日时分秒
格式:2021-09-27T11:07:13.123
获取实例 | 方法 |
LocalDateTime.now () 获取当前日期时间 LocalDateTime.of(2021,9,27,11,7,13) 活动起止日期时间 LocalDateTime.parse("2021-09-27T11:07:13") 通过字符串获取指定日期时间 LocalDateTime.atTime(LocalTime对象) LocalDateTime.atDate(LocalDate对象) |
DateTimeFormatter 格式化&解析
时间 | 字母 | 说明 |
年 | yy 表示2位数年份 | 21 |
yyyy 表示4位数年份 | 2021 | |
月 | MM 数字月份 | 05 |
MMM 根据语言环境显示不同语言的月份 | 中文:十月 英文:Oct | |
日 | dd 月份中的天数,表示当月的第几天 | 10 |
D 年份中的天数,表示当年的第几天 | 156 | |
周 | E 会根据语言环境显示不同语言的星期几 | 中文:星期四 英文:Thu |
w 年份中的周数,表示当年的第几周 | 44 | |
时 | hh 格式12小时 | 上午下午都是10 |
HH 格式24小时 | 上午10,下午22 | |
a 根据语言环境显示不同语言的上下午 | 中文:上午 英文:am | |
分 | mm | 29 |
秒 | ss 秒数 | 38 |
SSS 毫秒数 | 156 | |
公元 | G | 显示:公元 |
LocalDateTime now1 = LocalDateTime.now(); //2021-10-27T14:50:23.543
DateTimeFormatter formatter1 = DateTimeFormatter.ISO_DATE_TIME; //调用自带时间格式
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("现在是:yyyy年MM月dd日 HH时mm分ss秒"); //自定义时间格式
String now2 = now1.format(formatter2); //现在是:2021年10月28日 14时56分32秒
LocalDateTime parse = LocalDateTime.parse("现在是:2021年09月27日 07时04分16秒", formatter2); //2021-09-27T07:04:16
Instant 时间戳
时间线上的某个时间点,保存了自1970-01-01 00:00:00以来的秒和纳秒,用来计算两个点的时间差。
获取实例 | 方法 |
Instant.now () 获取系统当前时间点 Instant.offEpochSecond (秒,纳秒) 通过秒和纳秒创建时间点 | getEochSecond () 获取秒 getNano () 获取纳秒 plus*** () 增加时间 minus*** () 减少时间 |
Instant now = Instant.now(); //2021-10-29T06:53:33.861Z
long instantSecond = now.getEpochSecond(); //1635490413
int instantNano = now.getNano(); //861000000
Period 日期段
两个LocalDate日期之间的间隔
获取实例 | 方法 |
Period.between(LocalDate开始,LocalDate结束) 获取两个日期之间的日期段 Period.of (年,月,日) 获取指定长度的日期段 | isNegative () 年月日是否为负值 isZero () 年月日是否为0 getYears () 获取年数 getMonths () 获取月数 getDays () 获取天数 plus*** () 增加日期 minus*** () 减少日期 with*** () 修改日期 |
LocalDate nowDate = LocalDate.now(); //2021-10-29
LocalDate thenDate = LocalDate.of(1992, 9, 27); //1992-09-27
Period period = Period.between(nowDate, thenDate); //P-29Y-1M-2D 后面时间减去前面时间注意正负
int years = period.getYears(); //-29
int days = period.getDays(); //-2
Duration 时间段
两个LocalTime时间之间的间隔
获取实例 | 方法 |
Duration.between(Temporal开始,Temporal结束) 获取两个节点之间的时间段 Duration.of(7,ChronoUnit.DAYS) 通过天数获取时间段 Duration.ofDays () 通过天数获取时间段 | isNegative () 时分秒是否为负值 isZero () 时分秒是否为0 toDays () 总天数 toHours () 总小时 toMinutes () 总分钟 toSeconds () 总秒数 toMillis () 总毫秒 toNanos () 总纳秒 plus*** () 增加时间 minus*** () 减少时间 with*** () 修改时间 |
LocalTime nowTime = LocalTime.now(); //15:01:50.642
LocalTime thenTime = LocalTime.of(8, 35, 15); //08:35:15
Duration duration = Duration.between(nowTime, thenTime); //PT-6H-26M-35.642S 后面时间减去前面时间注意正负
long toDays = duration.toDays(); //0
long toHours = duration.toHours(); //-6
TemporalAdjuster 时间校正器
对时间日期进行调整。工具类TemporalAdjusters集成了很多校正方法。
方法 | 说明 |
dayOfWeekInMonth (第几周,星期几) | 当月第几周的星期几 (传入DayOfWeek的枚举) |
previous (周几) next (周几) | 当月上一个星期几是几号 当月下一个星期几是几号 |
firstDayOfMonth () firstDayOfYear ()firstDayOfNextMonth () firstDayOfNextYear () | 当月第一天 当年第一天下月第一天 下年第一天 |
lastDayofMonth () lastDayOfYear () | 当月最后一天 当年最后一天 |
//【TemporalAdjuster】时间校正器
LocalDateTime now = LocalDateTime.now(); //2021-11-02T04:31:33.196
//自定义校正器,调整到下月第一天
TemporalAdjuster adjuster1 = temporal -> {
LocalDateTime cpoy = (LocalDateTime) temporal;
LocalDateTime result = cpoy.plusMonths(1).withDayOfMonth(1);
return result;
};
//系统自带校正工具类
TemporalAdjuster adjuster2 = TemporalAdjusters.firstDayOfNextMonth(); //该日期的下月第一天
TemporalAdjuster adjuster3 = TemporalAdjusters.next(DayOfWeek.SATURDAY); //该日期的下周星期二
//修改时间
LocalDateTime now1 = now.with(adjuster1); //2021-12-01T04:31:33.196
LocalDateTime now2 = now.with(adjuster2); //2021-12-01T04:31:33.196
ZoneId 时区
获取实例 | 方法 |
ZoneId.of ("UTC+8") 使用格林威治时间 ZoneId.of ("Asia/Shanghai") 使用地区名字 ZoneId.from (ZoneDateTime.now()) 从日期时间中获取时区 | getAvailableZoneIds () 获取所有时区ID systemDefault () 获取系统默认时区 |
ZoneDate
ZoneTime
ZoneDateTime 带时区的日期时间
获取实例 | 方法 |
ZonedDateTime.now () 获取当前时区时间 ZonedDateTime.now (Clock.systemUTC()) 获取世界标准时间 ZonedDateTime.now (ZoneId.of("Asia/Shanghai")) 获取指定时区时间 ZonedDateTime.parse ("2007-12-03T10:15:30+01:00[Europe/Paris]") 通过字符串获取 | withZoneSameInstant () 改变时区,改变时间 withZoneSameLocal () 改变时区 get*** () 获取时间日期 plus*** () 增加时间日期 minus*** () 减少时间日期 |
//本地时间
LocalDateTime local = LocalDateTime.now(); //2021-11-01T22:17:43.099
//时区时间
ZonedDateTime zone = ZonedDateTime.now(); //2021-11-01T22:17:43.100+08:00[Asia/Shanghai]
//世界标准时间
ZonedDateTime time = ZonedDateTime.now(Clock.systemUTC()); //2021-11-01T14:17:43.100Z
// ZoneId.getAvailableZoneIds().forEach(System.out::println); //获取所有时区ID
//根据时区,获取白宫时间
ZonedDateTime america = ZonedDateTime.now(ZoneId.of("America/Whitehorse")); //2021-11-01T07:17:43.101-07:00[America/Whitehorse]
//根据时区获取,获取上海时间
ZonedDateTime china = ZonedDateTime.now(ZoneId.of("Asia/Shanghai")); //2021-11-01T22:17:43.109+08:00[Asia/Shanghai]
//美国修改为中国
ZonedDateTime a2c1 = america.withZoneSameInstant(ZoneId.of("Asia/Shanghai")); //2021-11-01T22:17:43.101+08:00[Asia/Shanghai]
ZonedDateTime a2c2 = america.withZoneSameLocal(ZoneId.of("Asia/Shanghai")); //2021-11-01T07:17:43.101+08:00[Asia/Shanghai]