I am trying to get time difference between two time values. (11:00:00 and 12:43:00) for above inputs it is showing correct output.
**
Time in seconds: 6180 seconds.
Time in minutes: 103 minutes.
Time in hours: 1 hours.
Time in hours Round : 2 hours.
** For Input values("11:00:00"and "2:43:00") It should give us 3 hours as round value **
Time in seconds: -29820 seconds.
Time in minutes: -497 minutes.
Time in hours: -8 hours.
Time in hours Round : -8 hours.
**
import java.lang.Math;
import java.text.SimpleDateFormat;
import java.util.Date;
public class HelloWorld
{
public static void main(String[] args)
{
String dateStart = "11:00:00";
String dateStop = "12:43:00";
// Custom date format
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
} catch (Exception e) {
e.printStackTrace();
}
// Get msec from each, and subtract.
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000;
long diffMinutes = diff / (60 * 1000);
long diffHours = diff / (60 * 60 * 1000);
int parkingEntry=2;
int firstHour=3;
int parkingRate=4;
int Total=0;
System.out.println("Time in seconds: " + diffSeconds + " seconds.");
System.out.println("Time in minutes: " + diffMinutes + " minutes.");
System.out.println("Time in hours: " + diffHours + " hours.");
double my_hrs=diffMinutes/60d;
int r = (int) Math.round(my_hrs);
System.out.println("Time in hours Round : " + r + " hours.");
}
}