Java 如何以秒为单位获取自午夜以来的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18974729/
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 get the time since midnight in seconds
提问by TaylorTDHouse
First off, this is a simple question that I'm stuck on in my Java 1 class. It's a static time that I set already as 8:49:12 "today" and I'm to figure out how many seconds past midnight and "to" midnight this represents. 8 hours, 49 minutes, and 12 seconds. Here is my code now:
首先,这是我在 Java 1 课程中遇到的一个简单问题。这是一个静态时间,我已经设置为“今天”的 8:49:12,我要弄清楚这代表午夜和午夜过后多少秒。8 小时 49 分 12 秒。这是我现在的代码:
hour = 8;
minute = 59;
second = 32;
System.out.println("The static time used for this program was: " + hour + ":" + minute + ":" + second);
My issue is that I have no clue on how to get the time fromand sincemidnight.
我的问题是,我如何获得的时候不知道从和自午夜。
So basically the output needs to be:
所以基本上输出需要是:
Number of seconds since midnight:
Number of seconds to midnight:
And the space after is for the seconds.
后面的空格是秒。
Thanks, and please explain why, and how you chose how to solve this. I want to learn :P
谢谢,请解释原因,以及您如何选择解决此问题的方法。我想学习:P
采纳答案by Vimal Bera
Try this simple maths :
试试这个简单的数学:
System.out.println("Number of seconds since midnight:" +(second + (minute*60) + (hour*3600)));
System.out.println("Number of seconds to midnight:" +((60-second) + ((60-1-minute)*60) + (24-1-hour)*3600));
回答by Gabriel Negut
final static int SEC_IN_MIN = 60;
final static int SEC_IN_HOUR = SEC_IN_MIN * 60;
int secFromMidnight = hour * SEC_IN_HOUR + minute * SEC_IN_MIN + second;
int secToMidnight = (24 * SEC_IN_HOUR) - secFromMidnight;
回答by angel_navarro
Your answer is in java.util.Calendar class. Please see get()/set() methods: http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html
您的答案在 java.util.Calendar 类中。请参阅 get()/set() 方法:http: //docs.oracle.com/javase/6/docs/api/java/util/Calendar.html
回答by smajlo
import java.util.Calendar;
public class HelloWorld{
public static void main(String []args){
int hour = 8;
int minute = 59;
int second = 32;
System.out.println("The static time used for this program was: " + hour + ":" + minute + ":" + second);
final Calendar currentTime = Calendar.getInstance();
currentTime.set(2013,Calendar.SEPTEMBER,24,8,59,32);
final Calendar midNight = Calendar.getInstance();
midNight.clear();
midNight.set(2013, Calendar.SEPTEMBER, 25);
System.out.println(.001*(midNight.getTimeInMillis() - currentTime.getTimeInMillis()));
}
}
}
Consider using Calendar
and or try JodaTime
library
考虑使用Calendar
和或尝试JodaTime
库
回答by MadProgrammer
Given the idiosyncrasies of time, you could simply use the Calendar
API
鉴于时间的特殊性,您可以简单地使用Calendar
API
Calendar fromMidnight = Calendar.getInstance();
fromMidnight.set(Calendar.HOUR, 0);
fromMidnight.set(Calendar.MINUTE, 0);
fromMidnight.set(Calendar.SECOND, 0);
fromMidnight.set(Calendar.MILLISECOND, 0);
Calendar toMidnight = Calendar.getInstance();
toMidnight.setTime(fromMidnight.getTime());
toMidnight.add(Calendar.DATE, 1);
System.out.println(fromMidnight.getTime());
System.out.println(toMidnight.getTime());
Calendar toFromTime = Calendar.getInstance();
toFromTime.set(Calendar.HOUR, 8);
toFromTime.set(Calendar.MINUTE, 59);
toFromTime.set(Calendar.SECOND, 32);
toFromTime.set(Calendar.MILLISECOND, 0);
long secondsFromMidnight = (toFromTime.getTimeInMillis() - fromMidnight.getTimeInMillis()) / 1000;
long secondsToMidnight = (toMidnight.getTimeInMillis() - toFromTime.getTimeInMillis()) / 1000;
System.out.println("from = " + secondsFromMidnight + "; to " + secondsToMidnight);
Which outputs...
哪个输出...
from = 32372; to 54028
Or you could use JodaTime...
或者你可以使用JodaTime...
MutableDateTime now = MutableDateTime.now();
now.setMillisOfDay(0);
now.setSecondOfDay(32);
now.setMinuteOfDay(59);
now.setHourOfDay(8);
DateTime fromMidnight = now.toDateTime().toDateMidnight().toDateTime();
DateTime toMidnight = fromMidnight.plusDays(1);
Duration duration = new Duration(fromMidnight, toMidnight);
Duration dFromMidnight = new Duration(fromMidnight, now);
System.out.println("From midnight: " + dFromMidnight.getStandardSeconds());
Duration dToMidnight = new Duration(now, toMidnight);
System.out.println("To Midnight: " + dToMidnight.getStandardSeconds());
Which outputs...
哪个输出...
From midnight: 32372
To Midnight: 54028
回答by Heisenberg
There is an enum in JAVA
called TimeUnit
that can convert time to any time unit like this:
在JAVA
调用TimeUnit
中有一个枚举可以将时间转换为任何时间单位,如下所示:
int hour = 8;
int minute = 59;
int second = 32;
System.out.println("The static time used for this program was: " + hour + ":" + minute + ":" + second);
long secInMidnight = TimeUnit.HOURS.toSeconds(24);
long timeInSeconds = (TimeUnit.HOURS.toSeconds(8) + TimeUnit.MINUTES.toSeconds(minute) + second);
System.out.println("\nSince midnight: " + timeInSeconds + "\nUntil midnight: " + (secInMidnight - timeInSeconds) );
回答by Meno Hochschild
The accepted answer of @Vimal Bera is fine and completely sufficient due to the mathematical simplicity of the problem. But if you still prefer a "formalized" (supported by library) approach without doing "much" calculation then you could do this in Java-8:
由于问题的数学简单性,@Vimal Bera 的公认答案很好并且完全足够。但是,如果您仍然喜欢“形式化”(由库支持)方法而不进行“大量”计算,那么您可以在Java-8 中执行此操作:
LocalTime time = LocalTime.of(8, 59, 32);
System.out.println(
"Seconds since midnight: "
+ time.get(ChronoField.SECOND_OF_DAY)); // 32372
System.out.println(
"Seconds to midnight: "
+ (86400 - time.get(ChronoField.SECOND_OF_DAY))); // 54028