Formatting seconds and minutes

前端 未结 4 1562
孤独总比滥情好
孤独总比滥情好 2020-12-31 07:19

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

相关标签:
4条回答
  • 2020-12-31 08:08

    I used Apache Commons StopWatch class. The default output of it's toString method is ISO8601-like, hours:minutes:seconds.milliseconds.

    Example of Apache StopWatch

    0 讨论(0)
  • 2020-12-31 08:10

    A real lazy way of doing this as long as you know you won't have more than 60 minutes is to just make a date and use SimpleDateFormat

    public void onTick(long millisUntilFinished) {
         SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");
         dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
         Date date = new Date(millisUntilFinished);
         text.setText("Time left:" + dateFormat.format(date));
    }
    
    0 讨论(0)
  • 2020-12-31 08:24

    i'd use

    org.apache.commons.lang.time.DurationFormatUtils.formatDuration(millisUntilFinished, "mm:ss")
    
    0 讨论(0)
  • 2020-12-31 08:25

    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);
    
    0 讨论(0)
提交回复
热议问题