-4

There is an HTML form through which I enter the two time values, in the following manner:

HTML FORM

The data entered in HTML form is being processed by a Java Servlet. I want to find the difference between these two time values in terms of minutes in java. If I perform normal subtraction, it says 0.7. But I want the answer to be 30 minutes. How can I accomplish it?

denvercoder9
  • 2,979
  • 3
  • 28
  • 41
Aswin Ram
  • 35
  • 5
  • You have to provide some code so that people may help you with a problem that you tried to first solve yourself. Also, this question surely has been asked dozens of times before, just look it up before asking here. – halileohalilei Feb 11 '17 at 18:26
  • 1
    Possible duplicate of [Calculate date/time difference in java](http://stackoverflow.com/questions/5351483/calculate-date-time-difference-in-java) – halileohalilei Feb 11 '17 at 18:27
  • The same way you subtract a meter from a kilometer. Unit conversion. – Nick Ziebert Feb 11 '17 at 18:34

1 Answers1

0

If you are using Java8 you could use LocalTime like so:

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

Output:

The difference is: 30 minutes

Try it here!

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40