Convert minutes into a human readable format

梦想的初衷 提交于 2019-12-10 18:39:48

问题


quick question.

Is there a smarter/sleeker way to convert minutes into a more readable format, showing only the most significant digit?

I'm using Android Studio's Java.

public String MinutesToHumanReadable(Long minutes) {

...

}

ie

2 mins = "2 mins"
45 mins = "45 mins"
60 mins = ">1 hr"
85 mins = ">1 hr"
120 mins = ">2 hrs"
200 mins = ">3 hrs"
1500 mins = ">1 day"

My code is very cumbersome, sloppy, and somewhat unreadable.

public String MinutesToHumanReadable(long minutes) {
    String sReturn = "";

    if (minutes > 515600) {
        sReturn = "> 1 yr";

    } else if (minutes > 43200) {
        sReturn = (minutes / 43200) + " mths";

    } else if (minutes > 10080) {
        sReturn = (minutes / 10080) + " wks";

    } else if (minutes > 1440) {
        sReturn = (minutes / 1440) + " days";

    } else if (minutes > 60) {
        sReturn = (minutes / 60) + " hrs";

    } else {
        //<60
        sReturn = minutes + " mins";

    }

    return sReturn;
}

Many thanks, J


回答1:


Well it is possible, I figure it out and I am proud of it! :) Note that you can easily add any other value without changing the method itself, only by changing values in these two arrays. For example, if "years" are not enough for you, you can add "decades" and "centuries"...

This code also adding "s" letter at the end, if you have more than 1 of output value.

public class SuperMinutesChangerClass {
    public static int[] barriers = {1, 60, 60*24, 60*24*7, 60*24*365, Integer.MAX_VALUE};
    public static String[] text = {"min", "hr", "day", "week", "year"};

    public static String minutesToHumanReadable(int minutes){
        String toReturn = "";
        for (int i = 1; i < barriers.length; i++) {
            if (minutes < barriers[i]){
                int ammount = (minutes/barriers[i-1]);
                toReturn = ">" + (ammount) + " " + text[i-1];
                if (ammount > 1){
                    toReturn += "s";
                }
                break;
            }
        }
        return toReturn;
    }         
}

Sample input :

    public static void main(String[] args) {
        System.out.println(minutesToHumanReadable(10));
        System.out.println(minutesToHumanReadable(60));
        System.out.println(minutesToHumanReadable(61));
        System.out.println(minutesToHumanReadable(121));
        System.out.println(minutesToHumanReadable(8887));
        System.out.println(minutesToHumanReadable(9999743));
    }  

Output is :

>10 mins
>1 hr
>1 hr
>2 hrs
>6 days
>19 years



回答2:


I would use some dummy if/else if/else statements :

public static String convert(int minutes) {
  if(minutes < 60) {
    return String.format("%d mins", minutes);
  } else if(minutes < 1440) { //1 day = 1440 minutes
    return String.format("%d hrs, %d mins", minutes/60, minutes%60);
  } else {
    return String.format("%d days", minutes / 1440);
  }
}

Test

System.out.println(convert(84));

Output :

1 hrs, 24 mins




回答3:


maybe this helps you

private static final int MINUTES_PER_HOUR = 60;
private static final int MINUTES_PER_DAY = MINUTES_PER_HOUR * 24;

public String minutesToHumanReadable(long minutes) {
    if (minutes > MINUTES_PER_DAY) {
        return String.format("> %d days", minutes / MINUTES_PER_DAY);
    } else if (minutes > MINUTES_PER_HOUR) {
        return String.format("> %d hours", minutes / MINUTES_PER_HOUR);
    }

    return String.format("%d minutes", minutes);
}


来源:https://stackoverflow.com/questions/19636583/convert-minutes-into-a-human-readable-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!