Java 如何将分钟转换为天、小时、分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2751073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert minutes to days,hours,minutes
提问by Eddinho
how to convert minutes into days hours and minutes in java ( we have a week here , 7 days )
如何在java中将分钟转换为天小时和分钟(我们这里有一周,7天)
public String timeConvert(int time){
String t = "";
int h = 00;
int m = 00;
// h= (int) (time / 60);
// m = (int) (time % 60);
// if(h>=24) h=00;
if((time>=0) && (time<=24*60)){
h= (int) (time / 60);
m = (int) (time % 60);
}else if((time>24*60) && (time<=24*60*2)){
h= (int) (time / (1440));
m = (int) (time % (1440));
}else if((time>24*60*2) && (time<=24*60*3)){
h= (int) (time / (2880));
m = (int) (time % (2880));
}else if((time>24*60*3) && (time<=24*60*4)){
h= (int) (time / (2880*2));
m = (int) (time % (2880*2));
}else if((time>24*60*4) && (time<=24*60*5)){
h= (int) (time / (2880*3));
m = (int) (time % (2880*3));
}else if((time>24*60*5) && (time<=24*60*6)){
h= (int) (time / (2880*4));
m = (int) (time % (2880*4));
}else if((time>24*60*6) && (time<=24*60*7)){
h= (int) (time / (2880*5));
m = (int) (time % (2880*5));
}
t =h+":"+m ;
return t;
}
I tried this but it dont work
我试过这个,但它不起作用
thanks
谢谢
采纳答案by Peter Lawrey
A shorter way. (Assumes time >= 0)
更短的方式。(假设时间 >= 0)
public String timeConvert(int time) {
return time/24/60 + ":" + time/60%24 + ':' + time%60;
}
回答by Eyal Schneider
If you use Java 6, TimeUnit enum can be useful. For example:
如果您使用 Java 6,TimeUnit 枚举会很有用。例如:
TimeUnit.HOURS.convert(10, TimeUnit.DAYS)
This static call converts 10 days into hour units, and returns 240. You can play with time units starting from NANOSECONDS and ending with DAYS.
此静态调用将 10 天转换为小时单位,并返回 240。您可以使用从 NANOSECONDS 开始到 DAYS 结束的时间单位。
Actually TimeUnit is available from Java 5, but in version 6 more units were added.
实际上 TimeUnit 在 Java 5 中可用,但在版本 6 中添加了更多单元。
--EDIT-- Now that I understand better your question, use the division and remainder approach as in the response of Romain. My tip is useful only for conversion to a single time unit.
--编辑--现在我更好地理解了您的问题,请使用 Romain 的响应中的除法和余数方法。我的提示仅对转换为单个时间单位有用。
回答by Romain Hippeau
If you want to do this yourself, go the other way.
如果你想自己做这件事,走另一条路。
- Divide the number by 60 * 24. (That will get the number of days.)
- Divide the remainder by 60. (That will give you number of hours.)
- The remainder of #2 is the number of minutes.
- 将数字除以 60 * 24。(这将得到天数。)
- 将余数除以 60。(这将为您提供小时数。)
- #2 的余数是分钟数。
回答by Loren Pechtel
1) Your code is repetitive. This is a sign of bad code in my opinion.
1)您的代码是重复的。在我看来,这是错误代码的标志。
2) The divisor shouldn't be changing with the number of days, because the number of days has little to do with the number of minutes in an hour.
2)除数不应随天数而变化,因为天数与一小时的分钟数几乎没有关系。
Beyond that, look at Romain Hippeau's approach, he told you how to do it.
除此之外,看看 Romain Hippeau 的方法,他告诉你如何去做。
回答by Eddinho
and the answer is :
答案是:
public String timeConvert(int time){
String t = "";
int j = time/(24*60);
int h= (time%(24*60)) / 60;
int m = (time%(24*60)) % 60;
t =j + ":" + h + ":" + m;
return t;
}
what do you think about this code?
你怎么看这段代码?
回答by jaspreet singh
i am using this code. it can also help.
我正在使用此代码。它也可以提供帮助。
private String getText(int minutes){
int weeks = minutes / 10080;
int aboveWeeks = minutes % 10080;
int days = aboveWeeks / 1440;
int aboveDays = aboveWeeks % 1440;
int hours = aboveDays / 60;
int aboveHours = aboveDays % 60;
int minute = aboveHours / 60;
if(weeks > 0 && days > 0) {
if(weeks > 1 && days > 1){
return weeks + " weeks " + days + " days before";
} else {
return weeks + " weeks " + days + " day before";
}
} else if (weeks > 0){
if (weeks > 1){
return weeks + " weeks before";
} else {
return weeks + " week before";
}
} else if(days > 0 && hours > 0){
if(days > 1 && hours > 1){
return days + " days " + hours + " hours before";
} else {
return days + " days " + hours + " hour before";
}
} else if(days > 0){
if (days > 1){
return days + " days before";
} else {
return days + " day before";
}
} else if(hours > 0 && minute > 0){
if(hours > 1 && minute > 1){
return hours + " hours " + minute + " minutes before";
} else {
return hours + " hours " + minute + " minute before";
}
} else if(hours > 0){
if (hours > 1){
return hours + " hours before";
} else {
return hours + " hour before";
}
} else {
if (minutes > 1){
return minutes + " minutes before";
} else {
return minutes + " minute before";
}
}
}
回答by Durgesh Pal
class time{
public static void main (String args[]){
System.out.println("Hello");
int duration=1500;
String testDuration = "";
if(duration < 60){
testDuration = duration + " minutes";
}
else{
if((duration / 60)<24)
{
if((duration%60)==0){
testDuration = (duration / 60) + " hours";
}
else{
testDuration = (duration / 60) + " hours," + (duration%60) + " minutes";
}
}
else{
if((duration%60)==0){
if(((duration/60)%24)==0){
testDuration = ((duration / 24)/60) + " days,";
}
else{
testDuration = ((duration / 24)/60) + " days," + (duration/60)%24 +"hours";
}
}
else{
testDuration = ((duration / 24)/60) + " days," + (duration/60)%24 +"hours"+ (duration%60) + " minutes";
}
}
}
System.out.println(testDuration);
}
}
回答by Ronny Shibley
1 day 2 hrs 5 mins
1 天 2 小时 5 分钟
public static String convertToDaysHoursMinutes(long minutes) {
int day = (int)TimeUnit.MINUTES.toDays(minutes);
long hours = TimeUnit.MINUTES.toHours(minutes) - (day *24);
long minute = TimeUnit.MINUTES.toMinutes(minutes) - (TimeUnit.MINUTES.toHours(minutes)* 60);
String result = "";
if (day != 0){
result += day;
if (day == 1){
result += " day ";
}
else{
result += " days ";
}
}
if (hours != 0){
result += hours;
if (hours == 1){
result += " hr ";
}
else{
result += " hrs ";
}
}
if (minute != 0){
result += minute;
if (minute == 1){
result += " min";
}
else{
result += " mins";
}
}
return result;
}
回答by NAVIN ROY
for best readability ,like it won't print 0 day or 0 hour or 0 minute. e.g- (if minute=1500,then it will print only 1 day 1 hour)
为了获得最佳可读性,就像它不会打印 0 天或 0 小时或 0 分钟一样。例如-(如果分钟=1500,那么它只会打印 1 天 1 小时)
int minute=150;
整数分钟=150;
StringBuilder sb=new StringBuilder();
int day=minute/1440;
int rem=minute%1440;
int hour=rem/60;
int Minute=rem%60;
if(day>0)
sb.append(day+" day ");
if(hour>0)
sb.append(hour+" hour ");
if(Minute>0)
sb.append(Minute+" minute");
System.out.println(sb);
回答by Kathy
const convertMinutesToDays = (minutes) => {
let hours
let days
let restMinutes
const onedayMinutes = 1440 //24*60
if(minutes < 60){
return `${minutes} Minutes`
}else if(minutes > 60 && minutes < onedayMinutes){
hours = Math.floor(minutes/60)
restMinutes = minutes%60
return `${hours} Hours ${restMinutes} Minutes`
}else{
days = Math.floor((minutes/60)/24)
restMinutes = minutes % onedayMinutes
hours = Math.floor(restMinutes/60)
restMinutes = restMinutes % 60
return `${days} Days ${hours} Hours ${restMinutes} Minutes`
}
}