Java LocalDate Calculate stream(LocalDate startInclusive, LocalDate endExclusive)

Here you can find the source of stream(LocalDate startInclusive, LocalDate endExclusive)

Description

Streams the set of dates included in the range.

License

Open Source License

Parameter

Parameter Description
startInclusive the start date
endExclusive the end date

Return

the stream of dates from the start to the end

Declaration

static Stream<LocalDate> stream(LocalDate startInclusive, LocalDate endExclusive) 

Method Source Code

//package com.java2s;
/**/*from  w ww  .  j a v a  2 s.  c  o  m*/
 * Copyright (C) 2015 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */

import java.time.LocalDate;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class Main {
    /**
     * Streams the set of dates included in the range.
     * <p>
     * This returns a stream consisting of each date in the range.
     * The stream is ordered.
     * 
     * @param startInclusive  the start date
     * @param endExclusive  the end date
     * @return the stream of dates from the start to the end
     */
    static Stream<LocalDate> stream(LocalDate startInclusive, LocalDate endExclusive) {
        Iterator<LocalDate> it = new Iterator<LocalDate>() {
            private LocalDate current = startInclusive;

            @Override
            public LocalDate next() {
                LocalDate result = current;
                current = plusDays(current, 1);
                return result;
            }

            @Override
            public boolean hasNext() {
                return current.isBefore(endExclusive);
            }
        };
        long count = endExclusive.toEpochDay() - startInclusive.toEpochDay() + 1;
        Spliterator<LocalDate> spliterator = Spliterators.spliterator(it, count,
                Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.DISTINCT | Spliterator.ORDERED
                        | Spliterator.SORTED | Spliterator.SIZED | Spliterator.SUBSIZED);
        return StreamSupport.stream(spliterator, false);
    }

    /**
     * Adds a number of days to the date.
     * <p>
     * Faster than the JDK method.
     * 
     * @param date  the date to add to
     * @param daysToAdd  the days to add
     * @return the new date
     */
    static LocalDate plusDays(LocalDate date, int daysToAdd) {
        if (daysToAdd == 0) {
            return date;
        }
        // add the days to the current day-of-month
        // if it is guaranteed to be in this month or the next month then fast path it
        // (59th Jan is 28th Feb, 59th Feb is 31st Mar)
        long dom = date.getDayOfMonth() + daysToAdd;
        if (dom > 0 && dom <= 59) {
            int monthLen = date.lengthOfMonth();
            int month = date.getMonthValue();
            int year = date.getYear();
            if (dom <= monthLen) {
                return LocalDate.of(year, month, (int) dom);
            } else if (month < 12) {
                return LocalDate.of(year, month + 1, (int) (dom - monthLen));
            } else {
                return LocalDate.of(year + 1, 1, (int) (dom - monthLen));
            }
        }
        long mjDay = Math.addExact(date.toEpochDay(), daysToAdd);
        return LocalDate.ofEpochDay(mjDay);
    }
}

Related

  1. parseDate(LocalDate date)
  2. period(LocalDate startDate, LocalDate endDate)
  3. plusDays(LocalDate date, int daysToAdd)
  4. rebucketingArray(List targetDates, double[] rebucketedSensitivityAmounts, double sensitivityAmount, LocalDate sensitivityDate)
  5. roundDown(final LocalDate dateTime, final TemporalUnit temporalUnit, final int size)
  6. stringDateToLocalDate(String day, String month, String year)
  7. stringToLocalDate(String birthDate)
  8. subscriptionDeletionRequired(LocalDate vehicleMotExpiryDate, LocalDate requestDate)
  9. toMilliseconds(LocalDate localDate)