如何在java中获取两个日期之间的所有日期

时间:2017-12-21 10:16:42

标签: java

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

String stratdate = 20-12-2017;

String enddate = 25-12-2017;

输出:

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

如果有任何解决方案,请尽早回复我。

这是迄今为止的代码(来自评论):

    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); 
    } 

3 个答案:

答案 0 :(得分:1)

您应该使用LocalDate类及其相关方法&#34; plusDays&#34;循环选择所选参数之间的日期。

例如

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);

答案 1 :(得分:1)

使用Java 8,您可以执行以下操作:

    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);

使用Java 9

List<LocalDate> totalDates =  startDate.datesUntil(endDate)
  .collect(Collectors.toList());

答案 2 :(得分: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
相关问题