Please help! I'm trying to create a code to what day Thanksgiving is on in any given year, pls tell me the equation to get the day on any given year

//this is my code so far but I am stuck, idk what to do with the limited commands that I can use such as while, if, else if, and int.

import java.util.*;

class Main {
public static void main(String[] args) {
Scanner getInput = new Scanner(System.in);
System.out.println(“Welcome to the Thanksgiving Day Calculator.”);
System.out.println(“Enter any given year to find out what day of November Thanksgiving Day will be on.”);
boolean validInput = false;

int Year;
int Month = 11;
Year = getInput.nextInt();

while (validInput == false) {
  if (Year >0) {
    int Day;
    Day = 
    System.out.println("Thanksgiving Day starts on" + Day + "for the year "+ Year.); 
    
    
  }else{
    System.out.println("Please enter a valid year!");
    Year = getInput.nextInt();
  }

}

}
}

Hi @michaelkhang

I’m assuming as the code you’ve provided is in Java you’re looking for a java solution. What your asking for is a perfect application for the new java date/time API in the java.time.* package. I’d recommend having a look into that, specifically what you can achieve with the TemporalAdjusters class.

If you’re stuck, here’s a little implementation to help you out:

public class DayOfThanksGiving {

	public static void main(String[] args) {
		System.out.println(whatDayIsThanksGiving(2018));
	}
	
	public static int whatDayIsThanksGiving(int year) {
		// Get the first of november for a given year
		LocalDate firstNov = LocalDate.of(year, 11, 1);
		
		// If the current day is thursday then add 3 weeks and return the day
		if (firstNov.getDayOfWeek().equals(DayOfWeek.THURSDAY)) {
			return firstNov.plusWeeks(3).getDayOfMonth();
		// Otherwise move to the next thursday then add 3 weeks and return the day
		} else {
			return firstNov.with(TemporalAdjusters.next(DayOfWeek.THURSDAY)).plusWeeks(3).getDayOfMonth();
		}
	}

}
1 Like

Thank you for helping me. I didn’t copy any of your work because the code is too complicated and I didn’t understand what the commands did. I managed to create my own program using just variables, conditionals, and loops to determine the day of Thanksgiving by finding the starting weekday of November.

My code isn’t complicated and it doesn’t have functions. I just kept it simple and used what I know.

import java.util.*;

class Main {
  public static void main(String[] args) {
    Scanner getInput = new Scanner(System.in);
    System.out.println("Welcome to the Thanksgiving Day Determiner");
    System.out.println("Enter any given year to find out what day of November Thanksgiving Day will be on.");
    boolean validInput = false;
    
    int Year;
    int Month = 11;
    Year = getInput.nextInt();
    
    while (validInput == false) {
      if (Year >0) {
        int startNovember;
        System.out.println("Please input which day of the week November starts on for the year " + Year + ". If november starts on Sunday please input 1, Monday is 2, Tuesday is 3, Wednesday is 4, Thursday is 5, Friday is 6, and Saturday is 7.");
        startNovember = getInput.nextInt();
          
          if (startNovember == 1) {
            int thanksgivingDay;
            thanksgivingDay = 22;
            System.out.println("Thanksgiving day starts on the " + thanksgivingDay + " of November for the year " + Year + ".");
            System.exit(0);
          
          }if (startNovember == 2) {
            int thanksgivingDay;
            thanksgivingDay = 23;
            System.out.println("Thanksgiving day starts on the " + thanksgivingDay + " of November for the year " + Year + ".");
            System.exit(0);

          }if (startNovember == 3) {
            int thanksgivingDay;
            thanksgivingDay = 24;
            System.out.println("Thanksgiving day starts on the " + thanksgivingDay +" of November for the year " + Year + ".");
            System.exit(0);            
          
          }if (startNovember == 4) {
            int thanksgivingDay;
            thanksgivingDay = 25;
            System.out.println("Thanksgiving day starts on the " + thanksgivingDay + " of November for the year " + Year + ".");
            System.exit(0);
      
          }if (startNovember == 5) {
            int thanksgivingDay;
            thanksgivingDay = 26;
            System.out.println("Thanksgiving day starts on the " + thanksgivingDay + " of November for the year " + Year + ".");
            System.exit(0);
            
          }if (startNovember == 6) {
            int thanksgivingDay;
            thanksgivingDay = 27;
            System.out.println("Thanksgiving day starts on the " + thanksgivingDay + " of November for the year " + Year + ".");
            System.exit(0);
          
          }if (startNovember == 7) {
            int thanksgivingDay;
            thanksgivingDay = 28;
            System.out.println("Thanksgiving day starts on the " + thanksgivingDay +" of November for the year " + Year + ".");
            System.exit(0);
          }else {
            System.out.println("Please check what you entered and read the instructions again:");
          }
      }else{
        System.out.println("Please enter a valid year, the year can be any year that is existing! (From 0 and onwards!)");
        Year = getInput.nextInt();
      }
    
    }
  }
}

But for 2019 the first week has two day and on the calendar it doesn’t count so it comes with the wrong date. So can you please help me in javascript.