private void setDateNowHelper(){
long currentTimeMilliseconds = System.currentTimeMillis();
long currentTimeSeconds = 0;
long currentTimeMinutes = 0;
int currentTimeHours = 0;
int currentTimeDays = 0;
int currentTimeMonths = 0;
int currentTimeYears = 0;
calculateTimeNow(currentTimeMilliseconds, currentTimeSeconds, currentTimeMinutes, currentTimeHours, currentTimeDays, currentTimeMonths, currentTimeYears);
this.day = currentTimeDays;
this.month = currentTimeMonths;
this.year = currentTimeYears;
}
private void calculateTimeNow(long milliseconds, long seconds, long minutes, int hours, int days, int months, int years){
while (milliseconds >= 1000) {
milliseconds -= 1000;
seconds++;
if (seconds >= 60) {
seconds -= 60;
minutes++;
}
if (minutes >= 60) {
minutes -= 60;
hours++;
}
if (hours >= 24) {
hours -= 24;
days++;
}
calculateMonthNow(days, months, years);
calculateYearNow(months, years);
}
}
private void calculateMonthNow(int days, int months, int years){
if (isLeapYear(years) && months == 2 && days >= 29){
days -= 29;
months++;
}
if (days >= 31){
switch (months) {
case 1, 3, 5, 7, 8, 10, 12 -> {
days -= 31;
months++;
}
}
}
if (days >= 30){
switch (months) {
case 4, 6, 9, 11 -> {
days -= 30;
months++;
}
}
}
if (days >= 28 && months == 2){
days -= 28;
months++;
}
}
private void calculateYearNow(int months, int years){
if (months > 12){
months -= 12;
years++;
}
}
public boolean isLeapYear(int year){
if (year % 4 == 0 && year % 100 != 0){
return true;
}
else if (year % 100 == 0 && year % 400 == 0){
return true;
}
return false;
}