我有一个方法可以从与hibernate连接的SQL数据库中获取记录(称为runtime)。当我想按日期过滤结果时,问题就来了。我用要查看结果的日期声明了变量,但从未通过过'if'语句。在调试器中,我看到要比较的这两个日期具有相同的格式,我不知道为什么它不起作用。有什么想法吗?
我在屏幕上附加了变量的调试器状态,问题出在我检查-> runtime.getDateTime是否在'reportDateFrom'变量之后。
public int getUpTimeInMinutes(String machineName) {
machineName = "4x2 brick mould";
List<Runtime> runTime = runtimeRepository.findAllByMachineName(machineName);
LocalDateTime reportDateFrom = LocalDateTime.of(2019, 1, 7, 0, 0, 0);
// Method will calculate the difference between the two records when the machine had the isRunning =1 and 0
String previousRecordTime = null;
String actualRecordTime;
int minutesUptime = 0;
for (Runtime runtime : runTime) {
// first i filter from the date requested
if (runtime.getDatetime().isAfter(reportDateFrom)) {
if (runtime.getIsRunning() == 1) {
// below i save the record where machine started working and convert it first to LocalTime and then to String of format HH:mm
previousRecordTime = runtime.getDatetime().toLocalTime().toString();
} else if (runtime.getIsRunning() == 0) {
// below i save the date where machine stopped working and calculate the difference between the two records using another method
actualRecordTime = runtime.getDatetime().toLocalTime().toString();
minutesUptime += calculateDifferenceBetweenStrings(previousRecordTime, actualRecordTime);
}
}
}
return minutesUptime;
}