亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果知道時間,如何計算每個月范圍內的數據并添加到一個列表中

如果知道時間,如何計算每個月范圍內的數據并添加到一個列表中

斯蒂芬大帝 2021-08-25 17:15:39
我有一個開始時間:startTime:09-2017,結束時間是 endTime:05-2018。我想計算每個月的數據量然后加起來計算從開始到結束的時間段內的數據。例如,開始時間為 09-2017,結束時間為 5-2018。在8個月期間,我想計算每個月的數據,然后相加計算出所有時間段。我使用 for 循環作為結束時間減去一個月。如果 endTime 的月份等于 monthOfStartTime,循環將停止。我將數據保存到一個 arrayList 中,一個月后我會將它添加回來。下面是我的代碼:開始時間:09-2017;結束時間:05-2018;@Autowiredprivate StudentDao studentDao;//total students in 8 monthList<Student> students = new ArrayList<>();//caculator data in everyMonth for (int i = dateTimeNow.getMonthOfYear(); i >= 0; i--) {    //break if equal startTime.    LocalDateTime startTimeCaculator = endTime.getMonthOfYear().minusMonths(i-1);    List<Student> studentOnMonth =     studentDao.getDataEveryMonth(startTimeCaculator,endTime);    students.addAll(studentOnMonth);   }我有兩個問題。當我計算開始日期時,循環停止的條件是什么?其次,如果我使用 i = endTime.getMonthOfYear 變量,循環將從月末開始計數到零,并且不會按年計數。時間到 5-2018 年,循環將運行 5 次,不計算 2017 年的月份。請幫忙。
查看完整描述

1 回答

?
四季花海

TA貢獻1811條經驗 獲得超5個贊

我會像這樣控制循環:


    YearMonth endMonth = YearMonth.of(2018, Month.MAY);

    YearMonth startMonth = YearMonth.of(2017, Month.SEPTEMBER);

    for (YearMonth m = endMonth; m.isAfter(startMonth); m = m.minusMonths(1)) {

        LocalDateTime monthStart = m.atDay(1).atStartOfDay();

        LocalDateTime monthEnd = m.plusMonths(1).atDay(1).atStartOfDay();


        System.out.println("Month from " + monthStart + " inclusive to " + monthEnd + " exclusive");

    }

由于代碼段在這里,它輸出:


Month from 2018-05-01T00:00 inclusive to 2018-06-01T00:00 exclusive

Month from 2018-04-01T00:00 inclusive to 2018-05-01T00:00 exclusive

Month from 2018-03-01T00:00 inclusive to 2018-04-01T00:00 exclusive

Month from 2018-02-01T00:00 inclusive to 2018-03-01T00:00 exclusive

Month from 2018-01-01T00:00 inclusive to 2018-02-01T00:00 exclusive

Month from 2017-12-01T00:00 inclusive to 2018-01-01T00:00 exclusive

Month from 2017-11-01T00:00 inclusive to 2017-12-01T00:00 exclusive

Month from 2017-10-01T00:00 inclusive to 2017-11-01T00:00 exclusive

如果這不是你想要的,請調整。


您可能還想修改您的學生 DAO 以接受YearMonth參數。這取決于您想要的靈活性:傳遞兩個日期時間實例允許比一個月更短或更長的時間段,因此提供更大的靈活性。


編輯:如果您想startMonth被包括在內,請使用“不在之前”來表示在或之后,例如:


    YearMonth endMonth = YearMonth.of(2017, Month.OCTOBER);

    YearMonth startMonth = YearMonth.of(2017, Month.SEPTEMBER);

    for (YearMonth m = endMonth; ! m.isBefore(startMonth); m = m.minusMonths(1)) {

        LocalDateTime monthStart = m.atDay(1).atStartOfDay();

        LocalDateTime monthEnd = m.plusMonths(1).atDay(1).atStartOfDay();


        System.out.println("Month from " + monthStart + " inclusive to " + monthEnd + " exclusive");

    }

輸出:


Month from 2017-10-01T00:00 inclusive to 2017-11-01T00:00 exclusive

Month from 2017-09-01T00:00 inclusive to 2017-10-01T00:00 exclusive


查看完整回答
反對 回復 2021-08-25
  • 1 回答
  • 0 關注
  • 261 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號