The problem is with the following line:
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss aa");
It should be:
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
The symbol, H
is used for a time in 24-hour format while h
is used for that in 12-hour format.
Your calculation of diffMinutes
is also wrong.
Do it as follows:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter start time (hh:mm:ss aa): ");
String starttime = input.nextLine();
System.out.print("Enter end time (hh:mm:ss aa): ");
String endtime = input.nextLine();
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(starttime);
d2 = format.parse(endtime);
// in milliseconds
long diff = Math.abs(d2.getTime() - d1.getTime());
long diffSeconds = (diff / 1000) % 60;
long diffMinutes = (diff / (60 * 1000));
System.out.print(diffMinutes + " minutes and " + diffSeconds + " seconds.");
} catch (Exception e) {
System.out.println("Invalid fromat");
}
}
}
A sample run:
Enter start time (hh:mm:ss aa): 10:20:30 am
Enter end time (hh:mm:ss aa): 10:20:13 pm
719 minutes and 43 seconds.
Notes:
- As @greg-449 has mentioned, you should strive to use modern date-time API.
- The difference between two quantities is an absolute value which is always positive i.e. the difference between 2 and 5 = difference between 5 and 2 = 3.
Math.abs
gives you the absolute value of a number which suites best for finding the difference between two quantities.
- You need to understand that without telling about the date, the difference between two times is always considered for the same date i.e. the difference between 12:02:15 am and 11:58:10 pm = difference between 11:58:10 pm and 12:02:15 am = 1435 minutes and 55 seconds. The difference between 11:58:10 pm on one date and 12:02:15 am on the next date is 4 minutes 5 seconds. However, your input is only for times and does not have date element and therefore the difference has been considered for the same date. Given below is the program to consider a date with time.
Program considering a date with time:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter start time (dd.MM.yyyy at hh:mm:ss aa): ");
String starttime = input.nextLine();
System.out.print("Enter end time (dd.MM.yyyy at hh:mm:ss aa): ");
String endtime = input.nextLine();
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy 'at' hh:mm:ss aa");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(starttime);
d2 = format.parse(endtime);
// in milliseconds
long diff = Math.abs(d2.getTime() - d1.getTime());
long diffSeconds = (diff / 1000) % 60;
long diffMinutes = (diff / (60 * 1000));
System.out.print(diffMinutes + " minutes and " + diffSeconds + " seconds.");
} catch (Exception e) {
System.out.println("Invalid fromat");
}
}
}
A sample run:
Enter start time (dd.MM.yyyy at hh:mm:ss aa): 03.03.2020 at 11:58:10 pm
Enter end time (dd.MM.yyyy at hh:mm:ss aa): 04.03.2020 at 12:02:15 am
4 minutes and 5 seconds.