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