直接上代码:
// 获取当天日期 LocalDate now = LocalDate.now(); // 当天开始时间 LocalDateTime todayStart = now.atStartOfDay(); // 当天结束时间 LocalDateTime todayEnd = LocalDateTime.of(now, LocalTime.MAX); // 周一 LocalDate monday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); // 周日 LocalDate sunday = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); // 本周开始时间 LocalDateTime weekStart = monday.atStartOfDay(); // 本周结束时间 LocalDateTime weekEnd = LocalDateTime.of(sunday, LocalTime.MAX); // 本月1号 LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth()); // 本月最后一天 LocalDate lastDayOfMonth = now.with(TemporalAdjusters.lastDayOfMonth()); // 本月1号的开始时间 LocalDateTime firstDayOfMonthStart = firstDayOfMonth.atStartOfDay(); // 本月最后一天的最后时间 LocalDateTime firstDayOfMonthEnd = LocalDateTime.of(lastDayOfMonth, LocalTime.MAX); // 今年第一天 LocalDate beginTime = LocalDate.now().with(TemporalAdjusters.firstDayOfYear()); // 今年最后一天 LocalDate endTiime = LocalDate.now().with(TemporalAdjusters.lastDayOfYear()); //获取前一天日期 LocalDate yesterday2 = LocalDate.now().minusDays(1); DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println("当天开始时间 = " + todayStart.format(pattern) + " 时间戳 :" + todayStart.toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("当天结束时间 = " + todayEnd.format(pattern) + " 时间戳 :" + todayEnd.toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本周开始时间 = " + weekStart.format(pattern) + " 时间戳 :" + weekStart.toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本周结束时间 = " + weekEnd.format(pattern) + " 时间戳 :" + weekEnd.toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本月开始时间 = " + firstDayOfMonthStart.format(pattern) + " 时间戳 :" + firstDayOfMonthStart.toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本月结束时间 = " + firstDayOfMonthEnd.format(pattern) + " 时间戳 :" + firstDayOfMonthEnd.toInstant(ZoneOffset.of("+8")).toEpochMilli());
2.当前时间减
LocalDate.now().minusYears(x); //当前日期减去指定的年份LocalDate.now().minusMonths(x); //当前日期减去指定的月份LocalDate.now().minusDays(x); //当前日期减去指定的天数 LocalDate.now().minusWeeks(x); //当前日期减去指定的周数
3.时间格式化
注意:
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");//格式化:DateTimeFormatter 转 字符串String str4 = formatter3.format(LocalDateTime.now());System.out.println(str4);//2021-09-22 06:33:08//解析:字符串 转 LocalDateTimeString dateString = "2023-07-13 15:30:00";LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter3);// string 转LocalDateLocalDate localDate = LocalDate.parse(dateString, formatter);
这个 hh:mm:ss 跟HH:mm:ss的区别,如果采用24小时的话,要用HH,如果是:2023-07-27 23:59:59 这种,确用了 hh:mm:ss 这种格式,会报错
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
4.两个LocalDateTime.now()之间相差的天数
LocalDateTime start = LocalDateTime.of(2023, 6, 1, 0, 0, 0);LocalDateTime end = LocalDateTime.of(2023, 6, 28, 0, 0, 0);Duration duration = Duration.between(start, end);long days = duration.toDays();System.out.println(days); // 输出:27
来源地址:https://blog.csdn.net/qq_26112725/article/details/129334298