Reputation: 47
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
Reputation: 339917
Duration
.between(
start ,
Instant.now()
)
.dividedBy( laps )
.toMinutes() // Renders minutes-per-lap.
// and .toSecondsPart() (or .toSeconds()%60 in Java 8 & early Android)
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:
toMinutes
method for total minutes, and toSecondsPart
method for seconds: Duration::toMinutes
& Duration::toSecondsPart
.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
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.
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"
;
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?
Upvotes: 2