Reputation:
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). The issue I think is with this line: day.plusDays(i);
, but I do not know a way around it.
Upvotes: 1
Views: 65
Reputation: 240898
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. from doc
Upvotes: 5