如何使用 API Stream 处理 ZonedDateTime?
How to work with ZonedDateTime using API Stream?
我正在尝试将这个使用经典 FOR 的方法传递给 Stream API
public static List<DateBucket> bucketize(ZonedDateTime fromDate,ZonedDateTime toDate, int bucketSize, ChronoUnit bucketSizeUnit) {
List<DateBucket> buckets = new ArrayList<>();
boolean reachedDate = false;
for (int i = 0; !reachedDate; i++) {
ZonedDateTime minDate = fromDate.plus(i * bucketSize, bucketSizeUnit);
ZonedDateTime maxDate = fromDate.plus((i + 1) * bucketSize, bucketSizeUnit);
reachedDate = toDate.isBefore(maxDate);
buckets.add(new DateBucket(minDate.toInstant(), maxDate.toInstant()));
}
return buckets;
}
像这样:
List<DateBucket> buckets =
buckets.stream().map(i-> new DateBucket(minDate.toInstant(),maxDate.toInstant()))
.collect(Collectors.toList());
谢谢
public static List<DateBucket> bucketize(ZonedDateTime fromDate,
ZonedDateTime toDate, int bucketSize, ChronoUnit bucketSizeUnit) {
return Stream.iterate(fromDate,
zdt -> zdt.isBefore(toDate),
zdt -> zdt.plus(bucketSize, bucketSizeUnit))
.map(zdt -> new DateBucket(zdt.toInstant(),
zdt.plus(bucketSize, bucketSizeUnit).toInstant()))
.collect(Collectors.toList());
}
尝试一下:
ZoneId zone = ZoneId.of("Asia/Urumqi");
ZonedDateTime from = ZonedDateTime.of(2020, 8, 18, 9, 0, 0, 0, zone);
ZonedDateTime to = ZonedDateTime.of(2020, 8, 20, 17, 0, 0, 0, zone);
List<DateBucket> buckets = bucketize(from, to, 1, ChronoUnit.DAYS);
buckets.forEach(System.out::println);
输出:
2020-08-18T03:00:00Z - 2020-08-19T03:00:00Z
2020-08-19T03:00:00Z - 2020-08-20T03:00:00Z
2020-08-20T03:00:00Z - 2020-08-21T03:00:00Z
我不确定在这里使用流操作是否有利,但如您所见,这当然是可能的。
我正在使用的iterate
方法是在Java9.
中介绍的
我正在尝试将这个使用经典 FOR 的方法传递给 Stream API
public static List<DateBucket> bucketize(ZonedDateTime fromDate,ZonedDateTime toDate, int bucketSize, ChronoUnit bucketSizeUnit) {
List<DateBucket> buckets = new ArrayList<>();
boolean reachedDate = false;
for (int i = 0; !reachedDate; i++) {
ZonedDateTime minDate = fromDate.plus(i * bucketSize, bucketSizeUnit);
ZonedDateTime maxDate = fromDate.plus((i + 1) * bucketSize, bucketSizeUnit);
reachedDate = toDate.isBefore(maxDate);
buckets.add(new DateBucket(minDate.toInstant(), maxDate.toInstant()));
}
return buckets;
}
像这样:
List<DateBucket> buckets =
buckets.stream().map(i-> new DateBucket(minDate.toInstant(),maxDate.toInstant()))
.collect(Collectors.toList());
谢谢
public static List<DateBucket> bucketize(ZonedDateTime fromDate,
ZonedDateTime toDate, int bucketSize, ChronoUnit bucketSizeUnit) {
return Stream.iterate(fromDate,
zdt -> zdt.isBefore(toDate),
zdt -> zdt.plus(bucketSize, bucketSizeUnit))
.map(zdt -> new DateBucket(zdt.toInstant(),
zdt.plus(bucketSize, bucketSizeUnit).toInstant()))
.collect(Collectors.toList());
}
尝试一下:
ZoneId zone = ZoneId.of("Asia/Urumqi");
ZonedDateTime from = ZonedDateTime.of(2020, 8, 18, 9, 0, 0, 0, zone);
ZonedDateTime to = ZonedDateTime.of(2020, 8, 20, 17, 0, 0, 0, zone);
List<DateBucket> buckets = bucketize(from, to, 1, ChronoUnit.DAYS);
buckets.forEach(System.out::println);
输出:
2020-08-18T03:00:00Z - 2020-08-19T03:00:00Z 2020-08-19T03:00:00Z - 2020-08-20T03:00:00Z 2020-08-20T03:00:00Z - 2020-08-21T03:00:00Z
我不确定在这里使用流操作是否有利,但如您所见,这当然是可能的。
我正在使用的iterate
方法是在Java9.