2017-02-11 40 views
-4

有通过该本人进入两个时间值的HTML表单,以下面的方式:查找差值

HTML FORM

在HTML形式输入的数据是由一个Java Servlet的处理。我想在Java中以分钟的形式查找这两个时间值之间的差异。如果我执行正常的减法,它说0.7。但我想要的答案是30分钟。我怎样才能做到这一点?

+0

你必须提供一些代码,以便使人们帮助你解决你试图首先解决问题的问题。而且,这个问题肯定已经被问过几十次了,只是在问这里之前查看它。 – halileohalilei

+1

[计算java中的日期/时间差异]的可能的重复(http://stackoverflow.com/questions/5351483/calculate-date-time-difference-in-java) – halileohalilei

+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!

+0

非常感谢。完美的作品! :) –