LocalDate、LocalTime、LocalDateTime
LocalDate、LocalTime、LocalDateTime 类的实例是不可变的对象,分别表示使用 ISO-8601 日历系统的日期、时间、日期和时间。它们提供了简单的日期和或时间,并不包含当前的时间信息。也不包含与时区相关的信息。
ISO-8601 日历系统是国际标准化组织制定的现代公民的日期和时间的表示法。
@Testpublic void test1() {// 获取当前的时间// 当前时间: 2022-02-11T17:42:07.282LocalDateTime localDateTime = LocalDateTime.now();System.out.println("当前时间: " + localDateTime);// 自己指定一个时间 参数依次是 ==> 年月日时分秒// 自定义的时间: 2025-02-10T10:42:20LocalDateTime dateTime = LocalDateTime.of(2025, 2, 10, 10, 42, 20);System.out.println("自定义的时间: " + dateTime);// 在 localDateTime1 实例之上加上时间LocalDateTime localDateTime1 = localDateTime.plusYears(10).plusMonths(10).plusDays(2).plusWeeks(2).plusHours(50).plusMinutes(200).plusSeconds(30).plusNanos(25); // 纳秒// 当前时间加上一段时间: 2032-12-29T23:02:37.282000025System.out.println("当前时间加上一段时间: " + localDateTime1);// 在 localDateTime2 实例之上减去时间LocalDateTime localDateTime2 = localDateTime1.minusYears(10).minusMonths(10).minusDays(2).minusWeeks(2).minusHours(50).minusMinutes(200).minusSeconds(30).minusNanos(25);// 回到当前时间: 2022-02-10T17:42:07.282System.out.println("回到当前时间: " + localDateTime2);// 获取当前的日期是本年的第多少天// 42int dayOfYear = localDateTime.getDayOfYear();System.out.println(dayOfYear);// 获取当前的日期是本月的第多少天// 11int dayOfMonth = localDateTime.getDayOfMonth();System.out.println(dayOfMonth);// 获取当天是星期几?// FRIDAYDayOfWeek dayOfWeek = localDateTime.getDayOfWeek();System.out.println(dayOfWeek);// 获取当前的年份int year = localDateTime.getYear();// 获取当前的月份, Month 类型的数据// 月份的单词: FEBRUARY// 月份的数字: 2Month month = localDateTime.getMonth();System.out.println("月份的单词: " + month);System.out.println("月份的数字: " + month.getValue());// 获取当前的小时int hour = localDateTime.getHour();// 获取当前的分钟int minute = localDateTime.getMinute();// 获取当前的秒int second = localDateTime.getSecond();// 获取当前的纳秒int nano = localDateTime.getNano();// 获取当前的月份,数字int monthValue = localDateTime.getMonthValue();// 2System.out.println(monthValue);}
Instant: 时间戳(以 Unix 元年: 1970年1月1日00:00:00 到某个时间为止的毫秒数)
@Testpublic void test2() {// 获取当前的世界协调时间 UTCInstant inst = Instant.now();// 2022-02-11T09:45:06.880ZSystem.out.println(inst);// inst 加上一段时间 ==> 东八区时间OffsetDateTime offsetDateTime = inst.atOffset(ZoneOffset.ofHours(8));// 2022-02-11T17:45:06.880+08:00System.out.println(offsetDateTime);// 时间戳, 获取 inst 的毫秒值// 1644572706880System.out.println(inst.toEpochMilli());// Unix 元年的时间加上 1000 毫秒Instant instant = Instant.ofEpochMilli(1000);// 加上 1000 毫秒的值: 1970-01-01T00:00:01ZSystem.out.println("加上 1000 毫秒的值: " + instant);// Unix 元年的时间加上 10 秒Instant instant1 = Instant.ofEpochSecond(10);// 加上 10 秒的值: 1970-01-01T00:00:10ZSystem.out.println("加上 10 秒的值: " + instant1);}
时间间隔
Duration: 计算两个时间之间的间隔
Period: 计算两个日期之间的间隔
@Testpublic void test3() {Instant inst1 = Instant.now();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}Instant inst2 = Instant.now();Duration duration = Duration.between(inst1, inst2);// 时间间隔以毫秒的形式输出System.out.println(duration.toMillis());// 时间间隔以分钟的形式输出System.out.println(duration.toMinutes());// 时间间隔以纳秒的形式输出System.out.println(duration.toNanos());// 时间间隔以天的形式输出System.out.println(duration.toDays());// 时间间隔以小时的形式输出System.out.println(duration.toHours());System.out.println("-----------两个时间的间隔------------------");LocalTime lt1 = LocalTime.now();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}LocalTime lt2 = LocalTime.now();Duration between = Duration.between(lt1, lt2);System.out.println(between.toMillis());System.out.println("-----------两个日期的间隔------------------");LocalDate ld1 = LocalDate.of(2020, 2, 20);LocalDate ld2 = LocalDate.now();Period between1 = Period.between(ld1, ld2);// 输出两个时间的间隔 P1Y11M22D , 1 年 11 月 22 日System.out.println(between1);System.out.println(between1.getYears());System.out.println(between1.getMonths());System.out.println(between1.getDays());}
1000
0
1000000000
0
0
-----------两个时间的间隔------------------
1001
-----------两个日期的间隔------------------
P1Y11M22D
1
11
22
日期的操纵
TemporalAdjuster: 时间校正器。有时候可能需要获取例如: 将日期调整到”下个周日“ 等操作。
TemporalAdjusters: 该类通过静态方法提供了大量的常用 TemporalAdjuster 的实现
@Testpublic void test4() {// 获取下一个周五的日期LocalDate with = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.FRIDAY));System.out.println("下一个周五: " + with);// 获取当前日期LocalDate now = LocalDate.now();// 获取下一个工作日的日期LocalDate workDay = now.with((ld) -> {LocalDate localDate1 = (LocalDate) ld;DayOfWeek dayOfWeek = localDate1.getDayOfWeek();switch (dayOfWeek) {case FRIDAY:localDate1 = localDate1.plusDays(3);break;case SATURDAY:localDate1 = localDate1.plusDays(2);break;case SUNDAY:localDate1 = localDate1.plusDays(1);break;}return localDate1;});System.out.println("下一个工作日: " + workDay);}
下一个周五: 2022-02-18
下一个工作日: 2022-02-14
DateTimeFormatter: 格式化时间和日期
@Testpublic void test5() {// 日期和时间// DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE_TIME;// 只要日期DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE;LocalDateTime now = LocalDateTime.now();// 将当前的时间应用到指定的格式上去String format = now.format(isoDateTime);System.out.println(format);// 自定义日期和时间DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy 年 MM 月 dd 日 HH:mm:ss");// 将自定义的时间和日期应用到当前的时间上去String format1 = now.format(dateTimeFormatter);System.out.println(format1);// 将转换后的格式还原成 LocalDateTime 的格式: format1 是转换后的字符串, dateTimeFormatter 是转换规则LocalDateTime parse = LocalDateTime.parse(format1, dateTimeFormatter);System.out.println(parse);}
时区
Java8 中加入对时区的支持,带时区的时间分别为:
ZoneDate、ZonedTime、ZonedDateTime
其中每个时区都对应着地区 ID 都为 “{区域}/{城市}” 的格式 例如: Asia / Shanghai 等
ZoneId: 该类中包含了所有的时区信息
getAvailableZoneIds(): 可以获取所有时区的时区信息
of(id): 用指定的时区信息获取 ZoneId 对象
@Testpublic void test6() {// 获取全部的时区Set<String> set = ZoneId.getAvailableZoneIds();set.forEach(System.out::println);// 获取当前牙买加的时间ZoneId zoneId = ZoneId.of("Jamaica");LocalDateTime ldt = LocalDateTime.now(zoneId);System.out.println(ldt);// 获取当前上海的时间ZoneId zoneId2 = ZoneId.of("Asia/Shanghai");LocalDateTime ldt2 = LocalDateTime.now(zoneId2);System.out.println(ldt2);// 包含 上海与 UTC 时间的时差 的上海时间// 2022-02-11T17:12:10.636+08:00[Asia/Shanghai]ZonedDateTime zdt = ldt2.atZone(zoneId.of("Asia/Shanghai"));System.out.println(zdt);}