-2

Hey guys i need help to figure out how to calculate the time difference between two certain times (entered by user). But the output has to be split up in hours, minutes and seconds as their own variables.

example:

Startingtime:

Hour: 15 Minute: 50 Second: 00

Finishtime:

Hour: 17 Minute: 14 Second: 23

Result: 1 Hour 24 minutes 23 seconds

Edit: This is what i've got so far, it's the calculation that's my problem

public static void main(String[] args) 
{
    Scanner in = new Scanner(System.in);
    System.out.println("Starttime:");
    System.out.println("---------");

    System.out.print("Hour: ");
    int HourS = in.nextInt();
    System.out.print("Minute: ");
    int minuteS = in.nextInt();
    System.out.print("Second: ");
    int secondS = in.nextInt();


    System.out.println("\t");

    System.out.println("Finishtime:");
    System.out.println("------------");

    System.out.print("Hour: ");
    int hourF = in.nextInt();
    System.out.print("Minute: ");
    int minuteF = in.nextInt();
    System.out.print("Second: ");
    int secondM = in.nextInt();
Pudo
  • 1
  • 2
  • I got stuck on the real calculation of the user-specified times. I have tried doing Hour - Hour and minute - minute and second - second but i quickly realized that wont work with time – Pudo Mar 01 '13 at 13:00
  • You have to remember that things overlap - every time a second becomes negative (ex. 23 - 50), you have to take one from the minute and add 60 to seconds, and apply the same to the minutes (also, start from seconds, not hours). – ddmps Mar 01 '13 at 13:22

4 Answers4

2

Use JodaTime - it does a great job.

This simple example calculates the difference between tow timestamps and prints it human readable:

    String a = "00:01:09:340";
    String b = "00:02:03:430";

    DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm:ss:SSS");
    DateTime oldTime = DateTime.parse(a, format);
    DateTime newTime = DateTime.parse(b, format);
    Interval interval = new Interval(oldTime, newTime);

    System.out.println(new StringBuffer("hours: ").append(interval.toDuration().getStandardHours())
            .append(" minutes: ").append(interval.toDuration().getStandardMinutes()).append(" seconds: ")
            .append(interval.toDuration().getStandardSeconds()).append(" milliseconds: ")
            .append(interval.toPeriod().getMillis()).toString());

The example will give you the following output:

hours: 0 minutes: 0 seconds: 54 milliseconds: 90
elToro
  • 1,003
  • 9
  • 31
1

This is the difference in ms:

SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date date = format.parse(time);
Date dateB = format.parse(timeB);
long difference = dateB.getTime() - date.getTime();

You can convert that to your desired format.

Paul
  • 5,756
  • 6
  • 48
  • 78
1
public static void main (String [] args) throws Exception
{
    SimpleDateFormat format = new SimpleDateFormat ("'Hour: 'HH' Minute: 'mm' Second: 'ss");

    BufferedReader reader = 
        new BufferedReader (
            new InputStreamReader (System.in));

    System.out.print ("Starting time: ");
    long startingTime = format.parse (reader.readLine ()).getTime ();
    System.out.print ("Finish time: ");
    long finishTime = format.parse (reader.readLine ()).getTime ();

    long delta = finishTime - startingTime;

    long seconds = delta / 1000 % 60;
    long minutes = delta / 60000 % 60;
    long hours = delta / 3600000;

    System.out.println (
        "Result: " + hours + " hour(s) " + minutes + 
        " minute(s) " + seconds + " second(s)");
}

For me output is:

Starting time: Hour: 15 Minute: 50 Second: 00
Finish time: Hour: 17 Minute: 14 Second: 23
Result: 1 hour(s) 24 minute(s) 23 second(s)
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
0

You might want to considder using JODA-time, it has support for the more complex date calculation methods.

G-Man
  • 1,321
  • 8
  • 15