//1. LocalDate、LocalTime、LocalDateTime
LocalDate localDate = LocalDate.now();
System.out.println("localDate = " + localDate);// localDate = 2021-06-02
LocalTime localTime = LocalTime.now();// localTime = 21:44:54.806
System.out.println("localTime = " + localTime);
LocalDateTime localDateTime = LocalDateTime.now();// localDateTime = 2021-06-02T21:44:54.806
System.out.println("localDateTime = " + localDateTime);
// 自定义时间
LocalDate diyDate = LocalDate.of(2021, 12, 12);// diyDate = 2021-12-12
System.out.println("diyDate = " + diyDate);
//LocalDateTime diyLocalDateTime = LocalDateTime.of(LocalDate.of(2021, 6, 18), LocalTime.of(23, 23, 23));
LocalDateTime diyLocalDateTime = LocalDateTime.of(localDate, localTime);// diyLocalDateTime = 2021-06-18T23:23:23
System.out.println("diyLocalDateTime = " + diyLocalDateTime);
// 时间的 ±
LocalDate plus3Years = diyDate.plusYears(3);// plus3Years = 2024-12-12
System.out.println("plus3Years = " + plus3Years);
// 获取年月日,其中特别需要注意获取"月份"的差异
int year = localDate.getYear();// year = 2021
System.out.println("year = " + year);
Month month = localDate.getMonth();
System.out.println("localDate.getMonth() = " + month);// localDate.getMonth() = JUNE
int monthValue = localDate.getMonthValue();// localDate.getMonthValue() = 6
System.out.println("localDate.getMonthValue() = " + monthValue);
//////////////////////////////////////////////
//2. Instant : 时间戳。 (使用 Unix 元年 1970年1月1日 00:00:00 所经历的毫秒值)
Instant now = Instant.now();
System.out.println("now = " + now);
//////////////////////////////////////////////
// 3. 计算时间间隔
// Duration : 用于计算两个“时间”间隔
// Period : 用于计算两个“日期”间隔
Period between = Period.between(diyDate, plus3Years);
int years = between.getYears();
System.out.println(diyDate + " 和 " + plus3Years + "之间的时间间隔(年):" + years);// 3
int months = between.getMonths();
System.out.println(diyDate + " 和 " + plus3Years + "之间的时间间隔(年):" + months);// 0,月份相同,所以为0。
Instant now2 = Instant.now();
System.out.println("now2 = " + now2);
Duration duration = Duration.between(now, now2);
long millis = duration.toMillis();
System.out.println(now + " 和 " + now2 + " 的时间间隔(毫秒):" + millis);
//////////////////////////////////////////////
//4. TemporalAdjuster、TemporalAdjusters:时间校正器
LocalDate tempDate = localDate.of(2021, 6, 12);// tempDate = 2021-06-12
System.out.println("tempDate = " + tempDate);
// 经典问题1:获取当前日期所在的周一和周日。
// 有可能今天就是周一
if (tempDate.getDayOfWeek().equals(DayOfWeek.MONDAY)) {
System.out.println(tempDate + " 是周一");
} else {
System.out.println(tempDate + " 不是周一");
LocalDate previousMonday = tempDate.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
System.out.println("本周周一是 " + previousMonday);// 2021-6-7
}
// 获取当前日期所在的周日
if (tempDate.getDayOfWeek().equals(DayOfWeek.SUNDAY)) {
System.out.println(tempDate + " 是周日");
} else {
System.out.println(tempDate + " 不是周日");
LocalDate nextSunday = tempDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));// nextSunday = 2021-06-13
System.out.println("本周周日是 " + nextSunday);
}
// 经典问题2:获取本月第一天。
LocalDate firstDayOfMonth = tempDate.with(TemporalAdjusters.firstDayOfMonth());// firstDayOfMonth = 2021-06-01
System.out.println("firstDayOfMonth = " + firstDayOfMonth);
//////////////////////////////////////////////
// 5. 日期格式化 DateTimeFormatter
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatDate = localDate.format(dateFormatter);// formatDate = 2021-06-02
System.out.println("formatDate = " + formatDate);
String formatDateTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));// formatDateTime = 2021-06-02 23:41:39
System.out.println("formatDateTime = " + formatDateTime);
LocalDate parseDate = LocalDate.parse("2021-02-11", dateFormatter);// parseDate = 2021-02-11
System.out.println("parseDate = " + parseDate);
LocalDateTime parseDateTime = LocalDateTime.parse("2021-06-02 23:48:06", dateTimeFormatter);// parseDateTime = 2021-06-02T23:48:06
System.out.println("parseDateTime = " + parseDateTime);