Compare user defined time and current time Java

前端 未结 3 1188
甜味超标
甜味超标 2021-01-26 23:25

I am trying to compare user defined time in format HH:MM with current time in an infinite loop. When they are equal, some action should occur.

I have used following code

3条回答
  •  独厮守ぢ
    2021-01-27 00:00

    This is OPs(*) fixed code following Tobías's instructions:

    //get user time and current time
    String time = jFormattedTextField1.getText();
    LocalTime userTime1 = LocalTime.parse(time);
    LocalTime timeNow = LocalTime.now().truncatedTo(ChronoUnit.SECONDS);
    
    //calculate the diff in seconds
    int timeDur = (int)Duration.between(timeNow, userTime1).getSeconds();
    //start the timer object using this difference
    Countdown countdown = new Countdown (timeDur);
    

    (*) Extracted from the edited question, because it doesn't belong there (see this meta discussion)

提交回复
热议问题