How to determine day of week by passing specific date?

To determine the day of the week for a specific date in Java, you can use the get() method of the Calendar class. The Calendar class provides a set of methods for manipulating dates and times, and the get() method returns the value for a specific field of the calendar.

Here is an example of how you can use the get() method to determine the day of the week for a specific date:

Calendar calendar = Calendar.getInstance();
calendar.set(2020, Calendar.JANUARY, 1);  // set the date to January 1, 2020

int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);  // dayOfWeek is Calendar.WEDNESDAY

The DAY_OF_WEEK field returns the day of the week as an integer, with Sunday being represented by Calendar.SUNDAY, Monday being represented by Calendar.MONDAY, and so on.

You can use a switch statement or an array to convert the integer value to the name of the day of the week:

String[] daysOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String dayOfWeekName = daysOfWeek[dayOfWeek - Calendar.SUNDAY];  // dayOfWeekName is "Wednesday"

Keep in mind that the Calendar class is a mutable class, which means that its state can be modified. If you need an immutable representation of a date, you can use the LocalDate class from the java.time package.

For example:

LocalDate date = LocalDate.of(2020, 1, 1);  // create a date for January 1, 2020
DayOfWeek dayOfWeek = date.getDayOfWeek();  // dayOfWeek is WEDNESDAY