We would like to know how to check whether the given date represents a date at the weekend (Saturday or Sunday).
// w w w . ja va 2 s.c o m import java.time.DayOfWeek; import java.time.LocalDate; import java.time.temporal.ChronoField; public class Main { public static void main(String[] argv) { System.out.println(isWeekend(LocalDate.now())); } /** * This method checks whether the given date object is representing a date at * the weekend (Saturday or Sunday) * * @param date * Date to check, cannot be null * @return TRUE is Saturday or Sunday */ public static boolean isWeekend(LocalDate date) { DayOfWeek dayOfWeek = DayOfWeek.of(date.get(ChronoField.DAY_OF_WEEK)); switch (dayOfWeek) { case SATURDAY: case SUNDAY: return true; default: return false; } } }
The code above generates the following result.