英文:
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());
英文:
I'm trying to pass this method that uses a clasic FOR to a 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;
}
something like this:
List<DateBucket> buckets = 
	buckets.stream().map(i-> new DateBucket(minDate.toInstant(),maxDate.toInstant()))
					.collect(Collectors.toList());
Thanks
答案1
得分: 1
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());
}
To try it out:
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);
Output:
>     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
I’m unsure whether it’s advantageous to use a stream operation here, but as you see, it is certainly possible.
The `iterate` method that I am using was introduced in Java 9.
英文:
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());
}
To try it out:
	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);
Output:
>     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
I’m unsure whether it’s advantageous to use a stream operation here, but as you see, it is certainly possible.
The iterate method that I am using was introduced in Java 9.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论