Advertisement
dimipan80

JavaBasics Exam 1. Timespan

Sep 27th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class _1_Timespan {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         String inputStart = scan.nextLine().trim();
  8.         String inputEnd = scan.nextLine().trim();
  9.  
  10.         int[] startTimes = readInputLineAndConvertStringToIntegerNums(inputStart);
  11.         int[] endTimes = readInputLineAndConvertStringToIntegerNums(inputEnd);
  12.  
  13.         int outSec = startTimes[2] - endTimes[2];
  14.         if (outSec < 0) {
  15.             startTimes[1]--;
  16.             outSec += 60;
  17.         }
  18.  
  19.         int outMin = startTimes[1] - endTimes[1];
  20.         if (outMin < 0) {
  21.             startTimes[0]--;
  22.             outMin += 60;
  23.         }
  24.  
  25.         int outHour = startTimes[0] - endTimes[0];
  26.  
  27.         System.out.printf("%d:%02d:%02d%n", outHour, outMin, outSec);
  28.     }
  29.  
  30.     private static int[] readInputLineAndConvertStringToIntegerNums(
  31.             String inputLine) {
  32.         String[] inputs = inputLine.split(":");
  33.         int[] nums = new int[inputs.length];
  34.         for (int i = 0; i < nums.length; i++) {
  35.             nums[i] = Integer.parseInt(inputs[i]);
  36.         }
  37.  
  38.         return nums;
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement