当我第一次运行我的代码时(下面),它打印出实时时间(忽略“LOL”,这是出于调试目的..),但是我一直在运行它一段时间,同时做其他的事情和我意识到它不再打印实时时间,只是它第一次运行它时注册的时间。码:
public static String classCheck(){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:MM");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now)+"LOL");
int currentTime = toMins(dtf.format(now));
//Beging collecting preset class timings..
String classOneBegin = "11:00", classTwoBegin = "12:30", classThreeBegin = "14:00", classFourBegin = "16:30", classFourEnd = "18:00";
int classOneBeginX, classTwoBeginX, classThreeBeginX, classFourBeginX, classFourEndX;
classOneBeginX = timeCheck.toMins(classOneBegin);
classTwoBeginX = timeCheck.toMins(classTwoBegin);
classThreeBeginX = timeCheck.toMins(classThreeBegin);
classFourBeginX = timeCheck.toMins(classFourBegin);
classFourEndX = timeCheck.toMins(classFourEnd);
//...End. I feel confident that I could've found a faster, more inclusive solution to this, but time is of the essence..
//Now to compare the time with each "cycle" and return an int that represent the current class.
if (currentTime >= classOneBeginX && currentTime <= classTwoBeginX) {
return "4";
}else if(currentTime >= classTwoBeginX && currentTime <= classThreeBeginX){
return "1";
}else if(currentTime >= classThreeBeginX && currentTime <= classFourBeginX){
return "2";
}else if(currentTime >= classFourBeginX && currentTime <= classFourEndX){
return "3";
}else return "0"; //Outside of class time ..
}
我不确定问题的确切位置,这里的焦点代码是第2~4行
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:MM");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now)+"LOL");
它停留在2:04,但现在是3:08,我期待每次运行我的代码时它会打印实时时间(+“LOL”......)
正如@SotiriosDelimanolis在评论中指出的那样,这个问题的答案是,编码“HH:MM”是不正确的。我应该用“HH:mm”代替。