Android Studio 将会议记录转换为可读格式

xriantvc  于 2023-02-05  发布在  Android
关注(0)|答案(4)|浏览(88)

一个小问题。
有没有更聪明/更时尚的方法将分钟转换为更可读的格式,只显示最高有效位?
我使用的是Android Studio的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"

我的代码非常繁琐、草率,而且有些难以阅读。

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

非常感谢J

db2dz4w8

db2dz4w81#

我将使用一些虚拟的if/else if/else语句:

public static String convert(int minutes) {
  if(minutes < 60) {
    return String.format("%d mins", minutes);
  } else if(minutes % 60 == 0) { // No minutes - i.e. not a fractional hour.
    return String.format("%d hrs", minutes/60);
  } 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);
  }
}

示例输出

System.out.println(convert(24));   //=> 24 mins
System.out.println(convert(120));  //=> 2 hrs
System.out.println(convert(84));   //=> 1 hrs, 24 mins
System.out.println(convert(2880)); //=> 2 days
3bygqnnd

3bygqnnd2#

嗯,这是可能的,我想通了,我为此感到自豪!:)注意,你可以很容易地添加任何其他值,而不需要改变方法本身,只需要改变这两个数组中的值。例如,如果“年”对你来说不够,你可以添加“十年”和“世纪”...
此代码还在末尾添加“s”字母,如果有多个输出值。

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
3bygqnnd

3bygqnnd3#

也许这能帮你

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

rdrgkggo4#

约书亚·品特和阿诺德·德诺耶尔改进了答案。

fun convertMinutesToHumanReadable(minutes: Int, context: Context): String {
    return if (minutes < 60) {
        "$minutes ${context.resources.getQuantityString(R.plurals.minutes_abbrev, minutes)}"
    } else if (minutes % 60 == 0 && minutes < 1440) {
        val hours1 = minutes / 60
        "$hours1 ${context.resources.getQuantityString(R.plurals.hours_abbrev, hours1)}"
    } else if (minutes < 1440) {
        val hours1 = minutes / 60
        val minutes1 = minutes % 60
        "$hours1 ${
            context.resources.getQuantityString(
                R.plurals.hours_abbrev, hours1
            )
        }, $minutes1 ${
            context.resources.getQuantityString(
                R.plurals.minutes_abbrev, minutes1
            )
        } "
    } else if (minutes % 1440 == 0) {
        val days1 = minutes / 1440
        "$days1 ${context.resources.getQuantityString(R.plurals.days, days1)}"
    } else {
        val days1 = minutes / 1440
        "$days1 ${context.resources.getQuantityString(R.plurals.days, days1)}, ".plus(
            convertMinutesHumanReadable(minutes % 1440, context)
        )
    }
}

并将其添加到res/values/strings.xml文件中

<plurals name="minutes_abbrev">
        <item quantity="one">min</item>
        <item quantity="other">mins</item>
    </plurals>
    <plurals name="hours_abbrev">
        <item quantity="one">hr</item>
        <item quantity="other">hrs</item>
    </plurals>
    <plurals name="days">
        <item quantity="one">day</item>
        <item quantity="other">days</item>
    </plurals>

通过这种方式,它将根据数量显示单数或复数,并支持值 大于一天。例如:2天8小时23分钟或1天2小时1分钟

相关问题