转换分钟进入人类可读的格式 [英] Convert minutes into a human readable format

查看:145
本文介绍了转换分钟进入人类可读的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速的问题。

有一个更聪明/更时尚的方式来分钟,转换成更易读的格式,只显示最显著的数字?

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

我使用Android Studio中的Java。

I'm using Android Studio's Java.

public String MinutesToHumanReadable(Long minutes) {

...

}

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"

我的code是非常繁琐的,不拘小节,而且有点无法读取。

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

推荐答案

那么它是可能的,我看着办吧,我为此而自豪! :)
请注意,您可以轻松地只能由这两个数组改变数值添加任何其他值,而不改变方法本身。例如,如果年是不够的,你可以添加十年和世纪...

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"...

这code还增加了S字母结尾,如果你产值已超过1。

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;
    }         
}

样品输入:

    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));
    }  

输出是:

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

这篇关于转换分钟进入人类可读的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆