0

Im struggling on the project I set myself. I want the user to be able to input the time, but it needs to be legal (i.e if the time is 9:15 and they add 4 hours, it must be 01:15 Please help!

package time;

public class NewTime {

    int hh, mm;

    public NewTime(int hh, int mm) {
        if (hh > 0 && hh < 24 && mm > 0 && mm < 60) {

            this.hh = hh;
            this.mm = mm;

        }
    }

    public void addTime(int hh, int mm) {
        if (mm + this.mm > 59) {
            this.hh += mm / 60;
        }

        this.hh += hh;
        this.mm += mm;
    }
}
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Savant
  • 31
  • 1
  • 1
  • 3
  • Can't you use any java APIs for this? – Codebender Jul 13 '15 at 13:37
  • 1
    Also, what's the problem you are facing? I only see one problem. **You are not reducing the minutes after you divide by 60 and add to hour**. Is there any other issue? – Codebender Jul 13 '15 at 13:39
  • Take the String as input use the String split() method to break apart the time into hours, minutes, and seconds. Really, it comes down to adding or subtracting from the hours. – Ankur Anand Jul 13 '15 at 13:40
  • @Savant Java offers at least three frameworks for date-time work. You should be using one of those. But if you are avoiding those to use this problem as an exercise, you should edit your Question to say so. – Basil Bourque Jul 13 '15 at 15:14

3 Answers3

1

Your problem lies here:

public void addTime(int hh, int mm) {
    if (mm + this.mm > 59) {
        this.hh += mm / 60;
    }

    this.hh += hh; //<--source of problem
    this.mm += mm;
}

You also need the check after all the addition whether the hh variable is more than 12. an if it is more than 12 deduct 12. So the corrected format would be:

public void addTime(int hh, int mm) {
    this.hh += hh;
    this.mm += mm;
    this.hh += this.mm / 60;
    this.mm = this.mm % 60; //This removes the problem where the mm may be 59 and this.mm is 2 
    this.hh = this.hh % 12; //This solves the problem of hour not getting over 12.
}

Here instead of checking whether the sum of this.mm and mm is greater than 59. We simply add the mm to this.mm and then add the integer division result of this.mm / 60 to hh. Also set the remainder of this integer division to this.mm. We repeat the same thing with hh to store only the remainder of the integer division of this.hh and 12 to give the output in the 12 hour format.

This should take care of your problem.

Blip
  • 3,061
  • 5
  • 22
  • 50
0

I would suggest using the mod operator. When the user adds 4 hours. Use an if statement. If the new hour is greater than 12, then mod the new hour to get the correct time.

i.e.

 9:15 + 4 hours => 13:15
 13 % 12 => 1
 New time = 1:15
Constuntine
  • 498
  • 2
  • 16
0

You might want to use the LocalDateTime API (added in Java 8).

LocalDateTime now = LocalDateTime.now();
LocalDateTime twoHoursFromNow = now.plusHours(2);

Getting user input of time requires parsing:

LocalDateTime inputTime = LocalDateTime.parse("2015-07-13T10:00:00");
LocalDateTime twoHoursFromInputTime = inputTime.plusHours(2);

This question may be a duplicate of Adding n hours to a date in Java?

Community
  • 1
  • 1
frontend_friend
  • 587
  • 6
  • 9