How do you count the time spent on the mission?



  • I'm writing a little Android app on Java. I have users in one activist tested and, at the end of the test, I have the following activism that passes the final infa by passing the test. So one of the fields is spent time on a mission... I'm trying to count time as a chronometer, but he's in milliseconds thinking and then formatting through different checks at 00:00 (02 : 34) and it's very circumstantial... I'm sure there's a better solution. ♪ ♪

    How can you do that?



    1. At the beginning of the mission, keep the current time long variable:

      long startTime = System.currentTimeMillis();
      
    2. After the end, get the difference:

      long totalTime = System.currentTimeMillis() - startTime;
      
    3. Take class. Calendar and assigns it the following value:

      Calendar cal = Calendar.getInstance();
      cal.setTimeInMillis( totalTime );
      
    4. We get minutes/seconds.

      int minutes = cal.get(Calendar.MINUTE);
      int seconds = cal.get(Calendar.SECOND);
      

    or in a class format SimpleDateFormat

    SimpleDateFormat format = new SimpleDateFormat("mm:ss");
    System.out.println(format.format(cal.getTime()));
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2