Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示在Vaadin中选择的日期列表?_Java_Date_Vaadin - Fatal编程技术网

Java 显示在Vaadin中选择的日期列表?

Java 显示在Vaadin中选择的日期列表?,java,date,vaadin,Java,Date,Vaadin,要求: 有两个日期字段可帮助选择开始日期和结束日期。 该范围之间的日期列表应显示在表格中 帮我找到一个解决vaadin 7的方法 public List<Date> getDatesBetween(final Date date1, final Date date2) { List<Date> dates = new ArrayList<Date>(); Calendar calendar = new GregorianCa

要求: 有两个日期字段可帮助选择开始日期和结束日期。 该范围之间的日期列表应显示在表格中

帮我找到一个解决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;
    }

如果我理解正确,你想把两个日期之间的日期列表(我想是一天一天)放在一个表中。 你可以这样做

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;
}
然后每次约会你都可以

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

主要问题是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;
}
公共静态列表getDatesBetween(最终日期date1,最终日期date2){ 列表日期=新建ArrayList(); 日历cal1=新的GregorianCalendar(); cal1.设置时间(日期1); 日历cal2=新的GregorianCalendar(); 计算2.设置时间(日期2); 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); add(cal1.getTime()); } 返回日期; }
我尝试使用Calendar Calendar=new GregoriaCalendar(),将年、月和日期设置为日期列表,并创建了一个while循环:while(Calendar.get(Calendar.year)!=date2.getYear()&&Calendar.get(Calendar.month)!=date2.getDate())这个函数将返回日期列表并将其添加到表中。请编辑您的问题并将其写入整个代码中,这样更易于阅读!我认为您的while语句是一个无限循环,date1永远不会更改。很好的一点..我应该使用“c.getTime()”。我会改正的。谢谢
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
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;
}