查出来的数据,java里遍历处理一遍数据 获取上月数量减去本月数量
时间: 2024-05-06 12:15:28 浏览: 91
假设你有一个包含每天销售数量的列表,你可以使用以下Java代码来计算上个月的总销售量:
```java
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
public class Sales {
public static void main(String[] args) {
// 创建一个包含每天销售数量的列表
List<Integer> salesList = new ArrayList<>();
salesList.add(10); // 1号销售量
salesList.add(15); // 2号销售量
salesList.add(20); // 3号销售量
// ...
// 获取上个月的起始日期和结束日期
LocalDate now = LocalDate.now();
LocalDate lastMonthStart = now.minus(1, ChronoUnit.MONTHS).withDayOfMonth(1);
LocalDate lastMonthEnd = now.withDayOfMonth(1).minus(1, ChronoUnit.DAYS);
// 遍历每天的销售量,计算上个月的总销售量
int lastMonthTotalSales = 0;
for (int i = 0; i < salesList.size(); i++) {
LocalDate saleDate = now.withDayOfMonth(i + 1);
if (saleDate.isAfter(lastMonthEnd)) {
break;
}
if (saleDate.isAfter(lastMonthStart) || saleDate.isEqual(lastMonthStart)) {
lastMonthTotalSales += salesList.get(i);
}
}
System.out.println("上个月的总销售量为:" + lastMonthTotalSales);
}
}
```
在这个例子中,我们首先创建了一个包含每天销售数量的列表,然后使用Java 8的日期时间API获取上个月的起始日期和结束日期。接下来,我们遍历每天的销售量,只计算在上个月内的销售量,并将它们相加得到上个月的总销售量。最后,我们将结果输出到控制台。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)