【问题标题】:How to get all dates between two dates in java [duplicate]如何在java中获取两个日期之间的所有日期[重复]
【发布时间】:2017-12-21 10:16:42
【问题描述】:
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 ,   

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

这是目前为止的代码(来自 cmets):

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

【问题讨论】:

  • 你会为这个问题吸引反对票,但我还没有反对票。请浏览这两页 - How to ask a good questionHow to create a Minimal, Complete, and Verifiable example
  • 你应该先自己尝试一下,具体问题再回来
  • @SimhachalamSopeti 向我们展示您目前拥有的代码 :)
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建 minimal reproducible example。使用edit 链接改进您的问题 - 不要通过 cmets 添加更多信息。谢谢!
  • 然后:忘记“请尽快回答”。你的优先事项不是我们的。写好问题,答案就会出现。写出这样不好的问题,然后投反对票。

标签: java


【解决方案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】:

    您应该使用 LocalDate 类及其关联的方法“plusDays”在所选参数之间的日期之间循环。

    例如

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

    【讨论】:

      【解决方案3】:
      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
      

      【讨论】:

      • 您应该将输出复制到答案本身,而不是作为图像。
      • 运行并查看输出
      猜你喜欢
      • 2016-11-08
      • 2013-08-09
      • 1970-01-01
      • 2013-07-05
      • 1970-01-01
      • 2011-11-08
      • 2017-06-12
      • 1970-01-01
      相关资源
      最近更新 更多