LocalDateTime变量停止处于活动状态

时间:2019-04-06 23:09:04

标签: java date format formatter

当我第一次运行代码(如下)时,它显示了实时时间(忽略“ 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” ..)

1 个答案:

答案 0 :(得分:1)

正如@SotiriosDelimanolis在评论中指出的那样,此查询的答案是formart“ HH:MM”不正确。我应该用“ HH:mm”代替。