格式化秒和分钟 [英] Formatting seconds and minutes

查看:147
本文介绍了格式化秒和分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从毫秒格式化秒和分钟。我正在使用countdownTimer。有没有人有sugestions?我看着乔达的时间。但所有我需要的是格式,所以我有1:05而不是1:5。谢谢

  private void walk(){
New CountDownTimer(15000,1000){
@Override
public void onFinish(){
lapCounter ++;
lapNumber.setText(Lap Number:+ lapCounter);
run();

$ b @Override
public void onTick(long millisUntilFinished){
text.setText(剩余时间:+ millisUntilFinished / 1000);
}
} .start();


解决方案

您可以使用标准的日期格式化类,但这可能有点重量级。我只是使用String.format方法。例如:

  int minutes = time /(60 * 1000); 
int seconds =(time / 1000)%60;
String str = String.format(%d:%02d,分钟,秒);


I need to format seconds and minutes from milliseconds. I am using countdownTimer. does anyone have sugestions? I looked at joda time. But all i need is a format so i have 1:05 and not 1:5. thanks

private void walk() {
    new CountDownTimer(15000, 1000) {
        @Override
        public void onFinish() {
            lapCounter++;
            lapNumber.setText("Lap Number: " + lapCounter);
            run();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            text.setText("Time left:" + millisUntilFinished/1000);
        }
    }.start();
}

解决方案

You could do it using the standard Date formatting classes, but that might be a bit heavy-weight. I would just use the String.format method. For example:

int minutes = time / (60 * 1000);
int seconds = (time / 1000) % 60;
String str = String.format("%d:%02d", minutes, seconds);

这篇关于格式化秒和分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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