[英]Convert minutes into a human readable format
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.我正在使用 Android Studio 的 Java。
public String MinutesToHumanReadable(Long minutes) {
...
}
ie 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非常感谢,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"...
例如,如果“年”对你来说还不够,你可以添加“几十年”和“几个世纪”......
This code also adding "s" letter at the end, if you have more than 1 of output value. 如果输出值超过1,此代码也会在末尾添加“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;
}
}
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
I would use some dummy if/else if/else
statements : 我会使用一些虚拟的
if/else if/else
语句:
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
1小时24分钟
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);
}
Improving answer by Joshua Pinter and Arnaud Denoyelle. Joshua Pinter 和 Arnaud Denoyelle 改进了答案。
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)
)
}
}
And add this to res/values/strings.xml file并将其添加到 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>
In this way it will show singular or plural depending on the amount and also supports values larger than one day.通过这种方式,它将根据数量显示单数或复数,并且还支持大于一天的值。 For example: 2 days, 8 hrs, 23 mins or 1 day, 2 hrs, 1 min
例如:2 天 8 小时 23 分钟或 1 天 2 小时 1 分钟
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.