Home > Back-end >  Is there a way to do 24-hour format and 60-minute format in Java?
Is there a way to do 24-hour format and 60-minute format in Java?

Time:12-02

if(ExcessTimeHours >= 24) {
    ExcessTimeHours = 0;
}
if(ExcessTimeMin >= 60) {
    ExcessTimeMin = 0;
    ExcessTimeHours   ;
}

I add time when the first movie starts, then I add how long is that movie and then I add when the second movie starts, also add how long this movie is and the program will tell me if I am able to see both movies with no problem, or if I will miss some minutes of the movie or if I am not able to make it.

My problem is that I don't know how to fix that when the time reaches over midnight and jumps from 24 to 0 hours. Can't figure out the condition.

    System.out.println("Start of movie A:");
    
    System.out.print("hour: ");                         // 17; 23; 0
    int startHourA = s.nextInt();
    System.out.print("min: ");                          // 30; 30; 30
    int startMinA = s.nextInt();
    
    System.out.println("Length of movie A:");
    
    System.out.print("hour: ");                         // 2; 2; 2
    int lengthHourA = s.nextInt();
    System.out.print("min: ");                          // 0; 0; 1
    int lengthMinA = s.nextInt();
    
    
    System.out.println("Start of movie B:");
    
    System.out.print("hour: ");                         // 20; 0; 0
    int startHourB = s.nextInt();
    System.out.print("min: ");                          // 0; 20; 31
    int startMinB = s.nextInt();
    
    System.out.println("Length of movie B:");
    
    System.out.print("hour: ");                         // 0; 1; 2
    int lengthHourB = s.nextInt();
    System.out.print("min: ");                          // 35; 35; 0
    int lengthMinB = s.nextInt();


    int timeBetweenMoviesHour = startHourB - ExcessTimeHours;
    int timeBetweenMoviesMin = startMinB - ExcessTimeMin;

    if (timeBetweenMoviesHour == 0 && timeBetweenMoviesMin >= 0) {
        System.out.println("Recommendation: No problem");
    }
    else if (timeBetweenMoviesMin > 0) {
        System.out.println("Recommendation: You won't see "   
        timeBetweenMoviesMin   " minutes");
    }
    else System.out.println("Recommendation: You can't make it");   

CodePudding user response:

java.time

You will want to use the LocalTime and Duration classes. For example like the following. I don’t think my code does exactly what you want your code to do, but it should get you started.

    // Convert inputs to LocalTime and Duration objects

    LocalTime startMovieA = LocalTime.of(16, 0);
    Duration lengthMoviaA = Duration.ofHours(2).plusMinutes(15);

    LocalTime startMovieB = LocalTime.of(18, 45);

    // Do calculations

    LocalTime endMovieA = startMovieA.plus(lengthMoviaA);
    Duration timeBetweenMovies = Duration.between(endMovieA, startMovieB);

    // Output

    if (timeBetweenMovies.isNegative()) {
        System.out.println("You will be missing "   timeBetweenMovies.abs());
    }
    else if (timeBetweenMovies.isZero()) {
        System.out.println("You will just make it");
    }
    else {
        System.out.println("You will have "   timeBetweenMovies   " between the movies");
    }

Output is:

You will have PT30M between the movies

Duration objects print a little funny. The format is ISO 8601. For your user you will want to format more nicely, like You will have 30 minutes between the movies. Search for how and/or use the links at the bottom. It’s described in various places on the WWW.

Links

  • Related