找出两个时间值之间的差异

时间:2017-02-11 18:22:48

标签: java time

有一个HTML表单,通过它我可以通过以下方式输入两个时间值:

HTML FORM

以Java形式输入的数据正在由Java Servlet处理。我想在java中以分钟为单位找出这两个时间值之间的差异。如果我执行正常减法,则表示 0.7 。但我希望答案是 30分钟。我怎么能完成它?

1 个答案:

答案 0 :(得分:0)

如果您使用的是Java8,可以使用LocalTime,如下所示:

import java.time.LocalTime;
import static java.time.temporal.ChronoUnit.MINUTES;

public class Main {
  public static void main(String[] args) {
    LocalTime actualStart = LocalTime.parse("11:45"); //Change this to get the to Actual Start Time from the HTML form
    LocalTime actualStop = LocalTime.parse("12:15"); //Change this to get the to Actual Stop Time from the HTML form
    System.out.println("The difference is: " + MINUTES.between(actualStart, actualStop) + " minutes");
  }
}

<强>输出:

The difference is: 30 minutes

试试here!

相关问题