You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中Java按日期汇总Payment金额生成图表数据求助

实现方案

核心是通过Map按日期分组累加金额,再结合时间区间过滤,最后转成AnyChart需要的DataEntry格式,具体步骤如下:

1. 按日期汇总金额

用LocalDate作为Map的key(彻底忽略时间部分),遍历Payment列表时把LocalDateTime截断到日期维度,然后累加对应日期的金额:

// 从Retrofit获取的原始支付列表
List<Payment> paymentsList = ...;

// 存储日期-总金额的映射,用LocalDate做key避免时间干扰
Map<LocalDate, Double> dailyTotalMap = new HashMap<>();

for (Payment payment : paymentsList) {
    // 截断LocalDateTime到日期部分,转成LocalDate
    LocalDate paymentDate = payment.getDateTime().truncatedTo(ChronoUnit.DAYS).toLocalDate();
    double currentAmount = payment.getAmount();
    
    // 累加:存在则加,不存在则初始化为当前金额
    dailyTotalMap.put(paymentDate, dailyTotalMap.getOrDefault(paymentDate, 0.0) + currentAmount);
}

如果金额用BigDecimal处理精度,把上面的Double换成BigDecimal,累加用add方法即可:

Map<LocalDate, BigDecimal> dailyTotalMap = new HashMap<>();
dailyTotalMap.put(paymentDate, dailyTotalMap.getOrDefault(paymentDate, BigDecimal.ZERO).add(payment.getAmount()));

2. 时间区间筛选

针对上周、上月、半年等需求,写一个区间判断方法,过滤汇总后的Map:

// 判断日期是否在指定区间内
private boolean isDateInRange(LocalDate date, String timeRange) {
    LocalDate now = LocalDate.now();
    LocalDate startDate;

    switch (timeRange) {
        case "上周":
            // 按周一作为周起始,可根据需求改为周日
            startDate = now.minusWeeks(1).with(DayOfWeek.MONDAY);
            LocalDate endOfLastWeek = startDate.plusWeeks(1).minusDays(1);
            return !date.isBefore(startDate) && !date.isAfter(endOfLastWeek);
        case "上月":
            YearMonth lastMonth = YearMonth.now().minusMonths(1);
            startDate = lastMonth.atDay(1);
            LocalDate endOfLastMonth = lastMonth.atEndOfMonth();
            return !date.isBefore(startDate) && !date.isAfter(endOfLastMonth);
        case "半年":
            startDate = now.minusMonths(6);
            return !date.isBefore(startDate);
        default:
            return true; // 默认显示所有数据
    }
}

// 执行筛选,比如选中"上月"
String selectedRange = "上月";
Map<LocalDate, Double> filteredMap = new HashMap<>();
for (Map.Entry<LocalDate, Double> entry : dailyTotalMap.entrySet()) {
    if (isDateInRange(entry.getKey(), selectedRange)) {
        filteredMap.put(entry.getKey(), entry.getValue());
    }
}

3. 转换为AnyChart的DataEntry

把过滤后的日期和金额转成AnyChart需要的ValueDataEntry,注意日期转成字符串格式:

import com.anychart.chart.common.dataentry.ValueDataEntry;

List<ValueDataEntry> chartData = new ArrayList<>();
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 自定义日期格式

// 可选:按日期排序,保证图表x轴顺序正确
List<Map.Entry<LocalDate, Double>> sortedEntries = new ArrayList<>(filteredMap.entrySet());
sortedEntries.sort(Map.Entry.comparingByKey());

for (Map.Entry<LocalDate, Double> entry : sortedEntries) {
    String dateStr = entry.getKey().format(dateFormatter);
    chartData.add(new ValueDataEntry(dateStr, entry.getValue()));
}

// 之后将chartData传入AnyChart组件,比如柱状图:
// Cartesian cartesian = AnyChart.column();
// cartesian.data(chartData);

注意事项

  • 时区处理:如果接口返回的LocalDateTime是UTC时间,需要先转换为本地时区再截断日期,避免日期计算偏差。
  • 空数据处理:如果筛选后没有数据,要给图表设置空状态提示,避免异常。

内容的提问来源于stack exchange,提问作者Delamir1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 08:35:29