我需要获得从当前时间到2038年的日,小时,分钟,秒。我遇到输出问题。
public class Assignment1 {
public static void main(String[] args) {
long now = System.currentTimeMillis();
long y2k38 = (long) Math.pow(2, 31)*1000;
long diffmillis = y2k38-now;
long diffsec = (y2k38-now)/1000;
long diffmin = diffsec/60;
long diffhours = diffmin/60;
long diffdays = diffhours/24;
System.out.printf(
"Y2K38 will occur in %d days.\n"+
"Y2K38 will occur in %d hours.\n"+
"Y2K38 will occur in %d minutes.\n"+
"Y2K38 will occur in %d seconds.\n",
(diffdays%24),(diffhours%60), (diffmin%60),(diffsec%60));
}
}
答案 0 :(得分:0)
您的主要问题是,您的diffdays
,diffhours
,diffmin
和diffsec
都只是不与当前时间戳实际交互的常量,因此{ {1}},y2k38-diffdays
和y2k38-diffhours
只是常量减去常量。
您可能希望使用y2k38-diffmin
计算到2038年之前的毫秒数,然后将其转换为您想要的单位。
例如,y2k38-now
应该给你几秒钟,直到2038年,(y2k38-now)/1000
应该给你分钟等等。