-2

I'm stuck in a Duration calculation problem given in my College Assignment. So, the user get to enter a Starting time and an Ending time, where hours and minutes are entered separately to the program. Hours are entered in 24h format(00 - 23). My program successfully calculate durations where 'Ending time' is greater than 'Starting time', with a simple 'Ending time - Starting time'. Eg: 23.55 - 22.55 = 1h. But my problem appears when something like this happens, 'Starting time = 23.45' and 'Ending time = 00.45'. So the earlier method wont work for this.

So I need a little help with this, since I'm still learning the basics of Java and this is a College assignment I cant do anything outside the lessons(Complex stuff). So I'd prefer to stay in the basics(BODMAS) and do it. Please ask if more information is needed!

Thanks in Advance!

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    What have you done till now? Add existing code in your question. – Rohan Kushwaha May 14 '16 at 21:40
  • Take a look [here](http://meta.stackexchange.com/a/10812) for the StackOverflow policy on homework. You really need to show some effort, and some existing code, especially if you're asking for homework help. – amiller27 May 14 '16 at 21:55

2 Answers2

0

This works:

if (start_time > end_time) end_time += 24 hours
duration = end_time - start_time

Or more succinctly

duration = end_time + (end_time < startTime ? 24 : 0) - start_time

The changes necessary to distinguish whether end_time == start_time means zero or 24 hours are left as an exercise.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • OMG, This is so simple, why didnt I thought of this! I've been trying with 10 lines of Algorithm still doesnt work, and this is only 1 line! LoL. Thanks man! – Dunura Dulshan May 15 '16 at 10:04
0

java.time

I recommend you use the java.time API to do it. The steps can be summarized in the following points:

  1. Create the instances of LocalDateTime using the current date at start time and the end time.
  2. If the LocalDateTime instance for end time is before the one for the start time, reset the instance for the end time to the midnight, then add one day to it and finally apply the time to it.
  3. Use java.time.Duration to get the duration between the instances of the LocalDateTime.

Demo:

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the start hour: ");
        int startHour = scanner.nextInt();
        System.out.print("Enter the start minute: ");
        int startMinute = scanner.nextInt();
        System.out.print("Enter the start hour: ");
        int endHour = scanner.nextInt();
        System.out.print("Enter the start minute: ");
        int endMinute = scanner.nextInt();

        LocalDate date = LocalDate.now();

        LocalDateTime startDateTime = date.atTime(LocalTime.of(startHour, startMinute));
        LocalDateTime endDateTime = date.atTime(LocalTime.of(endHour, endMinute));

        if (startDateTime.isAfter(endDateTime)) {
            endDateTime = endDateTime.with(LocalTime.MIN).plusDays(1).with(LocalTime.of(endHour, endMinute));
        }

        Duration duration = Duration.between(startDateTime, endDateTime);

        System.out.println(duration);

        // Custom format
        // ####################################Java-8####################################
        System.out.println(
                "Total duration: " + String.format("%02d:%02d", duration.toHours(), duration.toMinutes() % 60));
        // ##############################################################################

        // ####################################Java-9####################################
        System.out.println(
                "Total duration: " + String.format("%02d:%02d", duration.toHoursPart(), duration.toMinutesPart()));
        // ##############################################################################
    }
}

A sample run:

Enter the start hour: 23
Enter the start minute: 45
Enter the start hour: 0
Enter the start minute: 45
PT1H
Total duration: 01:00
Total duration: 01:00

java.time.Duration is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation. With Java-9 some more convenience methods were introduced.

Learn more about java.time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110