Joda-Time - 没有增加的日子

时间:2014-10-01 06:35:23

标签: java date datetime jodatime

这是针对学校项目的。

以下是代码:

// get the remaining available hours
public int getAvailableHours(Subject sub) {

    DateTime dt = new DateTime();
    int hours = 0;

    int mondays = 0;
    int tuesdays = 0;
    int wednesdays = 0;
    int thursdays = 0;
    int fridays = 0;
    int saturdays = 0;
    int sundays = 0;

    if(sub != null) {
        if(!sub.getExamDate().isBefore(dt)) {
            int days = Days.daysBetween(dt, sub.getExamDate()).getDays(); 

            // DEBUGGING - take out
            System.out.println(days);
            System.out.println(dt);
            System.out.println(sub.getExamDate());

            DateTime day = new DateTime();
            for(int i = 1; i < days; i++) {
                day.plusDays(i);

                // DEBUGGING - take out
                System.out.println(day.dayOfWeek().getAsText());
                System.out.println(day.dayOfMonth().getAsText());

                switch(day.dayOfWeek().getAsText()) {
                    case "Monday":
                        mondays++;
                        break;
                    case "Tuesday":
                        tuesdays++;
                        break;
                    case "Wednesday":
                        wednesdays++;
                        break;
                    case "Thursday":
                        thursdays++;
                        break;
                    case "Friday":
                        fridays++;
                        break;
                    case "Saturday":
                        saturdays++;
                        break;
                    case "Sunday":
                        sundays++;
                        break;
                    default:

                        break;
                }
            }
            hours = (mondays * hoursOnMonday) + (tuesdays * hoursOnTuesday) + 
                    (wednesdays * hoursOnWednesday) + (thursdays * hoursOnThursday)
                    + (fridays * hoursOnFriday) + (saturdays * hoursOnSaturday) 
                    + (sundays * hoursOnSunday);
        }
    }

    return hours;
}

我要做的是创建一个学习时间表调度程序。我的程序中有一部分允许用户输入他们每天想要学习的时间。所以我上面所做的是循环多少天,看看每天发生了多少(所以,现在和该主题的考试日期之间有多少星期一,星期二等等)所以我可以计算出多少天用户必须学习的总小时数。

但是现在,我尝试循环通过它,它总是返回&#34;星期三&#34;,它没有递增。今天,在南非,它是在10月1日的星期三,但应用程序现在似乎增加了一天,意识到它正在观察10月2日(明天)的星期四。我认为问题在于这一行:day.plusDays(i);,但我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:5)

它是不可变的实例,因此您必须将其捕获回来

day = day.plusDays(i);
  

此datetime实例是不可变的,不受此方法调用的影响。 from doc