简体   繁体   English

乔达时间-天数未增加

[英]Joda-Time - days not incrementing

This is for a school project. 这是用于学校项目。

Here is the code: 这是代码:

// 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;
}

What I am trying to do is create a study time table scheduler. 我想做的是创建一个学习时间表调度程序。 There is a part in my program that allows the user to input how long they want to study on each day. 我的程序中有一个部分,允许用户输入他们每天想学习多长时间。 So what I am doing above is looping through how many days there are and seeing how many of each day occurs (so, how many mondays, tuesdays, etc. are between now and the exam date of that subject) so I can calculate how many total hours the user has to study. 所以我在上面做的事情是循环浏览多少天,看看每天发生多少天(所以,从现在到该科目的考试日期之间有多少个星期一,星期二等),所以我可以计算出多少用户必须学习的总小时数。

But now, I try loop through it, and it always returns "Wednesday", it is not incrementing. 但是现在,我尝试遍历它,它始终返回“星期三”,并且没有递增。 Today, in South Africa, it is Wednesday the first of October, but the app does now seem to increment the day to realize that it is looking at Thursday the second of October (tomorrow). 今天,在南非,它是10月1日的星期三,但是该应用现在似乎确实增加了一天,以意识到它正在查看10月2日(明天)的星期四。 The issue I think is with this line: day.plusDays(i); 我认为这是与此行有关的问题: day.plusDays(i); , but I do not know a way around it. ,但我不知道如何解决。

it is immutable instance, so you would have to catch it back 它是不可变的实例,因此您必须将其捕获

day = day.plusDays(i);

This datetime instance is immutable and unaffected by this method call. 此datetime实例是不可变的,不受此方法调用的影响。 from doc 来自doc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM