I am learning about the use of LocalDate, and I want to compare the day of the week so that when it returns SATURDAY or SUNDAY I can perform other calculations
I am trying the following:
LocalDate fechaS = LocalDate.of(dia, mes, anio);
if(fechaS.getDayOfWeek() == SATURDAY || fechaS.getDayOfWeek() == SUNDAY){
int diaAux = fechaS.getDayOfMonth();
}
also try
if(fechaS.getDayOfWeek() == 6 || fechaS.getDayOfWeek() == 0){
}
but I get "illegal start of expression"
can you tell me how it can be compared, thanks
The getDayOfWeek() method of the LocalDate class returns a DayOfWeek enum . Being an enumerable, you can use the equals method or a switch to execute code based on its value.
Using equals :
Using switch :
The first thing to tell you is that it
LocalDate.of()
receives as a parameter, first the year, then the month and finally the day. This is so that you take it into account and change the order of your variablesdia
,mes
anio
.Then to compare if the day of the week is equal to a specific day of the week, you can use
equals()
andSATURDAY
-SUNDAY
must go between""
.Full code:
I hope you find it useful!