Android 如何将服务的持续时间格式化为;HH:mm:ss“;

Android 如何将服务的持续时间格式化为;HH:mm:ss“;,android,service,simpledateformat,duration,elapsedtime,Android,Service,Simpledateformat,Duration,Elapsedtime,我发现这个网站上的代码和这个代码适用于我的案例,但是有没有一种方法可以让我直接获得经过的时间“duration”并用SimpleDataFormat(“HH:mm:ss”)格式化它? 更具体地说。。我想要像这样的东西,而不需要1970年的HH:mm:ss:) ,例如:“00:00:10” SimpleDataFormat sdf=新的SimpleDataFormat(“HH:mm:ss”); 持续时间=结束时间开始时间; durationStr=sdf.format(duration) 您可以尝

我发现这个网站上的代码和这个代码适用于我的案例,但是有没有一种方法可以让我直接获得经过的时间“duration”并用SimpleDataFormat(“HH:mm:ss”)格式化它? 更具体地说。。我想要像这样的东西,而不需要1970年的HH:mm:ss:) ,例如:“00:00:10”

SimpleDataFormat sdf=新的SimpleDataFormat(“HH:mm:ss”); 持续时间=结束时间开始时间; durationStr=sdf.format(duration)

您可以尝试以下方法:

注意:在本例中我没有使用任何服务,因此我使用了
postDelayed
,它在5秒后执行代码。基于此,我正在计算从活动开始到
postdayed

long delayedTime, currTime;
TextView text;
currTime = System.currentTimeMillis();
            new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        delayedTime = System.currentTimeMillis();
                        text.setText("" + getDurationString((int)((delayedTime - currTime) * 0.001)));
                    }
                }, 5000);
--

long delayedTime, currTime;
TextView text;
currTime = System.currentTimeMillis();
            new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        delayedTime = System.currentTimeMillis();
                        text.setText("" + getDurationString((int)((delayedTime - currTime) * 0.001)));
                    }
                }, 5000);
public String getDurationString(int seconds) {
    int hours = seconds / 3600;
    int minutes = (seconds % 3600) / 60;
    seconds = seconds % 60;
    return twoDigitString(hours) + " : " + twoDigitString(minutes) + " : " + twoDigitString(seconds);
                }

private String twoDigitString(int number) {
  if (number == 0) {
     return "00";
  }
  if (number / 10 == 0) {
     return "0" + number;
  }
  return String.valueOf(number);
}