public class DateUtils { public static List<YearMonth> getMonthsBetween(LocalDate start, LocalDate end) { List<YearMonth> months = new ArrayList<>(); long monthsBetween = ChronoUnit.MONTHS.between(start.withDayOfMonth(1), end.withDayOfMonth(1)); for (int i = 0; i <= monthsBetween; i++) { YearMonth yearMonth = YearMonth.from(start.withDayOfMonth(1).plusMonths(i)); months.add(yearMonth); } return months; } }
时间: 2024-04-27 16:25:10 浏览: 13
这段代码定义了一个名为DateUtils的类,其中包含一个名为getMonthsBetween的静态方法,该方法接受两个LocalDate类型的参数:start和end,表示日期范围的开始和结束。
该方法返回一个List<YearMonth>类型的列表,其中包含了从开始日期到结束日期之间的所有月份。
在方法内部,首先通过使用ChronoUnit类的MONTHS常量计算出开始日期和结束日期之间的月份数。然后,使用for循环遍历每一个月份,并使用YearMonth类的from方法将LocalDate类型的日期转换为YearMonth类型的月份,最后将所有月份添加到列表中,并返回该列表。
相关问题
java中在List<String>日期集合中获取间隔两天的日期
可以使用Java 8中的日期时间类`LocalDate`来实现。
以下是一个示例代码,可以获取`dateList`中相邻两个日期之间间隔为两天的所有日期,并将它们存储在一个`List`中。
```java
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class DateUtils {
public static List<LocalDate> getDatesWithTwoDaysGap(List<String> dateList) {
List<LocalDate> result = new ArrayList<>();
for (int i = 0; i < dateList.size() - 1; i++) {
LocalDate date1 = LocalDate.parse(dateList.get(i));
LocalDate date2 = LocalDate.parse(dateList.get(i+1));
if (date2.minusDays(2).isEqual(date1)) {
result.add(date2.minusDays(1));
}
}
return result;
}
}
```
使用该方法,可以轻松获取`dateList`中间隔为两天的所有日期。例如:
```java
List<String> dateList = Arrays.asList("2021-01-01", "2021-01-03", "2021-01-05", "2021-01-06", "2021-01-09");
List<LocalDate> result = DateUtils.getDatesWithTwoDaysGap(dateList);
System.out.println(result);
```
将输出:
```
[2021-01-05]
```
代码优化 List<String> allRows = this.getHBaseRows(); if (CollectionUtils.isEmpty(allRows)) { return success("暂无数据"); } // 数据转换 List<PositionInfo> allInfos = allRows.stream() .map(ar -> JSONObject.parseObject(ar, PositionInfo.class)) .filter(ar-> ar.getTimestamp() > DateUtils.addHours(new Date(),-1).getTime()) .collect(Collectors.toList()); // 获取每个用户最近一次的记录 List<PositionInfo> result = new ArrayList<>(allInfos.stream() .collect(Collectors.toMap(PositionInfo::getUserId, Function.identity(), BinaryOperator.maxBy(Comparator.comparing(PositionInfo::getTimestamp)))) .values());
这段代码可以进行以下优化:
1. 使用Optional避免空指针异常
可以使用Optional避免空指针异常,如下:
```
List<String> allRows = this.getHBaseRows();
return Optional.ofNullable(allRows)
.filter(CollectionUtils::isNotEmpty)
.map(rows -> {
// 数据转换
List<PositionInfo> allInfos = rows.stream()
.map(ar -> JSONObject.parseObject(ar, PositionInfo.class))
.filter(ar -> ar.getTimestamp() > DateUtils.addHours(new Date(), -1).getTime())
.collect(Collectors.toList());
// 获取每个用户最近一次的记录
List<PositionInfo> result = new ArrayList<>(allInfos.stream()
.collect(Collectors.toMap(PositionInfo::getUserId, Function.identity(),
BinaryOperator.maxBy(Comparator.comparing(PositionInfo::getTimestamp))))
.values());
return success(result);
})
.orElse(success("暂无数据"));
```
2. 使用Lambda表达式简化代码
可以使用Lambda表达式简化代码,如下:
```
List<String> allRows = this.getHBaseRows();
return Optional.ofNullable(allRows)
.filter(CollectionUtils::isNotEmpty)
.map(rows -> rows.stream()
.map(ar -> JSONObject.parseObject(ar, PositionInfo.class))
.filter(ar -> ar.getTimestamp() > DateUtils.addHours(new Date(), -1).getTime())
.collect(Collectors.groupingBy(PositionInfo::getUserId,
Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparing(PositionInfo::getTimestamp)),
Optional::get)))
.values())
.map(ArrayList::new)
.map(this::success)
.orElse(success("暂无数据"));
```
这段代码使用了groupingBy方法将数据按照userId分组,然后使用collectingAndThen方法将每组数据取最大的timestamp,最后使用values方法获取所有分组的结果。