Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    public static String humanizeTimeSpan2(int minutes) {
        int hours = minutes / 60;

        if (minutes < 15) {
            return getUnits(minutes, "minute", "minutes");
        } else if (minutes < 30) {
            return getUnits(roundTo(minutes, 5), "minute", "minutes");
        } else if (minutes < 60) {
            return getUnits(roundTo(minutes, 15), "minute", "minutes");
        } else if (minutes < 60 * 4) {
            int hourMins = roundTo(minutes - hours * 60, 15);
            if (hourMins == 60) {
                hours++;
                hourMins = 0;
            }

            if (hourMins == 0) {
                return getUnits(hours, "hour", "hours");
            } else {
                return String.format("%dh:%02dmin", hours, hourMins);
            }
        } else if (minutes < 24 * 60) {
            return getUnits(hours, "hour", "hours");
        } else {
            return getUnits(hours / 24, "day", "days");
        }
    }

    private static String getUnits(int amount, String unitName, String unitNamePlural) {
        if (amount == 1)
            return String.format("1 %s", unitName);
        return String.format("%d %s", amount, unitNamePlural);
    }

    private static int roundTo(int in, int precision) {
        return precision * ((int) Math.floor(in / (1.0 * precision)));
    }
}