I have the following date of type LocalDate
2017-12-21
How do I check if it's a weekend?
I tried to use Calendar
, but it seems to only work with Date
.
I have the following date of type LocalDate
2017-12-21
How do I check if it's a weekend?
I tried to use Calendar
, but it seems to only work with Date
.
Just use getDayOfWeek()
. This returns an element of enum DayOfWeek
:
public static boolean fimDeSemana(LocalDate ld) {
DayOfWeek d = ld.getDayOfWeek();
return d == DayOfWeek.SATURDAY || d == DayOfWeek.SUNDAY;
}
Here's a complete test of it:
import java.time.DayOfWeek;
import java.time.LocalDate;
class Teste {
public static boolean fimDeSemana(LocalDate ld) {
DayOfWeek d = ld.getDayOfWeek();
return d == DayOfWeek.SATURDAY || d == DayOfWeek.SUNDAY;
}
public static void main(String[] args) {
System.out.println(fimDeSemana(LocalDate.of(2017, 12, 19))); // false, terça-feira
System.out.println(fimDeSemana(LocalDate.of(2017, 12, 20))); // false, quarta-feira
System.out.println(fimDeSemana(LocalDate.of(2017, 12, 21))); // false, quinta-feira
System.out.println(fimDeSemana(LocalDate.of(2017, 12, 22))); // false, sexta-feira
System.out.println(fimDeSemana(LocalDate.of(2017, 12, 23))); // true, sábado
System.out.println(fimDeSemana(LocalDate.of(2017, 12, 24))); // true, domingo
System.out.println(fimDeSemana(LocalDate.of(2017, 12, 25))); // false, segunda-feira
System.out.println(fimDeSemana(LocalDate.of(2017, 12, 26))); // false, terça-feira
System.out.println(fimDeSemana(LocalDate.of(2017, 12, 27))); // false, quarta-feira
System.out.println(fimDeSemana(LocalDate.of(2017, 12, 28))); // false, quinta-feira
System.out.println(fimDeSemana(LocalDate.of(2017, 12, 29))); // false, sexta-feira
System.out.println(fimDeSemana(LocalDate.of(2017, 12, 30))); // true, sábado
System.out.println(fimDeSemana(LocalDate.of(2017, 12, 31))); // true, domingo
System.out.println(fimDeSemana(LocalDate.of(2018, 1, 1))); // false, segunda-feira
}
}
Victor's answer is correct, I just wanted to add another alternative.
Instead of a static method, you can also create a java.time.temporal.TemporalQuery
:
// TemporalQuery que verifica se é fim de semana
TemporalQuery<Boolean> fds = t -> {
DayOfWeek dow = DayOfWeek.from(t);
return dow == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY;
};
To use it, just use the query
method:
LocalDate dt = LocalDate.of(2017, 12, 21);
boolean fimDeSemana = dt.query(fds); // false
The great advantage is that a TemporalQuery
can be used with any type that implements java.time.temporal.TemporalAccessor
, such as LocalDateTime
, OffsetDateTime
, and ZonedDateTime
(and any other class that implements TemporalAccessor
one day of the week):
boolean fimDeSemana = ZonedDateTime.now().query(fds);
You can also use it directly with DateTimeFormatter
, for example, if your input is a String
:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/uuuu");
boolean fimDeSemana = fmt.parse("21/12/2017", fds);
Although the static method works perfectly, I think using TemporalQuery
is more flexible, for the reasons above. But of course everything depends on your use cases.
Of course, if the class in question does not have day of the week, an exception will be thrown:
// lança exception porque LocalTime não tem dia da semana
LocalTime.now().query(fds);
I did it this way ...
import java.util.*;
public class Data{
public static void main(String[] args) {
//Instanciando a classe Calendar juntamente com a classe GregorianCalendar
Calendar data = new GregorianCalendar();
//Condicação do que diz qual o dia da semana apartir da data do sistema
switch(data.get(Calendar.DAY_OF_WEEK)){
case 1:
System.out.println("Domingo");
break;
case 2:
System.out.println("Segunda-feira");
break;
case 3:
System.out.println("Terça-feira");
break;
case 4:
System.out.println("Quarta-feira");
break;
case 5:
System.out.println("Quinta-feira");
break;
case 6:
System.out.println("Sexta-feira");
break;
case 7:
System.out.println("Sabado");
break;
}
//COMPLETANDO... CASO SEJA FIM DE SEMANA OU MEIO DE SEMANA...
if ((data.get(Calendar.DAY_OF_WEEK) == 7) || (data.get(Calendar.DAY_OF_WEEK) == 1){
System.out.println("Fim de Semana");
} else {
System.out.println("Dias úteis");
}
System.out.println("\n\nFIM DO PROGRAMA!!!");
}
}
import java.time.*;
public class Data{
public static void main(String[] args) {
LocalDate data = LocalDate.now();
DayOfWeek sabado = DayOfWeek.of(6);
DayOfWeek domingo = DayOfWeek.of(7);
if (data.getDayOfWeek().equals(sabado)){
System.out.println("Fim de Semana!");
} else if(data.getDayOfWeek().equals(domingo)) {
System.out.println("Fim de Semana!");
} else{
System.out.println("Qualquer dia comum da semana!");
}
System.out.println("\n\nFIM DO PROGRAMA!!!");
}
}