java 获取两个日期之间的日期列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16785643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:00:34  来源:igfitidea点击:

Get the list of dates between two dates

javadatejcalendar

提问by Maguzu

I'm using JDateChooserand I'm making a program that output the list of dates between the selected dates. for example:

我正在使用JDateChooser并且正在制作一个输出所选日期之间的日期列表的程序。例如:

date1= Jan 1, 2013  // Starting Date

date2= Jan 16,2013  // End Date

then it will output

然后它会输出

Jan 2, 2013...
Jan 3, 2013.. 
Jan 4, 2013..

and so on... until it reaches the end date.

依此类推......直到它到达结束日期。

I already finish working on the my program that once you click a date on the JDatechooserit will output the end date automatically. (selected date + 15 days = end dates)

我已经完成了我的程序,一旦你点击一个日期,JDatechooser它就会自动输出结束日期。(所选日期 + 15 天 = 结束日期)

I download the JCalendaror JDateChooserhere: http://www.toedter.com/en/jcalendar/

我下载JCalendarJDateChooser在这里:http: //www.toedter.com/en/jcalendar/

回答by MadProgrammer

You should try using Calendar, which will allow you to walk from one date to another...

您应该尝试使用Calendar,这将允许您从一个日期走到另一个日期...

Date fromDate = ...;
Date toDate = ...;

System.out.println("From " + fromDate);
System.out.println("To " + toDate);

Calendar cal = Calendar.getInstance();
cal.setTime(fromDate);
while (cal.getTime().before(toDate)) {
    cal.add(Calendar.DATE, 1);
    System.out.println(cal.getTime());
}

Updated

更新

This example will include the toDate. You can rectify this by creating a second calendar that acts as the lastDateand subtracting a day from it...

此示例将包括toDate. 您可以通过创建第二个日历lastDate并从中减去一天来纠正此问题...

Calendar lastDate = Calendar.getInstance();
lastDate.setTime(toDate);
lastDate.add(Calendar.DATE, -1);

Calendar cal = Calendar.getInstance();
cal.setTime(fromDate);
while (cal.before(lastDate)) {...}

This will give you all the dates "between" the start and end dates, exclusively.

这将为您提供开始日期和结束日期“之间”的所有日期。

Adding Dates to an ArrayList

将日期添加到 ArrayList

List<Date> dates = new ArrayList<Date>(25);
Calendar cal = Calendar.getInstance();
cal.setTime(fromDate);
while (cal.getTime().before(toDate)) {
    cal.add(Calendar.DATE, 1);
    dates.add(cal.getTime());
}

2018 java.timeUpdate

2018年java.time更新

Time moves on, things improve. Java 8 introduces the new java.timeAPI which has superseded the "date" classes and should, as a preference, be used instead

时间在前进,情况在改善。Java 8 引入了新的java.timeAPI,它取代了“日期”类,作为首选,应该改用它

LocalDate fromDate = LocalDate.now();
LocalDate toDate = LocalDate.now();

List<LocalDate> dates = new ArrayList<LocalDate>(25);

LocalDate current = fromDate;
//current = current.plusDays(1); // If you don't want to include the start date
//toDate = toDate.plusDays(1); // If you want to include the end date
while (current.isBefore(toDate)) {
    dates.add(current));
    current = current.plusDays(1);
}