JDK8日期新特性获取七天前和上个月最后一天日期


   //日期字符串格式为20210506 
   String coverageCharDate = "20210506";
    //日期格式化变为2021-05-06(如果是20210506格式,格式化格式为yyyyMMdd)
    DateTimeFormatter inFormat = DateTimeFormatter.ofPattern("yyyyMMdd");
    LocalDate date = LocalDate.parse(coverageCharDate, inFormat);
   //七天前
    LocalDate weekDayDate = date.minusDays(7);
   //格式为字符串日期格式yyyyMMdd
    String weekDay = weekDayDate.format(inFormat);
   //上个月最后一天
    LocalDate dateMonth = date.minusMonths(1);
    LocalDate lastMonth = dateMonth.with(TemporalAdjusters.lastDayOfMonth());
    String lastMonthDate = lastMonth.format(inFormat);

  获取当前时间的当月第一天和最后一天

  LocalDateTime date = LocalDateTime.now();
  LocalDateTime firstday = date.with(TemporalAdjusters.firstDayOfMonth());
  LocalDateTime lastDay = date.with(TemporalAdjusters.lastDayOfMonth());


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM