两个LocalDateTime变量arent比较正确吗?

时间:2019-06-17 09:33:03

标签: java spring hibernate

我有一个方法可以从与hibernate连接的SQL数据库中获取记录(称为runtime)。当我想按日期过滤结果时,问题就来了。我用要查看结果的日期声明了变量,但从未通过过'if'语句。在调试器中,我看到要比较的这两个日期具有相同的格式,我不知道为什么它不起作用。有什么想法吗?

我在屏幕上附加了变量的调试器状态,问题出在我检查-> runtime.getDateTime是否在'reportDateFrom'变量之后。

调试器变量状态-> https://scontent-waw1-1.xx.fbcdn.net/v/t1.15752-9/64350066_418943105623455_284358622048157696_n.png?_nc_cat=110&_nc_ht=scontent-waw1-1.xx&oh=a2ff24ad936bc2843658afe9dc130128&oe=5D881E1E

方法-> https://scontent-waw1-1.xx.fbcdn.net/v/t1.15752-9/64673116_2354086638248857_6383211241770319872_n.png?_nc_cat=111&_nc_ht=scontent-waw1-1.xx&oh=7207d510996b24f54235adcc76de7100&oe=5D7B4FE6

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

0 个答案:

没有答案