mam problem z zadaniem, mianowicie muszę dodać do klasy metodę zwracającą nazwę dnia tygodnia dla podanej daty. Trzeba wykonać to na dwa sposoby, za pomocy metod przesuwania o tydzień (z porównaniem do wybranej daty referencyjnej np. 14.05.2023 jest niedziela) oraz za pomocą wyznaczania z wykorzystaniem operacji modulo (https://en.wikipedia.org/w/index.php?title=Zeller%27s_congruence&oldid=1033222037). mój kod wskazuje błędne wyniki z wykorzystaniem obu metod, nie rozumiem dlaczego. np. przy podaniu 17.03.2030 metoda z referencja wyświetla środę, a z algorytmem Zellera piątek, kiedy prawidłowy wynik to niedziela. proszę o pomoc w znalezieniu błędu.
import java.util.Scanner;
import java.util.Comparator;
class DateComparator implements Comparator<Date>{
@Override
public int compare(Date date1, Date date2){
int yearComparison = Integer.compare(date1.getYear(), date2.getYear());
if( yearComparison != 0){
return yearComparison;
}
int monthComparison = Integer.compare(date1.getMonth(), date2.getMonth());
if(monthComparison != 0){
return monthComparison;
}
return Integer.compare(date1.getDay(), date2.getDay());
}
}
class Month{
private int monthNumber;
private String name;
private int days;
public Month(int monthNumber, String name, int days){
this.monthNumber = monthNumber;
this.name = name;
this.days = days;
}
public int getMonthNumber() {
return monthNumber;
}
public int getDays() {
return days;
}
public void setDays(int days){
this.days = days;
}
}
public class Date {
Months monthsObj = new Months();
private int year;
private int month;
private int day;
public Date() {
}
public Date(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
public int getYear(){
return year;
}
public void setYear(int year){
this.year = year;
}
public int getMonth(){
return month;
}
public void setMonth(int month){
this.month = month;
}
public int getDay(){
return day;
}
public void setDay(int day){
this.day = day;
}
public void printDate(){
System.out.println(year + "-" + month + "-" + day);
}
public void incrementWeek(){
int daysInMonth = monthsObj.getDaysInMonth(year, month);
day += 7;
if(day > daysInMonth){
day -= daysInMonth;
month++;
if(month > 12){
year++;
month = 1;
}
}
}
public void decrementWeek(){
int daysInPreviousMonth;
int daysToRemove = 8 - day;
if(daysToRemove <= 0){
day -= 7;
}else{
month--;
if(month == 0){
month = 12;
year--;
}
daysInPreviousMonth = monthsObj.getDaysInMonth(year, month);
day = daysInPreviousMonth + 1 - daysToRemove;
}
}
public void whatDay() {
int h = (day + 13 * (month + 1) / 5 + year % 100 + (year % 100) / 4 + (year / 100) / 4 - 2 * (year / 100)) % 7;
String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
System.out.println(days[h]);
}
public void moveWeek(Date reference, Date calendar, int result){
String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
if(result > 0){
while((reference.getYear() != calendar.getYear()) || (reference.getMonth() != calendar.getMonth()) || (reference.getDay()-calendar.getDay() >= 7)){
calendar.incrementWeek();
calendar.printDate();
}
}
if(result < 0){
while((reference.getYear() != calendar.getYear()) || (reference.getMonth() != calendar.getMonth()) || ((reference.getDay()-calendar.getDay() >= 7)||(reference.getDay()- calendar.getDay()<0))){
calendar.decrementWeek();
calendar.printDate();
}
}
int difference = reference.getDay() - calendar.getDay();
System.out.println(days[6-difference]);
}
public static void main(String[] args) {
DateComparator comparator = new DateComparator();
Scanner sc = new Scanner(System.in);
System.out.println("podaj dzien, miesiac i rok:");
int day = sc.nextInt();
int month = sc.nextInt();
int year = sc.nextInt();
Date reference = new Date(2023, 5, 14);
Date calendar = new Date(year, month, day);
calendar.moveWeek(reference, calendar, comparator.compare(reference, calendar));
calendar.whatDay();
}
}
class Months{
private static final Month[] months = {
new Month(1, "January", 31),
new Month(2, "February", 28),
new Month(3, "March", 31),
new Month(4, "April", 30),
new Month(5, "May", 31),
new Month(6, "June", 30),
new Month(7, "July", 31),
new Month(8, "August", 31),
new Month(9, "September", 30),
new Month(10, "October", 31),
new Month(11, "November", 30),
new Month(12, "December", 31)
};
public boolean isLeapYear(int year){
return (year % 4 == 0 && year % 100 !=0) || year % 400 == 0;
}
public int getDaysInMonth(int year, int month){
if(isLeapYear(year) && month == 2){
months[1].setDays(29);
}
return months[month-1].getDays();
}
}