# 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{
Year = getInput.nextInt();
}

}
``````

}
}

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://freecodecamp.org.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

Please provide some example of what you’ve tried and I’m sure you’ll get more help. If you are unsure of what the code should look like, then write out your algorithm (steps) of how you would go about figuring out how to capture the 4th Thursday of November of a given year.

Happy coding

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.

``````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 {
}
}

}
``````
1 Like

Since you were already given a solution, in case anybody needs a JavaScript version of this function, it could look like:

``````function thanksgivingDay(theYear) {
var nov1stDay = new Date(theYear, 10, 1, 0, 0, 0, 0);
var dayOfWeek = nov1stDay.getDay();
var tgDayOfMonth =  nov1stDay.getDate() - (dayOfWeek - 4) + 21;
return `Thanksgiving Day is on November \${tgDayOfMonth} for the year \${theYear}.`;
}
thanksgivingDay(2018); // 'Thanksgiving Day is on November 22 for the year 2018.'
``````
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.

Well, please share your final solution, so we can see how you did it.

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 {
}
}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();
}

}
}
}
``````

Thanks for sharing.

In the future, when you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

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.