-4
List<LocalDate> totalDates = new ArrayList<>();

String stratdate = 20-12-2017;

String enddate = 25-12-2017;

output:

20-12-2017,
21-12-2017,
22-12-2017,
23-12-2017,
24-12-2017,
25-12-2017 ,   

Please reply me as early as possible if any solution regrding this.

This is the code so far (from comments):

    List<LocalDate> totalDates = new ArrayList<>(); 
    String stratdate = fromdate1;
    String enddate = todate1; 
    LocalDate start = LocalDate.parse(stratdate); 
    LocalDate end = LocalDate.parse(enddate); 
    System.out.println("Converted start date is : " + start); 
    System.out.println("Converted end date is : " + end); 
    while (!start.isBefore(end)) { 
        totalDates.add(start); 
        start = start.plusDays(1); 
        System.out.println("dates are ..."+start); 
    } 
Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
  • 5
    You are going to attract downvotes for this question, however I have not downvoted yet. Please go through these two pages - [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – vinS Dec 21 '17 at 10:18
  • Use a `while` loop? – rst Dec 21 '17 at 10:18
  • 1
    You should first try it by your self and come back with specific problems – Jens Dec 21 '17 at 10:19
  • yes,I am using while loop – Simhachalam Sopeti Dec 21 '17 at 10:19
  • 1
    @SimhachalamSopeti Show us the code you have so far :) – Michael Berry Dec 21 '17 at 10:19
  • @Jens...yeah i was tried but no result that's why i asking.. – Simhachalam Sopeti Dec 21 '17 at 10:21
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the [edit] link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Dec 21 '17 at 10:22
  • 2
    And then: forget about "please answer as quickly as possible." Your priorities are not ours. Write good questions, and answers will come. Write bad questions like this, and downvotes come in. – GhostCat Dec 21 '17 at 10:23
  • @berry120.. List totalDates = new ArrayList<>(); String stratdate = fromdate1; String enddate = todate1; LocalDate start = LocalDate.parse(stratdate); LocalDate end = LocalDate.parse(enddate); System.out.println("Converted start date is : " + start); System.out.println("Converted end date is : " + end); while (!start.isBefore(end)) { totalDates.add(start); start = start.plusDays(1); System.out.println("dates are ..."+start); } – Simhachalam Sopeti Dec 21 '17 at 10:23
  • Have a look at the `while` condition. Think about it. – DodgyCodeException Dec 21 '17 at 10:28

3 Answers3

3

Using Java 8 you can do something like :

    String strat = "2017-12-25";
    String end   = "2017-12-25";
    LocalDate startDate = LocalDate.parse(strat);
    LocalDate endDate = LocalDate.parse(end);
    long daysBetween = ChronoUnit.DAYS.between(startDate, endDate)+1;
    List<LocalDate> totalDates =  
            LongStream.iterate(0,i -> i+1)
            .limit(daysBetween).mapToObj(i->startDate.plusDays(i))
            .collect(Collectors.toList());
    System.out.println(totalDates);

Using Java 9

List<LocalDate> totalDates =  startDate.datesUntil(endDate)
  .collect(Collectors.toList());
Eritrean
  • 15,851
  • 3
  • 22
  • 28
1

You should use the LocalDate class and its associated method "plusDays" to cycle through the dates between your chosen parameters.

For example

String startString = "2017-12-20";
String endString = "2017-12-25";

LocalDate incrementingDate = LocalDate.parse(startString);
LocalDate endDate = LocalDate.parse(endString);

List<LocalDate> allDates = new ArrayList<>();

if (incrementingDate.isAfter(endDate)) {
    throw new IllegalStateException("start date must be before or equal to end date");
}

while (!incrementingDate.isAfter(endDate)) {
    allDates.add(incrementingDate);
    incrementingDate = incrementingDate.plusDays(1);
}

System.out.println(allDates);
AdamPillingTech
  • 456
  • 2
  • 7
0
import java.util.*;
import java.text.*;

public class MyClass {
    public static void main(String args[]) {
        List<Date> dates = new ArrayList<Date>();

String str_date ="27/08/2010";
String end_date ="02/09/2010";

DateFormat formatter ; 

formatter = new SimpleDateFormat("dd/MM/yyyy");
try {
Date  startDate = (Date)formatter.parse(str_date); 
Date  endDate = (Date)formatter.parse(end_date);
long interval = 24*1000 * 60 * 60; // 1 hour in millis
long endTime =endDate.getTime() ; // create your endtime here, possibly using Calendar or Date
long curTime = startDate.getTime();
while (curTime < endTime) {
    dates.add(new Date(curTime));
    curTime += interval;
}
for(int i=0;i<dates.size();i++){
    Date lDate =(Date)dates.get(i);
    String ds = formatter.format(lDate);    
    System.out.println(" Date is ..." + ds);
}
}catch (Exception e) {

}
    }
}

Result:
Date is 27/08/2010
Date is 28/08/2010
Date is 29/08/2010
Date is 30/08/2010
Date is 31/08/2010
Date is 01/09/2010
gangu
  • 59
  • 3