13

I use some kind of stopwatch in my project and I have

start time ex: 18:40:10 h
stop time  ex: 19:05:15 h

I need a result from those two values like final time = stop - start

I found some examples but they all are very confusing .

Is there any simple solution ?

Peter Lang
  • 54,264
  • 27
  • 148
  • 161
Nezir
  • 6,727
  • 12
  • 54
  • 78

5 Answers5

32

If you have strings you need to parse them into a java.util.Date using java.text.SimpleDateFormat. Something like:

        java.text.DateFormat df = new java.text.SimpleDateFormat("hh:mm:ss");
        java.util.Date date1 = df.parse("18:40:10");
        java.util.Date date2 = df.parse("19:05:15");
        long diff = date2.getTime() - date1.getTime();

Here diff is the number of milliseconds elapsed between 18:40:10 and 19:05:15.

EDIT 1:

Found a method online for this (at http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html?page=2):

  int timeInSeconds = diff / 1000;
  int hours, minutes, seconds;
  hours = timeInSeconds / 3600;
  timeInSeconds = timeInSeconds - (hours * 3600);
  minutes = timeInSeconds / 60;
  timeInSeconds = timeInSeconds - (minutes * 60);
  seconds = timeInSeconds;

EDIT 2:

If you want it as a string (this is a sloppy way, but it works):

String diffTime = (hours<10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds) + " h";

EDIT 3:

If you want the milliseconds just do this

long timeMS = diff % 1000;

You can then divide that by 1000 to get the fractional part of your seconds.

John
  • 2,012
  • 2
  • 21
  • 33
  • Aham i saw samples like that but again I need result as 01:10.34 h , here I get result in millisecond and need more mathematics to get wished result , is there simplest solution ? – Nezir Aug 18 '10 at 17:26
  • How are you going to get milliseconds in your result if your time is only recording hours, minutes and seconds? – John Aug 18 '10 at 17:36
  • quadelirus Thanks a lot I get what I want :) I use : start= System.currentTimeMillis(); but I do some DateFormating to get only hours :) Now I get what I need , thank's again .. – Nezir Aug 18 '10 at 18:34
  • I was done some string formating , but as I tested those line it is better solution :) Thanks one more time quadelirus. – Nezir Aug 20 '10 at 12:17
13

Assuming you are using java.util.Date:

long totalTime = endDate.getTime() - startDate.getTime();

The result will be the total time in milliseconds.

Thierry Roy
  • 8,452
  • 10
  • 60
  • 84
1

I am providing the modern answer.

java.time and ThreeTenABP

    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("H:mm:ss 'h'");

    String startTimeString = "18:40:10 h";
    String stopTimeString = "19:05:15 h";

    LocalTime startTime = LocalTime.parse(startTimeString, timeFormatter);
    LocalTime stopTime = LocalTime.parse(stopTimeString, timeFormatter);

    if (stopTime.isBefore(startTime)) {
        System.out.println("Stop time must not be before start time");
    } else {
        Duration difference = Duration.between(startTime, stopTime);

        long hours = difference.toHours();
        difference = difference.minusHours(hours);
        long minutes = difference.toMinutes();
        difference = difference.minusMinutes(minutes);
        long seconds = difference.getSeconds();

        System.out.format("%d hours %d minutes %d seconds%n", hours, minutes, seconds);
    }

Output from this example is:

0 hours 25 minutes 5 seconds

The other answers were good answers in 2010. Today avoid the classes DateFormat, SimpleDateFormat and Date. java.time, the modern Java date and time API, is so much nicer to work with.

But doesn’t it require API level 26?

No, using java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Try this answer it will help you a lot

from here

In this answer, you will find subtract to two times discarding the day and month and year.

it gives you the number of minutes you will spend until you reach the second Time value.

Momen Ali
  • 21
  • 6
0

Today, with newer Java (no idea, at what Android version this works):

Instant before = Instant.now();
// do stuff

Duration.between(before, Instant.now()).getSeconds()

The clumsy java ways from olden days are now gone.

Frischling
  • 2,100
  • 14
  • 34