Ian*_*Ian 2 java loops calendar if-statement date
首先,非常感谢人们提供的任何帮助!
好的,所以我在这里有这个循环,我似乎遇到了if语句的问题.
Eclipse告诉我下面的查询部分和"else"中的代码是"死代码":
|| calDateOfDay.DAY_OF_WEEK == Calendar.SUNDAY
Run Code Online (Sandbox Code Playgroud)
但我不知道为什么.
此外,当它执行时,它总是执行"if"语句中的代码,(据我的理解)这意味着每天都是星期六.但是,我知道这个日子有正确的价值(确实有几天被检查不是星期六).我知道这一点,因为我已经通过Eclipse中的代码进行了调试,逐个检查日期的值.
老实说,即使我试图解释它,我也很困惑!:S
我希望代码能做的是:
基本上就是这样.
for (DayEntry dayEntry : daySet){
//day value to be filled
Day day = new Day();
Calendar calDateOfDay = new GregorianCalendar();
//set the date of the day
calDateOfDay.setTime(dayEntry.getDateOfDay());
day.setDate(calDateOfDay);
//set the hours of the day
day.setWorkHours(dayEntry.getHours());
if (Calendar.DAY_OF_WEEK == Calendar.SATURDAY || Calendar.DAY_OF_WEEK == Calendar.SUNDAY ){
day.setType(DayType.WEEK_END);
}else{
day.setType(DayType.WEEK_DAY);
}
//add the day to the week
week.addDay(day);
}
Run Code Online (Sandbox Code Playgroud)
现在,我上面编写代码的原因是因为当我编写如下代码时,Eclipse告诉我应该以DAY_OF_WEEK
静态方式访问静态变量,所以我也不确定这是否是导致的一个问题.
for (DayEntry dayEntry : daySet){
//day value to be filled
Day day = new Day();
Calendar calDateOfDay = new GregorianCalendar();
//set the date of the day
calDateOfDay.setTime(dayEntry.getDateOfDay());
day.setDate(calDateOfDay);
//set the hours of the day
day.setWorkHours(dayEntry.getHours());
if (calDateOfDay.DAY_OF_WEEK == Calendar.SATURDAY || calDateOfDay.DAY_OF_WEEK == Calendar.SUNDAY ){
day.setType(DayType.WEEK_END);
}else{
day.setType(DayType.WEEK_DAY);
}
//add the day to the week
week.addDay(day);
}
Run Code Online (Sandbox Code Playgroud)
DAY_OF_WEEK
是一个常量,包含星期几的字段编号.您的代码使用它,就像它包含该字段的值一样,但它不包含该字段的值.
要修复代码,只需替换calDateOfDay.DAY_OF_WEEK
为calDateOfDay.get(Calendar.DAY_OF_WEEK)
.