在Java中获取两天之间的可用天数

时间:2018-10-19 08:21:09

标签: java datediff

我是Java的新用户,我想构建预订系统,现在我尝试获取两天之间的可用天,而无需预订天数。但是,但是我的代码运行得不好。

这是我的代码

Connection connection = DBConnection.getInstance().getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * from A");
        ResultSet executeQuery = preparedStatement.executeQuery();

        while (executeQuery.next()) {
            String refid = executeQuery.getString("REFID");
            statDate = executeQuery.getDate("Start_date");

            endDate = executeQuery.getDate("End_date");
            System.out.println("ref no : " + refid + "   statDate :" + statDate + "     end date :" + endDate);
            System.out.println("\n");
            int month = statDate.getMonth();

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.MONTH, month);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

            //
            for (int i = 1; i < maxDay; i++) {
                cal.set(Calendar.DAY_OF_MONTH, i + 1);

                String format = df.format(cal.getTime());
                dateOfMonth.add(format);

                addDays = addDays(statDate, 2);

                if (df.format(cal.getTime()).equals("" + statDate) || df.format(cal.getTime()).equals("" + endDate)) {

                    //System.out.println(statDate + " is not available !");
                    System.out.println("---");

                } else {

                    int bDays = endDate.getDate() - statDate.getDate();
                    //System.out.println(bDays);

                    //===========
                    List<DateTime> between = getDateRange(DateTime.parse(statDate.toString()), DateTime.parse(endDate.toString()));

                    for (DateTime dateTime : between) {
                        Date da = dateTime.toDate();

                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");                          
                        dateOfBooked.add(sdf.format(da));
                    }

                    System.out.println(df.format(cal.getTime()));
                }

            }
            System.out.println("\n");
        }

这是我的数据库脚本

insert into  A  values('1','ref1','2018-02-18','2018-02-25');
insert into  A  values('2','ref1','2018-03-10','2018-03-17');
insert into  A  values('3','ref1','2018-03-20','2018-03-27');

我尝试从您预订的日子中获取可用的日子。我如何只打印可用的日子?

output

0 个答案:

没有答案