1

I'm trying to calculate the future time when a user inputs hours and minutes separated by a space. I'm stuck on when the user inputs minutes that calculates the future minutes to be less than 10.

For example, if the current time now is 10:04 and the user inputs 1 hour, 4 minutes, the future time SHOULD be 11:08.

Instead, I get printed 11:8

int extraHours = minutesGiven/60;
hoursGiven = hoursGiven+extraHours;
// System.out.println("hoursGiven= " + hoursGiven);
int extraMinutes = minutesGiven%60;

if((minutesNow + extraMinutes)>59){
    minutesNow = minutesNow + extraMinutes-60;
}else if((minutesNow+hoursGiven)<10){
    minutesNow = minutesNow + extraMinutes;
    String padded = String.format("%02d" , minutesNow);
}else{
    minutesNow = minutesNow + extraMinutes;
}

//calculate the future hour
int futureHours = 0;
if(minutesGiven==30){
    futureHours = (hourNow + hoursGiven +1)%24;
}else{
    futureHours = (hourNow + hoursGiven)%24;
}
//print the time
System.out.println("The time will be " + futureHours + ":" + minutesNow);

// LocalTime noon = LocalTime.of(12, 0); //12 hours, 0 minutes
//System.out.println("Noon printed as a LocalTime object: " + noon);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
MusicGirl
  • 123
  • 1
  • 2
  • 8
  • As an aside, you had commented the line with `LocalTime.of(12, 0)` out. You may just use `LocalTime.NOON` to obtain the same. – Ole V.V. Jan 21 '18 at 16:44

4 Answers4

2

Create an if statement when you print the time like this:

if (minutesNow < 10)
    System.out.println("The time will be " + futureHours + ":0" + minutesNow);
else
    System.out.println("The time will be " + futureHours + ":" + minutesNow);

Hope it helped ;)

1

You can use java8's LocalTime class.

Scanner s = new Scanner(System.in);
System.out.print("Enter hour and minute: ");
int hour = s.nextInt();
int minute = s.nextInt();

LocalTime time = LocalTime.now();
time = time.plusHours(hour).plusMinutes(minute);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm");
System.out.println("\n" + "\n" + formatter.format(time));
s.close();
  • Very good suggestion. You don’t need a formatter at all, `LocalTime.toString()` will produce the desired format. If you do use a formatter, use uppercase `HH` in the format pattern string for hours on a 24 hours clock (lowercase `hh` is for hour within AM or PM, from 1 through 12). – Ole V.V. Jan 21 '18 at 16:23
0

You can add a 0 to the integer like this

String time = String.ValueOf(hours) +":"+ "0"+String.ValueOf(minutes);
Rahulrr2602
  • 701
  • 1
  • 13
  • 34
0

You can achive this with Date and Calendar objects. They will automatically include padding for you and respect any possible daylight savings adjustments:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

class Main {    
    public static void main(String[] args) {
        int minutesNow = 4;
        int hourNow = 10;
        int minutesGiven = 4;
        int hoursGiven = 1;
        try {
            String myTime = hourNow + ":" + minutesNow;
            SimpleDateFormat df = new SimpleDateFormat("HH:mm");
            Date d = df.parse(myTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(d);
            cal.add(Calendar.MINUTE, minutesGiven);
            cal.add(Calendar.HOUR, hoursGiven);
            String newTime = df.format(cal.getTime());
            System.out.println("The time will be " + newTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • While using a library class is a good idea, please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 21 '18 at 16:25