如何从午夜到秒获得时间

时间:2013-09-24 06:46:47

标签: java time clock

首先,这是一个我在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);

我的问题是,我不清楚如何从和获得时间午夜。

所以基本上输出需要是:

Number of seconds since midnight: 
Number of seconds to midnight:

之后的空间是秒。

谢谢,请解释原因,以及如何选择如何解决这个问题。我想学习:P

7 个答案:

答案 0 :(得分:7)

试试这个简单的数学:

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));

答案 1 :(得分:3)

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;

答案 2 :(得分:3)

鉴于时间的特殊性,您可以简单地使用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);

哪些输出......

from = 32372; to 54028

或者您可以使用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());

哪些输出......

From midnight: 32372
To Midnight: 54028

答案 3 :(得分:3)

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) );

答案 4 :(得分:1)

由于问题的数学简单性,@ 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

答案 5 :(得分:0)

你的答案是在java.util.Calendar类中。请参阅get()/ set()方法: http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html

答案 6 :(得分:0)

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()));
 }

}

考虑使用Calendar和/或尝试JodaTime

http://www.joda.org/joda-time/