LocalDataTime
1、获取当前时间-构造时间-格式化时间
LocalDataTime data = LocalDataTime.now();//年月日时分秒
LocalData data = LocalData.now();//年月日
LocalTime data = LocalTime.now();//时分秒
System.out.println("构造时间(2023-05-05 01:01:01):" + LocalDateTime.of(2023, 5, 5, 12, 1, 1));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println("格式化后的日期:" + LocalDateTime.now().format(formatter));
2、获取当前时间的时间戳
LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());//毫秒
LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());//秒
3、年-月-日 拼接 时:分:秒
LocalData data = LocalData.now();
LocalTime timeEnd = LocalTime.of(23, 59, 59);
LocalDateTime monday = LocalDateTime.of(data, timeEnd );
4、获取当前日期所在年、月、日、周
System.out.println("当前时间所属于的年份: " + LocalDateTime.now().getYear());
System.out.println("当前时间所属于的月份: " + LocalDateTime.now().getMonth().getValue());
System.out.println("当前时间所属于的天: " + LocalDateTime.now().getDayOfMonth());
System.out.println("当前时间所属于的周: " + WeekFields.of(DayOfWeek.MONDAY,1););
5、获取当前时间所在 周开始-结束时间--星期几
System.out.println("本周周一的日期:" + LocalDate.now().with(weekFields.dayOfWeek(), 1L));
System.out.println("本周周天的日期:" + LocalDate.now().with(weekFields.dayOfWeek(), 7L));
System.out.println("本周周一的日期:" + LocalDateTime.now().with(DayOfWeek.MONDAY).toLocalDate());
System.out.println("本周周天的日期:" + LocalDateTime.now().with(DayOfWeek.SUNDAY).toLocalDate());
System.out.println("今天是星期(英文): " + LocalDateTime.now().getDayOfWeek());
System.out.println("今天是星期(数字): " + LocalDateTime.now().getDayOfWeek().getValue());
6、获取当前时间所在月开始、结束时间
LocalDateTime now = LocalDateTime.now();
LocalDateTime startDateTime = now.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
LocalDateTime endDateTime = now.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
//手动设置时分秒
LocalDateTime startDateTime = req.getBeginTime().with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
LocalDateTime endDateTime = req.getBeginTime().with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
7、判断时间前后关系和是否相等
LocalDateTime date = LocalDateTime.of(2022, 8, 14, 14, 10, 55);
System.out.println("当前时间是否在 date 之前 " + LocalDateTime.now().isBefore(date));
System.out.println("当前时间是否在 date 之后 " + LocalDateTime.now().isAfter(date));
LocalDateTime date1 = LocalDateTime.of(2022, 8, 14, 12, 12, 12);
LocalDateTime date2 = LocalDateTime.of(2022, 8, 14, 16, 12, 12);
System.out.println("date1 == date2 ? " + date1.isEqual(date2));
8、获取两个时间间隔
LocalDate time1 = LocalDate.of(2023,5, 1);
LocalDate time2 = LocalDate.of(2022, 5, 5);
System.out.println("time1 与 time2 相差:" + Period.between(time1, time2).getDays() + " 天");//相差月数:getMonths();相差年份:getYears()
9、时间的修改-增加-减少:年、月、日、周、时、分、秒
System.out.println("当前时间增加一年:" + LocalDateTime.now().plusYears(1));//减少使用minusYears
System.out.println("当前时间增加一个月:" + LocalDateTime.now().plusMonths(1));//减少使用minusMonths (增加的一个月不能比当前月天数多)
System.out.println("当前时间增加一日:" + LocalDateTime.now().plusDays(1));//减少使用minusDays
System.out.println("当前时间增加一周:" + LocalDateTime.now().plusWeeks(1));//减少使用minusYears
System.out.println("当前时间增加一个小时:" + LocalDateTime.now().plusHours(1));//减少使用minusHours
System.out.println("当前时间增加一分钟:" + LocalDateTime.now().plusMinutes(1));//减少使用minusMinutes
System.out.println("当前时间增加一秒钟:" + LocalDateTime.now().plusSeconds(1));//减少使用minusSeconds
withDayOfMonth(i) 设置默认日期
System.out.println("修改年份,修改成2023年:" + LocalDateTime.now().withYear(2023));
System.out.println("修改月份,修改成5月份:" + LocalDateTime.now().withMonth(5));
System.out.println("修改天数,修改成15号:" + LocalDateTime.now().withDayOfMonth(15));
System.out.println("修改小时,修改成15点:" + LocalDateTime.now().withHour(15));
System.out.println("修改分钟,修改成50分钟:" + LocalDateTime.now().withMinute(50));
System.out.println("修改秒,修改成50秒:" + LocalDateTime.now().withSecond(50));