Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.support.annotation.NonNull;

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * @param uptime has to be in milliseconds
     * @return String representation rounded down to seconds, minutes, hours or days
     */
    @NonNull
    public static String getTimeString(int uptime) {
        int uptimeSecs = uptime / 10;
        String timeOn;
        if (TimeUnit.SECONDS.toMinutes(uptimeSecs) == 0) {
            timeOn = uptimeSecs + (uptimeSecs == 1 ? " sec" : " secs");
        } else if (TimeUnit.SECONDS.toHours(uptimeSecs) == 0) {
            long uptimeMins = TimeUnit.SECONDS.toMinutes(uptimeSecs);
            timeOn = uptimeMins + (uptimeMins == 1 ? " min" : " mins");
        } else if (TimeUnit.SECONDS.toDays(uptimeSecs) == 0) {
            long uptimeHours = TimeUnit.SECONDS.toHours(uptimeSecs);
            timeOn = uptimeHours + (uptimeHours == 1 ? " hour" : " hours");
        } else {
            long uptimeDays = TimeUnit.SECONDS.toDays(uptimeSecs);
            timeOn = uptimeDays + (uptimeDays == 1 ? " day" : " days");
        }
        return timeOn;
    }
}