将双秒转换为分钟和几秒钟来计算步行/跑步速度 [英] Convert double to minutes and seconds to work out walking/running pace
问题描述
我正在使用计时器来计算步行/跑步的速度,以获取经过的秒数,并使用距离计算器来计算距离,并使用速度=时间/距离的公式。使用下面的代码,它将每分钟的步速转换为2个小数位,每分钟最多.99。一切正常,但是我如何才能在几分钟和几秒钟内进行转换。谢谢
public void calPace(){
double speed = 0;
progressTextView =(TextView)findViewById(R.id.paceTextView);
if(chronometerOn){
int经过的时间Millis =(int)(SystemClock.elapsedRealtime()-chronometer.getBase());
double activityTimeMins =(double)经过的英里/ 1000/60;
//仅在0.1英里后显示以加快速度
if(totalDistance> 0.1){
speed = activityTimeMins / totalDistance;
progressTextView.setText( Pace: + String.format(%。2f,步速)+ /英里);
}
tl; dr
持续时间
.between(
start,
Instant.now()
)
.dividedBy(laps)
.toMinutes()//渲染每分钟分钟数。
//和.toSecondsPart()(或Java 8和Android早期的Android中的.toSeconds()%60)
< h1> java.time
请勿自行计算日期时间数学。我们有针对该类的类。
捕获当前时间(以UTC表示),分辨率最高可达纳秒,但可能实时捕获的时间为毫秒或微秒。
立即开始= Instant.now();
再次捕获当前时刻,最后。
即时结束= Instant.now();
计算经过的时间。
持续时间持续时间= Duration.between(start,end);
询问其各个部分的持续时间:
- 在Java 9+中,使用
toMinutes
方法获取总分钟数,并使用toSecondsPart
方法获取秒数: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"); }
解决方案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, andtoSecondsPart
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
andDuration.toSeconds() % 60
.
Duration.dividedBy
For your division (
pace = activityTimeMins/totalDistance
) we can useDuration.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
. TheP
marks the beginning. TheT
separates the years-month-days from the hours-minutes-seconds. SoPT30S
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?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 brought some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android (26+) bundle implementations of the java.time classes.
- For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
- If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….
这篇关于将双秒转换为分钟和几秒钟来计算步行/跑步速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- In Java 9+, use the