Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting seconds and minutes

Tags:

java

time

format

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();
}
like image 293
Chad Bingham Avatar asked Sep 17 '12 23:09

Chad Bingham


People also ask

How do you format minutes and seconds?

Select and right click the cells with time you want to display in minutes and seconds, and then click Format Cells in the right-clicking menu. See screenshot: 2. In the Format Cells dialog box, click Custom in the Category box under Number tab, type [m]:ss into the Type box, and then click the OK button.

How do I format minutes and seconds in Excel?

In the Format Cells dialog box, click the Number tab. Under Category, click Custom. In the Type box, type [h]:mm. TIP You can also show the results in minutes and seconds by setting the format to [m]:ss, or minutes only by typing [m].

How do you convert seconds to HH mm SS format in Excel?

As with Excel, the first step to converting elapsed second to time is to divide the value by 86400. To format the cells for mm:ss, select Format > Number > More Formats > More date and time formats from the Menu.

What is the format for hours and minutes?

We express hours (60 minutes) and portions of an hour (minutes) with the format HH:MM. So whenever we see time expressed with a colon (:) we know that we are seeing time in the hours and minutes format. Let's look at a simple example. 7:30 is read as 'seven hours and 30 minutes.


1 Answers

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);
like image 92
Joe K Avatar answered Oct 15 '22 11:10

Joe K