要求: 有两个dateFields,可帮助选择开始日期和结束日期。 该范围之间的日期列表应显示在表格中。
帮助我解决vaadin 7的问题。
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;
}
然后使用以下方式显示此列表:
table=new Table("Employee dates!");
table.removeAllItems();
/// ...... within the loop
table.addItem();
/// ...... within the loop
NOTE: an alternative way of getting the list of dates is appreciated.
答案 0 :(得分:0)
如果我已经正确理解你想要在两个日期之间列出日期列表(我假设的日期)。 你可以做点什么
public List<Date> getDatesBetween(final Date date1, final Date date2) {
List<Date> dates = new ArrayList<Date>();
Calendar c = Calendar.getInstance();
c.setTime(date1);
//dates.add(date1); //use it if you need the first day
while(c.getTime().compareTo(date2) < 0) { //date1 is lesser than date2, use <= if you need the last day
c.add(Calendar.DATE, 1);
dates.add(c.getTime());
}
return dates;
}
现在将这些值正确地放在表中,因为它不是纯粹的&#34;项目&#34; s
Table tableDates = new Table("Dates from ... to ...");
tableDates.setSizeFull();
tableDates.setImmediate(true);
tableDates.addContainerProperty("date", Date.class, null);
tableDates.setColumnHeader("date", "Date");
然后对于你可以做的每个日期
Object[] tableRow = new Object[] {date}; //date is a Date object..you can merge this in the "getDatesBetween" method above
tableDates.addItem(tableRow , null); //null -> autogenerated id
答案 1 :(得分:0)
主要问题是Date类方法已被弃用(并且具有误导性)
getYear()
javadoc写道:此日期所代表的年份,减去1900年。
此外,您必须在while语句表达式中将&&
替换为||
。
我的建议:
public static List<Date> getDatesBetween(final Date date1, final Date date2) {
List<Date> dates = new ArrayList<Date>();
Calendar cal1 = new GregorianCalendar();
cal1.setTime(date1);
Calendar cal2 = new GregorianCalendar();
cal2.setTime(date2);
while (cal1.get(Calendar.YEAR) != cal2.get(Calendar.YEAR)
|| cal1.get(Calendar.MONTH) != cal2.get(Calendar.MONTH)
|| cal1.get(Calendar.DATE) != cal2.get(Calendar.DATE)) {
cal1.add(Calendar.DATE, 1);
dates.add(cal1.getTime());
}
return dates;
}