this is probably a very unique question because I have searched on Google for a while & could not find a solution. It is also interesting.
What I need bascailly is a number, which you can get the day name by it.
For example our number is 7, we need to go through a formula to find out it's day name, but for 7 it's basic, all you do is just get "saturday" in the switch statement.
Basically it's simple to get the name, you just done:
switch (dayNumber) {
case 1: return "Sunday"; etc...
But my question is pretty complicated I think, and I am not sure if logically there is a possible solution for it.
I want to get the index number of the week, by the number.
examples:
- day 7 - Index: 7
- day 16 - Index: 2 (because you count the first 2 weeks 7 + 7 = 14 and then + 2 = 16 so the day is monday.
- day 24 - index: 3
Why do I need this:
Well I had a question recently in a java course, not related to this, but it has something to do with looping through the month days, so I wanted to be creative & even get the name of the specific day, in a quick way using Mathematics functions and formulas.
So example on how I want to get the name:
for (int i = 1; i <= 31; i++) {
System.out.println(getDayByInteger(i));
}
public static String getDayByInteger(int day) {
int dayNumber = 0; //TODO Formula..
switch (dayNumber) {
case 1:
return "Sunday";
case 2:
return "Monday";
case 3:
return "Tuesday";
case 4:
return "Wednesday";
case 5:
return "Thursday";
case 6:
return "Friday";
case 7:
return "Saturday";
default: return "N/A";
}
}
I don't mind if it only will support the number range 1-31, but my question focuses on if it's possible to make it work with any number, because you can always build a formula to work for a specific numbers range I think.
Is it even possible for any number? Or will it require checking statements?