Java LocalDate Between getLocalDatesBetween(final LocalDate localDate1, final LocalDate localDate2)

Here you can find the source of getLocalDatesBetween(final LocalDate localDate1, final LocalDate localDate2)

Description

get Local Dates Between

License

Open Source License

Declaration

public static List<LocalDate> getLocalDatesBetween(final LocalDate localDate1, final LocalDate localDate2) 

Method Source Code

//package com.java2s;
/*-//from   w  w  w  . jav a  2  s  .co m
 * ============================LICENSE_START============================
 * data.messie (core)
 * =====================================================================
 * Copyright (C) 2013 - 2017 Dr. Raphael Romeikat
 * =====================================================================
 * This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public
License along with this program.  If not, see
<http://www.gnu.org/licenses/gpl-3.0.html>.
 * =============================LICENSE_END=============================
 */

import java.time.LocalDate;

import java.time.temporal.ChronoUnit;
import java.util.Collections;

import java.util.List;
import com.google.common.collect.Lists;

public class Main {
    public static List<LocalDate> getLocalDatesBetween(final LocalDate localDate1, final LocalDate localDate2) {
        if (localDate1 == null || localDate2 == null) {
            return Collections.emptyList();
        }

        if (localDate1.equals(localDate2)) {
            return Lists.newArrayList(localDate1);
        }

        final int daysBetween = (int) Math.abs(ChronoUnit.DAYS.between(localDate1, localDate2));
        final List<LocalDate> localDatesBetween = Lists.newArrayListWithExpectedSize(daysBetween + 1);

        // Add first date
        localDatesBetween.add(localDate1);
        // Add further dates
        final boolean ascending = localDate1.isBefore(localDate2);
        for (int i = 1; i <= daysBetween; i++) {
            final int offset = ascending ? i : -i;
            final LocalDate localDate = localDate1.plusDays(offset);
            localDatesBetween.add(localDate);
        }

        return localDatesBetween;
    }
}

Related

  1. daysBetween(LocalDate date1, LocalDate date2)
  2. daysBetween(LocalDate firstDate, LocalDate secondDate)
  3. getRandomLocalDateBetween(int startYear, int endYear)
  4. isBetween(LocalDate date, LocalDate before, LocalDate after)
  5. isBetween13And18YearsBetween(LocalDate birthLocalDate, LocalDate now)
  6. isBetweenDates(LocalDate date, LocalDate start, LocalDate end)