[英]Displaying time elapsed since epoch
I am working on creating a time app that calculates both the current time and the time elapsed since midnight, January 1, 1970, in milliseconds. 我正在创建一个时间应用程序,该应用程序可以计算当前时间以及自1970年1月1日午夜以来经过的时间(以毫秒为单位)。 I went ahead and used Calendar and was able to successfully return the current time but for some reason the elapsed time returns 0. Not sure why that would be.
我继续使用Calendar,并能够成功返回当前时间,但是由于某种原因,经过的时间返回0。不知道为什么会这样。
Here is my current code: 这是我当前的代码:
import java.util.Calendar;
import java.util.TimeZone;
public class TimeApp {
public static void main(String[] args) {
Time time1 = new Time();
System.out.println("Hour: " + time1.getHour() + " Minute: " +
time1.getMinute() + " Second: " + time1.getSecond());
Time time2 = new Time();
System.out.println("Elapsed time since epoch: " + time2.getElapsedTime());
}
}
final class Time {
private int hour;
private int minute;
private int second;
private long secondsSinceEpoch;
public Time() {
Calendar calendar = Calendar.getInstance();
this.second = calendar.get(Calendar.SECOND);
this.minute = calendar.get(Calendar.MINUTE);
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
}
public Time(long elapsedTime) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.clear();
calendar.set(2016, Calendar.SEPTEMBER, 9);
secondsSinceEpoch = calendar.getTimeInMillis() / 1000L;
}
@Override
public String toString() {
return hour + ":" + minute + ":" + second;
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public int getSecond() {
return second;
}
public long getElapsedTime() {
return secondsSinceEpoch;
}
}
You aren't setting elapsedTime
for time2
. 您没有为
time2
设置elapsedTime
。 I think you wanted 我想你想要
Time time2 = new Time(System.currentTimeMillis());
And as pointed out in the comments, you aren't using elapsedTime
in your constructor. 正如在评论中指出,不使用
elapsedTime
在构造函数。 Something like 就像是
public Time(long elapsedTime) {
secondsSinceEpoch = elapsedTime / 1000;
}
I think you are using the wrong constructor for time2
since you called Time()
and this version does not set secondsSinceEpoch
. 我认为自从您调用
Time()
以来,您为time2
使用了错误的构造函数,并且此版本未设置secondsSinceEpoch
。 Try using your other constructor Time(long elapsedTime)
with any long value and see if it works. 尝试将您的其他构造函数
Time(long elapsedTime)
与任何long值一起使用,看看是否Time(long elapsedTime)
。 Like this .. 像这样 ..
Time time2 = new Time(10000);
Then re-write this constructor since you never use elapsedTime
anyway, or delete it completely and re-write the first constructor to assign a value to secondsSinceEpoch
. 然后重新编写此构造函数,因为无论如何从不使用
elapsedTime
,或者完全删除它,然后重新编写第一个构造函数以将值分配给secondsSinceEpoch
。
public Time() {
Calendar calendar = Calendar.getInstance();
this.second = calendar.get(Calendar.SECOND);
this.minute = calendar.get(Calendar.MINUTE);
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
secondsSinceEpoch = calendar.getTimeInMillis() / 1000L;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.