如何将分钟转换为天、小时、分钟
如何在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;
}
我尝试了这个,但它不起作用,
谢谢
how to convert minutes into days hours and minutes in java ( we have a week here , 7 days )
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
一条更短的路。 (假设时间 >= 0)
A shorter way. (Assumes time >= 0)
如果您想自己执行此操作,请走另一条路。
If you want to do this yourself, go the other way.
如果您使用 Java 6,TimeUnit 枚举可能会很有用。例如:
此静态调用将 10 天转换为小时单位,并返回 240。您可以使用从 NANOSECONDS 开始到 DAYS 结束的时间单位。
实际上,TimeUnit 从 Java 5 就可以使用,但在版本 6 中添加了更多单位。
- 编辑 -
现在我更好地理解了你的问题,请使用除法和余数方法,就像罗曼的回答一样。我的技巧仅适用于转换为单个时间单位。
If you use Java 6, TimeUnit enum can be useful. For example:
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.
Actually TimeUnit is available from Java 5, but in version 6 more units were added.
--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.
Java-9
java.time
解决方案:输出:
Java-8
java.time
解决方案:输出:
了解Trail: Date Time< 中的现代日期时间 API /强>。
Java-9
java.time
Solution:Output:
Java-8
java.time
Solution:Output:
Learn about the modern date-time API from Trail: Date Time.
答案是:
你觉得这段代码怎么样?
and the answer is :
what do you think about this code?
1)你的代码是重复的。在我看来,这是糟糕代码的标志。
2)除数不应随天数变化,因为天数与一小时内的分钟数关系不大。
除此之外,看看 Romain Hippeau 的方法,他告诉你如何去做。
1) Your code is repetitive. This is a sign of bad code in my opinion.
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.
Beyond that, look at Romain Hippeau's approach, he told you how to do it.
如果有人想使用 JavaScript 接受的答案,时间将以小数显示。为了删除这些小数,您可以使用以下代码:
In case someone wants to use the accepted answer for JavaScript, time will be shown with decimals. In order to remove those decimals, you can use this code:
1天2小时5分钟
1 day 2 hrs 5 mins
我正在使用这个代码。它也有帮助。
i am using this code. it can also help.
为了获得最佳可读性,它不会打印 0 天或 0 小时或 0 分钟。
例如-(如果分钟=1500,那么它只会打印1天1小时)
int分钟=150;
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)
int minute=150;
(时间 / 24 / 60).toFixed(0) + ":" +(时间 / 60 % 24).toFixed(0) + ':' + (时间 % 60)
做到最好
(time / 24 / 60).toFixed(0) + ":" +(time / 60 % 24).toFixed(0) + ':' + (time % 60)
Does it best
只需让方法返回字符串并采取( int 分钟)
String getTextOfTime(int minuts) {返回结果
String getTextOfTime(int minuts) {
}
just make method return string and take( int minuts)
String getTextOfTime(int minuts) {return result
String getTextOfTime(int minuts) {
}
对于那些寻找此逻辑的 SQL 版本的人:
其中 c.a_ARR 是开始日期时间,c.a_DEP 是结束日期时间
For those looking for an SQL version of this logic:
where c.a_ARR is a Start Datetime, c.a_DEP is an End DateTime