获取两个日期之间的日期列表 [英] Get the list of dates between two dates
问题描述
我正在使用 JDateChooser
,我正在制作一个程序,输出所选日期之间的日期列表。例如:
I'm using JDateChooser
and I'm making a program that output the list of dates between the selected dates. for example:
date1= Jan 1, 2013 // Starting Date
date2= Jan 16,2013 // End Date
然后它将输出
Jan 2, 2013...
Jan 3, 2013..
Jan 4, 2013..
等等...直到达到结束日期。
and so on... until it reaches the end date.
我已经完成工作在我的程序中,一旦您点击 JDatechooser
之后的日期,它将自动输出结束日期。 (选择日期+ 15天=结束日期)
I already finish working on the my program that once you click a date on the JDatechooser
it will output the end date automatically. (selected date + 15 days = end dates)
我下载 JCalendar
或 JDateChooser
这里: http://www.toedter.com/en/jcalendar/
推荐答案
您应该尝试使用 日历
,这将允许您从一个日期走到另一个日期。 ..
You should try using Calendar
, which will allow you to walk from one date to another...
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());
}
更新
此示例将包含 toDate
。您可以通过创建一个第二个日历,作为 lastDate
并从中减去一天...
This example will include the toDate
. You can rectify this by creating a second calendar that acts as the lastDate
and subtracting a day from it...
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.
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());
}
这篇关于获取两个日期之间的日期列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!