So my assignment is to write a program that prompts the user for two different times on the same day (with 0 being midnight, 0700 being 7:00 am and 2200 being 10 pm) and then returns the difference between the two times with the amount of hours and amount of minutes elapsed. For example, if the user inputted 1255 and 1305, the program should return 0 hours and 10 minutes. I'm fine with how to find the number of hours but I cannot come up with a formula for finding the difference in minutes. Here is what I have so far:

    Scanner keyboard1 = new Scanner(System.in);                 
    System.out.print("Train A departs at: ");
    int a = keyboard1.nextInt();

    Scanner keyboard2 = new Scanner(System.in);                 
    System.out.print("Train B departs at: ");
    int b = keyboard2.nextInt();

    int difference = Math.abs(a - b);
    int hours = difference / 100;
    int minutes = (I need help here);

    System.out.print("\nDifference: " + hours + " hours and " + minutes + " minutes");
  • 2
    $\begingroup$ Note that, outside the context of this programming exercise, you should never attempt to write this kind of code yourself. Date and time manipulations are notoriously difficult because there are a billion and one special cases. For example, daylight saving time adds and subtracts an hour once a year; leap years mean there's an extra day in February. Different countries adopted the Gregorian calendar at different times so, e.g., 1900 was a leap year in Russia but not in England. And leap seconds. Don't even ask about leap seconds. $\endgroup$ Jun 2, 2015 at 9:29
  • $\begingroup$ Programming exercises are offtopic here. It may be ontopic on Code Review, but check their scope carefully before reposting. $\endgroup$
    – Raphael
    Jun 2, 2015 at 10:33

1 Answer 1


The solution I originally provided here is wrong, the comments on why the question itself is out of scope are interesting though.

int hours = Math.abs(a - b)/ 100;
int minutes1 = Math.min(a,b) % 100; int minutes2 = Math.max(a,b) % 100; int mdifference = ((Math.min(a,b) / 100) == (Math.max(a,b) / 100)) ? minutes2 - minutes1 : (60 - minutes1) + minutes2; System.out.print("\nDifference: " + hours + " hours and " + mdifference + " minutes");

  • $\begingroup$ That code doesn't work for all cases and I would like to limit the minutes calculations to one or two formulas. $\endgroup$
    – user18174
    Jun 2, 2015 at 0:32
  • $\begingroup$ I'm presuming to the mistake I made regarding the case where the trains leave at the different hours but within sixty minutes. $\endgroup$ Jun 2, 2015 at 2:09
  • $\begingroup$ Anyway, I dont understand what you mean with 'limit the minutes calculations to one or two formulas', can you be more precise about what it is you want actually? You can write the all the calculations on one line in the print statement if you want but that doesn't make it simpler. $\endgroup$ Jun 2, 2015 at 2:16
  • $\begingroup$ A naked block of code is never an appropriate answer on CS.SE. First, we're not a coding site; second, even if we were, answers should contain an explanation of why they're correct. Except this answer isn't correct: the time between 1300 and 1400 is not "1 hours and 60 minutes". $\endgroup$ Jun 2, 2015 at 9:44
  • 1
    $\begingroup$ I agree that the presence of the word "Java" is largely irrelevant. But this question is only algorithmic in the sense that any program implements some algorithm. This is a request to write a program to solve a problem that nobody would ever use a computer to solve, which indicates that the writing of the program is the only goal of the exercise. Further, since the problem itself is defined to have only a finite number of instances, no algorithmic notions such as efficiency come into play. $\endgroup$ Jun 3, 2015 at 0:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.