Android Java : How to subtract two times?

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

  • augmented reality framework
  • Android Action Bar with Search View
  • Scene transition with hero elements throws Layer exceeds max. dimensions supported by the GPU
  • Android mobile user agent?
  • Different textsizes in a button
  • How to implement onBackPressed() in Fragments?
  • I found some examples but they all are very confusing .

    Is there any simple solution ?

    Related posts:

    Action Bar's onClick listener for the Home button
    Get clicked item and its position in RecyclerView
    image rotation with opencv in android cuts off the edges of an image
    Handle screen rotation without losing data - Android
    Access sdk.dir value in build.gradle after project evaluation
    Add an attribute at the “application” tag in the AndroidManifest from a cordova ...
  • Android ViewPager - Show preview of page on left and right
  • Changing where cursor starts in an expanded EditText
  • Changing the width of AutoCompleteTextView in Android
  • Android Realm copyToRealmOrUpdate creates duplicates of nested objects
  • “Native typeface cannot be made” only for some people
  • Android, Checkbox listener in XML?
  • 2 Solutions collect form web for “Android Java : How to subtract two times?”

    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.

    Assuming you are using java.util.Date:

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

    The result will be the total time in milliseconds.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.