如何使用API Stream处理ZonedDateTime?

huangapple go评论73阅读模式
英文:

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&lt;DateBucket&gt; bucketize(ZonedDateTime fromDate,ZonedDateTime toDate,	int bucketSize,	ChronoUnit bucketSizeUnit) {
	  

	List&lt;DateBucket&gt; buckets = new ArrayList&lt;&gt;();
	   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&lt;DateBucket&gt; buckets = 
	buckets.stream().map(i-&gt; 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

Im unsure whether its 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&lt;DateBucket&gt; bucketize(ZonedDateTime fromDate,
		ZonedDateTime toDate, int bucketSize, ChronoUnit bucketSizeUnit) {
	return Stream.iterate(fromDate,
					zdt -&gt; zdt.isBefore(toDate),
					zdt -&gt; zdt.plus(bucketSize, bucketSizeUnit))
			.map(zdt -&gt; new DateBucket(zdt.toInstant(),
					zdt.plus(bucketSize, bucketSizeUnit).toInstant()))
			.collect(Collectors.toList());
}

To try it out:

	ZoneId zone = ZoneId.of(&quot;Asia/Urumqi&quot;);
	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&lt;DateBucket&gt; 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.

huangapple
  • 本文由 发表于 2020年8月16日 06:15:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63431287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定