Java8(四):獲取當前月和上個月的第一天、最後一天

獲取上個月第一天和最後一天:

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

獲取當前月第一天和最後一天

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章