Java8(四):获取当前月和上个月的第一天、最后一天

爱被打了一巴掌 2023-07-19 10:51 38阅读 0赞

获取上个月第一天和最后一天:

  1. LocalDateTime date = LocalDateTime.now().minusMonths(1);
  2. LocalDateTime firstday = date.with(TemporalAdjusters.firstDayOfMonth());
  3. LocalDateTime lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
  4. System.out.println("firstday:"+firstday.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
  5. System.out.println("lastDay:"+lastDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

获取当前月第一天和最后一天

  1. LocalDateTime date = LocalDateTime.now();
  2. LocalDateTime firstday = date.with(TemporalAdjusters.firstDayOfMonth());
  3. LocalDateTime lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
  4. System.out.println("firstday:"+firstday.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
  5. System.out.println("lastDay:"+lastDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

发表评论

表情:
评论列表 (有 0 条评论,38人围观)

还没有评论,来说两句吧...

相关阅读