计算天,小时,分钟,秒,直到2038年

时间:2015-09-15 03:14:26

标签: java computer-science

我需要获得从当前时间到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));
}

}

1 个答案:

答案 0 :(得分:0)

您的主要问题是,您的diffdaysdiffhoursdiffmindiffsec都只是不与当前时间戳实际交互的常量,因此{ {1}},y2k38-diffdaysy2k38-diffhours只是常量减去常量。

您可能希望使用y2k38-diffmin计算到2038年之前的毫秒数,然后将其转换为您想要的单位。

例如,y2k38-now应该给你几秒钟,直到2038年,(y2k38-now)/1000应该给你分钟等等。