请求协助并显示日期

时间:2017-03-08 02:39:36

标签: java time

所以,我有一些代码通过使用System.currentTimeMillis();语句执行大量的东西来确定日期。但是,我一直遇到代码问题,虽然我可以正确地显示月份和日期,但这一年总是提前2年(例如:如果它是1970年,我的代码将显示1972年,等等。 ..)。虽然插入一个从计算年份减去2年的语句会更容易,但我希望它可靠地计算年份。但是,唉,我找不到导致这个bug的问题。如果有人能帮助我解决这个问题,我将不胜感激。代码如下。

import java.io.*;
import java.util.*;

public class fancyClock {
public static void main(String[] args) {
int counter = 1;
int year = 1970;
boolean isLeapYear = false;
int yearsUntilLeapYear = 2;
int days = 0;
int daysTemp = 0;
int months = 1;
String month = "";
int hours = 24;
int minutes = 00;
int day = 0;


long millis = System.currentTimeMillis();
System.out.println(millis);

while (counter == 1) {
  if (millis - 86400000 > 0) {
     days++;
     millis = millis - 86400000;
  } else {
     counter--;
  }
}
counter++;
daysTemp = days;
while (counter == 1) {
   year++;
   yearsUntilLeapYear--;
if (yearsUntilLeapYear == 0) {
   isLeapYear = true;
}
  // ESSENTIAL!!! IF COND MUST BE MOD, NOT MINUS!!!
  if (daysTemp % 365 > 0 && isLeapYear == false) {
     daysTemp = daysTemp - 365;
  } else if (daysTemp % 366 > 0 && isLeapYear == true) {
     isLeapYear = false;
     yearsUntilLeapYear = 4;
     daysTemp = daysTemp - 366;
  } else {
     counter--;
  }
}
counter++;
if (isLeapYear == true) {
   daysTemp = daysTemp + 366;
} else if (isLeapYear == false) {
   daysTemp = daysTemp + 365;
}
System.out.println("Days after flip: " + daysTemp);

while (counter == 1) {
   if ((months == 1 && (daysTemp - 31 > 0)) || (months == 3 && (daysTemp - 31 > 0)) || (months == 5 && (daysTemp - 31 > 0)) || (months == 7 && (daysTemp - 31 > 0)) || (months == 8 && (daysTemp - 31 > 0)) || (months == 10 && (daysTemp - 31 > 0)) || (months == 12 && (daysTemp - 31 > 0))) {
     daysTemp = daysTemp - 31;
     months++;
  } else if (months == 2 && isLeapYear == true && (daysTemp - 29 > 0)) {
     daysTemp = daysTemp - 29;
     months++;
  } else if (months == 2 && isLeapYear == false && (daysTemp - 28 > 0)) {
     daysTemp = daysTemp - 28;
     months++;
  } else if ((months == 4 && (daysTemp - 30 > 0)) || (months == 6 && (daysTemp - 30 > 0)) || (months == 9 && (daysTemp - 30 > 0)) || (months == 11 && (daysTemp - 30 > 0))) {
     daysTemp = daysTemp - 30;
     months++;
  } else {
     counter--;
  }
}

if (months == 1) {
   month = "January";
} else if (months == 2) {
   month = "Febuary";
} else if (months == 3) {
   month = "March";
} else if (months == 4) {
   month = "April";
} else if (months == 5) {
   month = "May";
} else if (months == 6) {
   month = "June";
} else if (months == 7) {
   month = "July";
} else if (months == 8) {
   month = "August";
} else if (months == 9) {
   month = "September";
} else if (months == 10){
   month = "October";
} else if (months == 11){
   month = "November";
} else {
   month = "December";
}
day = daysTemp;


System.out.println("FANCY CLOCK BETA");
System.out.println("Current date: " + month + " " + day + ", " + year);


}
}

1 个答案:

答案 0 :(得分:0)

试试这个:

我做了很多改变以使其发挥作用:

  • 您显示的日期不正确,因为它已初始化为0,因为我们从1970年1月1日开始,它应为1

  • 如果您想考虑1970年的计算年度,您必须在1969年初始化年份,以便在考虑逻辑时考虑1970年。

  • 此外,只有当daysTemp符合365或366天的标准时才会发生++。

我在下面的代码中进行了这些更改,并将其输出为:

Days after flip: 67
FANCY CLOCK BETA
Current date: March 8, 2017

代码在这里:

public class fancyClock {
public static void main(String[] args) {
int counter = 1;
int year = 1969;
boolean isLeapYear = false;
int yearsUntilLeapYear = 2;
int days = 1;
int daysTemp = 0;
int months = 1;
String month = "";
int hours = 24;
int minutes = 00;
int day = 0;

long millis = System.currentTimeMillis();
System.out.println(millis);


while (counter == 1) {
  if (millis - 86400000 > 0) {
     days++;
     millis = millis - 86400000;
  } else {
     counter--;
  }
}
counter++;
daysTemp = days;
while (counter == 1) {
if (yearsUntilLeapYear == 0) {
   isLeapYear = true;
}
  // ESSENTIAL!!! IF COND MUST BE MOD, NOT MINUS!!!
  if (daysTemp % 365 > 0 && isLeapYear == false) {
     daysTemp = daysTemp - 365;
     year++;
     yearsUntilLeapYear--;
  } else if (daysTemp % 366 > 0 && isLeapYear == true) {
     isLeapYear = false;
     yearsUntilLeapYear = 4;
     daysTemp = daysTemp - 366;
     year++;
     yearsUntilLeapYear--;
  } else {
     counter--;
  }
}
counter++;
if (isLeapYear == true) {
   daysTemp = daysTemp + 366;
} else if (isLeapYear == false) {
   daysTemp = daysTemp + 365;
}
System.out.println("Days after flip: " + daysTemp);

while (counter == 1) {
   if ((months == 1 && (daysTemp - 31 > 0)) || (months == 3 && (daysTemp - 31 > 0)) || (months == 5 && (daysTemp - 31 > 0)) || (months == 7 && (daysTemp - 31 > 0)) || (months == 8 && (daysTemp - 31 > 0)) || (months == 10 && (daysTemp - 31 > 0)) || (months == 12 && (daysTemp - 31 > 0))) {
     daysTemp = daysTemp - 31;
     months++;
  } else if (months == 2 && isLeapYear == true && (daysTemp - 29 > 0)) {
     daysTemp = daysTemp - 29;
     months++;
  } else if (months == 2 && isLeapYear == false && (daysTemp - 28 > 0)) {
     daysTemp = daysTemp - 28;
     months++;
  } else if ((months == 4 && (daysTemp - 30 > 0)) || (months == 6 && (daysTemp - 30 > 0)) || (months == 9 && (daysTemp - 30 > 0)) || (months == 11 && (daysTemp - 30 > 0))) {
     daysTemp = daysTemp - 30;
     months++;
  } else {
     counter--;
  }
}

if (months == 1) {
   month = "January";
} else if (months == 2) {
   month = "Febuary";
} else if (months == 3) {
   month = "March";
} else if (months == 4) {
   month = "April";
} else if (months == 5) {
   month = "May";
} else if (months == 6) {
   month = "June";
} else if (months == 7) {
   month = "July";
} else if (months == 8) {
   month = "August";
} else if (months == 9) {
   month = "September";
} else if (months == 10){
   month = "October";
} else if (months == 11){
   month = "November";
} else {
   month = "December";
}
day = daysTemp;


System.out.println("FANCY CLOCK BETA");
System.out.println("Current date: " + month + " " + day + ", " + year);


}
}