private static int[] daysInMonth = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31};
I'm looking for a way to make it that if the year % 4 == 0 then daysInMonth[1] = 29
CodePudding user response:
While the question is not very clear, I'll attempt an answer.
What you seem to be asking is that when you access daysInMonth[1], you need it to be dynamic based on the year you are working with.
If you wish to use daysInMonth[1], then it's not possible without using lamdas or method references as the array instead of integer.
What I would instead recommend is that you access the values via an accessor method, something like this:
public int daysOfMonth(int year, int month) {
if (month == 1 && year % 4 == 0) {
return 29;
}
return daysInMonth[1];
}
CodePudding user response:
You can check if it's a leap year. If it is, the Feb has 29 days.
private static int[] daysInMonth = {31 , LocalDate.now().isLeapYear() ? 29:28 ,
31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31};