Schoolwork assignment…
I need to know which day and day name is on a specific date. And somehow I made a mistake because for years with 19.. it works but at 2000 I can't get the right day anymore. For example if I use the date 9.1.2001 it doesn't say me the day, instead the error of the switch I made occurs.
and i shouldn't use the calendar methods
here is my code:
public class FindDay {
    public static void main(String[] args) {
        System.out.println("Type a day: ");
        int day = In.readInt();
        System.out.println("Type a month: ");
        int month = In.readInt();
        System.out.println("Type a year: ");
        int year = In.readInt();
        
        if(day <  1 || day > 32) {
            System.out.println("Type valid day");
        }else if (month < 1 || month >12) {
            System.out.println("Type valid month");
        }else if (year < 1900) {
            System.out.println("Type valid year");
        }
        
        int wholeDaysYear; //to calculate how much days a year has
        
        if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0) ) {
            wholeDaysYear = 366;
        }else {
            wholeDaysYear = 365;
        }
        int monthDays = 0;  //calculates days to this month
        int february;       //if February has 28 or 2 days
        if(wholeDaysYear ==366) {
            february = 29;
        }else {
            february = 28;
        }
        switch(month) { 
        case 1: monthDays= 0; break;
        case 2: monthDays =31; break;
        case 3: monthDays= 31+february; break;
        case 4: monthDays= 31+february+31; break;
        case 5: monthDays= 31+february+31+30; break;
        case 6: monthDays= 31+february+31+30+31; break;
        case 7: monthDays= 31+february+31+30+31+30; break;
        case 8: monthDays= 31+february+31+30+31+30+31; break;
        case 9: monthDays= 31+february+31+30+31+30+31+31; break;
        case 10: monthDays= 31+february+31+30+31+30+31+31+30; break;
        case 11: monthDays= 31+february+31+30+31+30+31+31+30+31; break;
        case 12: monthDays= 31+february+31+30+31+30+31+31+30+31+30; break;
        default: System.out.println("Enter valid month!");break;
        }
        
        int leapYear = ((year-1) / 4 - 474) - ((year-1) / 100 - 18) + ((year-1) / 400 - 4); //calculates the leap years
        int allDays =(((year - leapYear)-1900)*wholeDaysYear)+monthDays+day-1; //Calculates all days
        
        System.out.println(allDays);
        
        int dayName = allDays % 7;
        String dayNames = null; //gives the days their name
        switch (dayName) {
        case 1: dayNames = "Monday";break;
        case 2: dayNames = "Tuesday";break;
        case 3: dayNames = "Wendesday";break;
        case 4: dayNames = "Thursday";break;
        case 5: dayNames = "Friday";break;
        case 6: dayNames = "Saturday";break;
        case 7: dayNames = "Sunday";break;
        default: System.out.println("Type valid Input");break;
        }
        
        System.out.println(dayNames);
    }
}
                You don't have to work so hard... assuming you have the day of the month, month of the year and full-year, you can simply do (example):
    LocalDate ld = LocalDate.of(1999, 12, 12);
    System.out.println(ld.getDayOfWeek()); // prints SUNDAY
LocalDate is available since Java 8.
As for your code, two bugs that are easy to spot are:
int dayName = allDays % 7; here you assume that the first day of the year is Monday (according to your case switch) which is not necessarily true.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With