Java:将秒转换为分钟、小时和天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25903354/
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
Java: convert seconds to minutes, hours and days
提问by dsfafdadf
"The output should look like this (it is a good idea to echo back the input): You entered 500,000 seconds, which is 5 days, 18 hours, 53 minutes and 20 seconds. (5 days 18:53:20 hours)
“输出应如下所示(回显输入是个好主意):您输入了 500,000 秒,即 5 天 18 小时 53 分 20 秒。(5 天 18:53:20 小时)
So can you guys tell me how I should do it, what is the easiest way to understand and do it, please help guys, I'm really stuck, also he said "no hard coding" which I'm not exactly sure what it is but I think he wants us to assign them constants.
所以你们能告诉我我应该怎么做吗,什么是最简单的理解和做的方法,请帮助伙计们,我真的被卡住了,他还说“没有硬编码”,我不确定它是什么是,但我认为他希望我们为它们分配常量。
回答by CharlieS
Have a look at the class
看看班级
org.joda.time.DateTime
This allows you to do things like:
这允许您执行以下操作:
old = new DateTime();
new = old.plusSeconds(500000);
System.out.println("Hours: " + (new.Hours() - old.Hours()));
However, your solution probably can be simpler:
但是,您的解决方案可能会更简单:
You need to work out how many seconds in a day, divide your input by the result to get the days, and subtract it from the input to keep the remainder. You then need to work out how many hours in the remainder, followed by the minutes, and the final remainder is the seconds.
您需要计算一天中有多少秒,将您的输入除以结果以获得天数,然后从输入中减去它以保留余数。然后您需要计算出剩余的小时数,然后是分钟数,最后的剩余数是秒数。
This is the analysis done for you, now you can focus on the code.
这是为您完成的分析,现在您可以专注于代码。
You need to ask what s/he means by "no hard coding", generally it means pass parameters, rather than fixing the input values. There are many ways to do this, depending on how you run your code. Properties are a common way in java.
你需要问他/她“没有硬编码”是什么意思,通常是指传递参数,而不是固定输入值。有很多方法可以做到这一点,这取决于您如何运行代码。属性是java中常用的方式。
回答by ambes
my quick answer with basic java arithmetic calculation is this:
我对基本的java算术计算的快速回答是这样的:
First consider the following values:
首先考虑以下值:
1 Minute = 60 Seconds
1 Hour = 3600 Seconds ( 60 * 60 )
1 Day = 86400 Second ( 24 * 3600 )
- First divide the input by 86400, if you you can get a number greater than 0 , this is the number of days. 2.Again divide the remained number you get from the first calculation by 3600, this will give you the number of hours
- Then divide the remainder of your second calculation by 60 which is the number of Minutes
- Finally the remained number from your third calculation is the number of seconds
- 首先将输入除以 86400,如果你能得到一个大于 0 的数字,这就是天数。2.再次将您从第一次计算中得到的剩余数字除以 3600,这将为您提供小时数
- 然后将第二次计算的余数除以 60,即分钟数
- 最后,第三次计算的剩余数字是秒数
the code snippet is as follows:
代码片段如下:
int input=500000;
int numberOfDays;
int numberOfHours;
int numberOfMinutes;
int numberOfSeconds;
numberOfDays = input / 86400;
numberOfHours = (input % 86400 ) / 3600 ;
numberOfMinutes = ((input % 86400 ) % 3600 ) / 60
numberOfSeconds = ((input % 86400 ) % 3600 ) % 60 ;
I hope to be helpful to you.
我希望对你有所帮助。
回答by Elliott Frisch
You can use the Java enum TimeUnit
to perform your math and avoid any hard coded values. Then we can use String.format(String, Object...)
and a pair of StringBuilder
(s) as well as a DecimalFormat
to build the requested output. Something like,
您可以使用 Java 枚举TimeUnit
来执行数学运算并避免使用任何硬编码值。然后我们可以使用String.format(String, Object...)
和一对StringBuilder
(s) 以及 aDecimalFormat
来构建请求的输出。就像是,
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number of seconds:");
String str = scanner.nextLine().replace("\,", "").trim();
long secondsIn = Long.parseLong(str);
long dayCount = TimeUnit.SECONDS.toDays(secondsIn);
long secondsCount = secondsIn - TimeUnit.DAYS.toSeconds(dayCount);
long hourCount = TimeUnit.SECONDS.toHours(secondsCount);
secondsCount -= TimeUnit.HOURS.toSeconds(hourCount);
long minutesCount = TimeUnit.SECONDS.toMinutes(secondsCount);
secondsCount -= TimeUnit.MINUTES.toSeconds(minutesCount);
StringBuilder sb = new StringBuilder();
sb.append(String.format("%d %s, ", dayCount, (dayCount == 1) ? "day"
: "days"));
StringBuilder sb2 = new StringBuilder();
sb2.append(sb.toString());
sb2.append(String.format("%02d:%02d:%02d %s", hourCount, minutesCount,
secondsCount, (hourCount == 1) ? "hour" : "hours"));
sb.append(String.format("%d %s, ", hourCount, (hourCount == 1) ? "hour"
: "hours"));
sb.append(String.format("%d %s and ", minutesCount,
(minutesCount == 1) ? "minute" : "minutes"));
sb.append(String.format("%d %s.", secondsCount,
(secondsCount == 1) ? "second" : "seconds"));
System.out.printf("You entered %s seconds, which is %s (%s)%n",
new DecimalFormat("#,###").format(secondsIn), sb, sb2);
Which, when I enter 500000
outputs the requested (manual line break added for post) -
其中,当我输入500000
请求的输出时(为帖子添加了手动换行符)-
You entered 500,000 seconds, which is 5 days, 18 hours,
53 minutes and 20 seconds. (5 days, 18:53:20 hours)
回答by dsfafdadf
Thanks guys for all the help, I really appreciate but I actually did some thinking and start doing some pseudo code and came up with this.
感谢大家的帮助,我真的很感激,但我实际上做了一些思考并开始编写一些伪代码并想出了这个。
import java.util.Scanner;
public class Project {
公共类项目{
public static void main(String[] args) {
//variable declaration
Scanner scan = new Scanner(System.in);
final int MIN = 60, HRS = 3600, DYS = 84600;
int input, days, seconds, minutes, hours, rDays, rHours;
//input
System.out.println("Enter amount of seconds!");
input = scan.nextInt();
//calculations
days = input/DYS;
rDays = input%DYS;
hours = rDays/HRS;
rHours = rDays%HRS;
minutes = rHours/MIN;
seconds = rHours%MIN;
//output
if (input >= DYS) {
System.out.println(input + " seconds equals to " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds");
}
else if (input >= HRS && input < DYS) {
System.out.println(input + " seconds equals to " + hours + " hours " + minutes + " minutes " + seconds + " seconds");
}
else if (input >= MIN && input < HRS) {
System.out.println(input + " seconds equals to " + minutes + " minutes " + seconds + " seconds");
}
else if (input < MIN) {
System.out.println(input + " seconds equals to seconds");
}
scan.close();
}
I know it looks really noobie but keep in mind I'm still new not just Java but programming entirely, and who knew pseudo code was actually really helpful.
我知道它看起来真的很菜,但请记住,我仍然是新手,不仅是 Java,而且是完全编程,谁知道伪代码实际上真的很有帮助。
回答by alkber
An example using built in TimeUnit
.
使用内置TimeUnit
.
long uptime = System.currentTimeMillis();
long days = TimeUnit.MILLISECONDS
.toDays(uptime);
uptime -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS
.toHours(uptime);
uptime -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS
.toMinutes(uptime);
uptime -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS
.toSeconds(uptime);
回答by Jura.Sl
The simpliest way
最简单的方法
Scanner in = new Scanner(System.in);
System.out.println("Enter seconds");
int s = in.nextInt();
int sec = s % 60;
int min = (s / 60)%60;
int hours = (s/60)/60;
System.out.println(hours + ":" + min + ":" + sec);
回答by High Zedd
You should try this
你应该试试这个
import java.util.Scanner;
public class Time_converter {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int seconds;
int minutes ;
int hours;
System.out.print("Enter the number of seconds : ");
seconds = input.nextInt();
hours = seconds / 3600;
minutes = (seconds%3600)/60;
int seconds_output = (seconds% 3600)%60;
System.out.println("The time entered in hours,minutes and seconds is:");
System.out.println(hours + " hours :" + minutes + " minutes:" + seconds_output +" seconds");
}
}
回答by Anand Pandey
It should be like:
它应该是这样的:
public static void calculateTime(long seconds) {
int day = (int)TimeUnit.SECONDS.toDays(seconds);
long hours = TimeUnit.SECONDS.toHours(seconds) - (day *24);
long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds)* 60);
long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) *60);
System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second);
}
Explanation:
解释:
TimeUnit.SECONDS.toHours(seconds) will give you direct conversion from seconds to hours with out consideration for days. Minus the hours for days you already got i.e, day*24. You now got remaining hours. Same for minute and second. You need to minus the already got hour and minutes respectively.
TimeUnit.SECONDS.toHours(seconds) 会给你从秒到小时的直接转换,而不需要考虑几天。减去您已经获得的天数的小时数,即天 * 24。你现在有剩余的时间。分秒相同。您需要分别减去已经得到的小时和分钟。