hotspuds
hotspuds

Reputation: 47

Convert double to minutes and seconds to work out walking/running pace

I am working out the pace of a walk/run using my chronometer to get the elapsed seconds, and a distance calculator for distance and using the formulae pace = time/distance. Using the below code it converts the pace per minute into 2 decimal places up to .99 each minute. This is working fine but how can I convert this so it's in minutes and seconds. Thanks

public void calPace(){

double pace = 0;
       
paceTextView = (TextView)findViewById(R.id.paceTextView);
      
if(chronometerOn) {
          
  int elapsedMillis = (int) (SystemClock.elapsedRealtime() - chronometer.getBase());
 
           double activityTimeMins = (double) elapsedMillis / 1000 / 60;
  
// only show after 0.1 mile to build pace up
  if(totalDistance>0.1){
             
        pace = activityTimeMins/totalDistance;

   paceTextView.setText("Pace : "+String.format("%.2f", pace)+" / mile");
      
  }

Upvotes: 0

Views: 448

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 339917

tl;dr

Duration
.between(
    start , 
    Instant.now()
)
.dividedBy( laps )
.toMinutes() // Renders minutes-per-lap.
// and .toSecondsPart() (or .toSeconds()%60 in Java 8 & early Android)

java.time

Do not roll-your-own date-time math. We have classes for that.

Capture the current moment as seen in UTC, with a resolution up to nanoseconds, but likely captured live in either milliseconds or microseconds.

Instant start = Instant.now() ;

Capture the moment again, at the end.

Instant end = Instant.now() ;

Calculate elapsed time.

Duration duration = Duration.between( start , end ) ;

Interrogate the duration for its parts:

  • In Java 9+, use the toMinutes method for total minutes, and toSecondsPart method for seconds: Duration::toMinutes & Duration::toSecondsPart.
  • In Java 8 and early Android, we must do the math ourselves for the seconds by using modulo: Duration::toMinutes and Duration.toSeconds() % 60.

Duration.dividedBy

For your division (pace = activityTimeMins/totalDistance) we can use Duration.dividedBy to do the math.

Take example the turtle race where the winner finished the 3 inch distance in 90 seconds. How many seconds per inch?

Duration timePerInch = Duration.ofSeconds( 90 ).dividedBy( 3L ) ;

PT30S

ISO 8601

The output shown there is in standard ISO 8601 format PnYnMnDTnHnMnS. The P marks the beginning. The T separates the years-month-days from the hours-minutes-seconds. So PT30S is a half minute.

  • Always use ISO 8601 formats when exchanging date-time values as text.
  • The formats may or may not be suitable for presentation to your users.

Full example

Pull that all together.

Instant start = Instant.now() ;
…
Instant end = Instant.now() ;
Duration elapsed = Duration.between( start , end ) ;
int distance = … some number of laps, meters, miles, whatever.
Duration timePerDistanceUnit = elapsed.dividedBy( distance ) ; 
String message = 
    "Pace : " + 
    timePerDistanceUnit.toMinutes() + "m" +
    timePerDistanceUnit.toSecondsPart() + "s" +
    " per mile"
;

If your distance (our divisor here) is fractional rather than an integer, you'll need to do a bit of the math yourself.

Duration.ofNanos(
    Double
    .valueOf( 
        elapsed.toNanos() / 1.5   // For 1.5 miles as example distance.
    )  
    .longValue()  
);

All together again.

Instant start = Instant.now() ;
…
Instant end = Instant.now() ;
Duration elapsed = Duration.between( start , end ) ;
double distance = … some number of laps, meters, miles, whatever.
Duration timePerDistanceUnit = 
    Duration.ofNanos(
        Double.valueOf( elapsed.toNanos() / distance ).longValue()
    )
; 
String message = 
    "Pace : " + 
    timePerDistanceUnit.toMinutes() + "m" +
    timePerDistanceUnit.toSecondsPart() + "s" +
    " per mile"
;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Table of which java.time library to use with which version of Java or Android

Upvotes: 2

Related Questions