how to get a list of dates between two dates in java

后端 未结 22 1514
余生分开走
余生分开走 2020-11-22 13:24

I want a list of dates between start date and end date.

The result should be a list of all dates including the start and end date.

相关标签:
22条回答
  • 2020-11-22 13:53

    java.time Package

    If you are using Java 8, there is a much cleaner approach. The new java.time package in Java 8 incorporates the features of the Joda-Time API.

    Your requirement can be solved using the below code:

    String s = "2014-05-01";
    String e = "2014-05-10";
    LocalDate start = LocalDate.parse(s);
    LocalDate end = LocalDate.parse(e);
    List<LocalDate> totalDates = new ArrayList<>();
    while (!start.isAfter(end)) {
        totalDates.add(start);
        start = start.plusDays(1);
    }
    
    0 讨论(0)
  • 2020-11-22 13:53

    One solution would be to create a Calendar instance, and start a cycle, increasing it's Calendar.DATE field until it reaches the desired date. Also, on each step you should create a Date instance (with corresponding parameters), and put it to your list.

    Some dirty code:

        public List<Date> getDatesBetween(final Date date1, final Date date2) {
        List<Date> dates = new ArrayList<Date>();
    
        Calendar calendar = new GregorianCalendar() {{
            set(Calendar.YEAR, date1.getYear());
            set(Calendar.MONTH, date1.getMonth());
            set(Calendar.DATE, date1.getDate());
        }};
    
        while (calendar.get(Calendar.YEAR) != date2.getYear() && calendar.get(Calendar.MONTH) != date2.getMonth() && calendar.get(Calendar.DATE) != date2.getDate()) {
            calendar.add(Calendar.DATE, 1);
            dates.add(new Date(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE)));
        }
    
        return dates;
    }
    
    0 讨论(0)
  • 2020-11-22 13:53
    List<Date> dates = new ArrayList<Date>();
    String str_date = "DD/MM/YYYY";
    String end_date = "DD/MM/YYYY";
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    Date startDate = (Date)formatter.parse(str_date); 
    Date endDate = (Date)formatter.parse(end_date);
    long interval = 1000 * 60 * 60; // 1 hour in milliseconds
    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);
        //Write your code for storing dates to list
    }
    
    0 讨论(0)
  • 2020-11-22 13:56

    Get the number of days between dates, inclusive.

    public static List<Date> getDaysBetweenDates(Date startdate, Date enddate)
    {
        List<Date> dates = new ArrayList<Date>();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startdate);
    
        while (calendar.getTime().before(enddate))
        {
            Date result = calendar.getTime();
            dates.add(result);
            calendar.add(Calendar.DATE, 1);
        }
        return dates;
    }
    
    0 讨论(0)
提交回复
热议问题