I want to get the difference between two time values in Java. Below is the code snippet for the same:
DateFormat sdf = new SimpleDateFormat("hh:mm");
Date date1 = sdf.parse("11:45");
Date date2 = sdf.parse("12:00");
System.out.println(""+(date2.getTime()-date1.getTime())/60000);
date1 = sdf.parse("12:45");
date2 = sdf.parse("13:00");
System.out.println(""+(date2.getTime()-date1.getTime())/60000);
date1 = sdf.parse("13:00");
date2 = sdf.parse("13:15");
System.out.println(""+(date2.getTime()-date1.getTime())/60000);
Output in all the three cases respectively: Output1:-705 Output2:735 Output3:15
I don't understand why it is taking wrong value as it should be 15 in all the three cases.