Calculate the difference b/w two given times in java
<p>I am trying to calculate the difference between given two times and i find some problem in it.
Suppose we have times value are "11:AM" and 10:00 AM".
I am able to calculate but there is a bit of confusion in AM ,PM.
Thanks in advance</p>

<p>I am using following code:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;</p>

<p>public class StringToDate {</p>

<pre><code>public static String timeDiff(String time1, String time2) {
    String Time = null;
    int difference;
     String a1 = time1.substring(6);
     String a2 = time2.substring(6);

    try {
        // Define a date format the same as the expected input
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm a");
        // Get time values of the date objects
        long l1 = sdf.parse(time1).getTime();
        long l2 = sdf.parse(time2).getTime();
        difference = (int) (l2 - l1);
        difference = (difference &lt; 0 ? -difference : difference) / 60000;
        if (difference &gt; 60) {
            int Hour = difference / 60;
            int Mins = difference % 60;
            if (Mins == 0)
                Time = Hour + " Hours";
            else
                Time = Hour + " Hours" + " " + Mins + " Mins";
        } else {
            Time = difference + " " + "Mins";
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return Time;
}

public static void main(String[] args) {
    String diff = StringToDate.timeDiff("10:00 AM", "01:00 PM");
    System.out.println("Difference: " + diff);
}
</code></pre>

<p>}</p>
