Total Pageviews

2019/02/15

[Java] How to convert seconds to minutes?

Problem
How to convert seconds to minutes in Java ?
For example, 230 seconds should convert to 3 minutes 50 seconds.

How-To
Here has sample code:
package albert.test;

import java.util.concurrent.TimeUnit;

public class TimeUtils {

    public static void main(String[] args) {
        System.out.println(TimeUtils.toMinutes(230));
    }

    public static String toMinutes(long second) {
        long mm = TimeUnit.SECONDS.toMinutes(second);
        long ss = TimeUnit.SECONDS.toSeconds(second) - (mm * 60);

        String str = "%d 分 %d 秒";
        return String.format(str, mm, ss);
    }

}

No comments: