Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TimeBetween{
- public static void main(String[]args){
- //Input
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter first time: ");
- String first = scan.next();
- System.out.print("Enter second time: ");
- String second = scan.next();
- int firsthour = 0;
- int firstminute = 0;
- int secondhour = 0;
- int secondminute = 0;
- //Substrings
- //First Hour Tester
- if(first.substring(0,1).equals("0")){
- firsthour = Integer.parseInt(first.substring(1,2));
- }
- else{
- firsthour = Integer.parseInt(first.substring(0,2));
- }
- //First Minute Tester
- if(first.substring(2).equals("00")){
- firstminute = 0;
- }
- else{
- firstminute = Integer.parseInt(first.substring(2));
- }
- //Second Hour Tester
- if(second.substring(0,1).equals("0")){
- secondhour = Integer.parseInt(second.substring(1,2));
- }
- else{
- secondhour = Integer.parseInt(second.substring(0,2));
- }
- //Second Minute Tester
- if(first.substring(2,3).equals("00")){
- secondminute = 0;
- }
- else{
- secondminute = Integer.parseInt(second.substring(2));
- }
- //Calculation
- int hours = secondhour-firsthour;
- int minutes = secondminute-firstminute;
- //Output
- System.out.println(hours+" Hours, "+minutes+" minutes");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement