计算某物料在某段时间内的累计添加量-本人自用样例

Map<String, Map<String, Double>> statisticsMap = new HashMap<>();
for (DosingStatistics statistics : dosingStatisticsList) {
    String key = statistics.getPlantName() + "," + statistics.getMedicamentName();

    // 初始化统计数据
    if (!statisticsMap.containsKey(key)) {
        statisticsMap.put(key, new HashMap<>());
    }

    //先获取日期,再分割
    String date = new SimpleDateFormat("yyyy-MM-dd").format(statistics.getDate());
    //日期按-分隔
    String[] split = date.split("-");
    //年
    String y = split[0];
    //月
    String m = split[1];
    //日
    String d = split[2];
    String newDate = null;
    if (Long.valueOf(d) < 27 ){
        //并且月份小月12,若等于12,年份加1
        //若日小于27号,则,保留年月,若大约27号,则年不动,月份加一(保证月份是1-12月)
        newDate = y + "-" + m;
    }else {
        //大于27号,则月份加一
        String newMonth = m+1;
        //若月份小月等于12,则保留原来的和
        if (Long.valueOf(newMonth) <= 12){
            newDate = y + "-" + newMonth;
        }else {
            //若大于12
            String newYear = y + 1;
            newDate = newYear + "-" + 1;
        }
    }

    // 更新统计数据
    Map<String, Double> monthStatistics = statisticsMap.get(key);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
    Date filtrationDate = dateFormat.parse(newDate);
    String boundaryDateString = filtrationDate.toString();
    if (!monthStatistics.containsKey(boundaryDateString)) {
        if (statistics.getCumulativeAddition() != null){
            monthStatistics.put(boundaryDateString, statistics.getCumulativeAddition());
        }else {
            monthStatistics.put(boundaryDateString, Double.valueOf(0));
        }

    } else {
        Double previousAddition = monthStatistics.get(boundaryDateString);
        if (statistics.getCumulativeAddition() != null){
            monthStatistics.put(boundaryDateString, previousAddition + statistics.getCumulativeAddition());
        }else{
            monthStatistics.put(boundaryDateString, previousAddition + Double.valueOf(0));
        }

    }

}
for (Map.Entry<String, Map<String, Double>> entry : statisticsMap.entrySet()) {
    String[] keyParts = entry.getKey().split(",");
    //装置名称
    String plantName = keyParts[0];
    //物料名称
    String medicamentName = keyParts[1];
    Map<String, Double> monthStatistics = entry.getValue();

    for (Map.Entry<String, Double> monthEntry : monthStatistics.entrySet()) {
        //时间
        String dateString = monthEntry.getKey();
        //添加量
        Double cumulativeAddition = monthEntry.getValue();

        try {
            Date firstDayOfMonth = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH).parse(dateString);
            result.add(new DosingStatistics(plantName, medicamentName, firstDayOfMonth, cumulativeAddition));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值