将双秒转换为分钟和几秒钟来计算步行/跑步速度 [英] Convert double to minutes and seconds to work out walking/running pace

查看:128
本文介绍了将双秒转换为分钟和几秒钟来计算步行/跑步速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用计时器来计算步行/跑步的速度,以获取经过的秒数,并使用距离计算器来计算距离,并使用速度=时间/距离的公式。使用下面的代码,它将每分钟的步速转换为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, 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?

    这篇关于将双秒转换为分钟和几秒钟来计算步行/跑步速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆