如何以秒为单位获取自午夜以来的时间 [英] How to get the time since midnight in seconds

查看:25
本文介绍了如何以秒为单位获取自午夜以来的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是我在 Java 1 课程中遇到的一个简单问题.这是一个静态时间,我已经设置为今天"的 8:49:12,我要弄清楚这代表午夜和午夜过后多少秒.8 小时 49 分 12 秒.这是我现在的代码:

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:

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 from and since midnight.

所以基本上输出需要是:

So basically the output needs to be:

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

后面的空格是秒.

谢谢,请解释原因,以及您如何选择解决此问题的方法.我想学习:P

Thanks, and please explain why, and how you chose how to solve this. I want to learn :P

推荐答案

试试这个简单的数学:

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

这篇关于如何以秒为单位获取自午夜以来的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆