Given a specific date, determining the corresponding day of the week is a common task in programming. This question explores several approaches to retrieve the desired information.
To obtain the full day of the week as a string (e.g., "Tue"), you can utilize the following methods:
Calendar c = Calendar.getInstance(); c.setTime(yourDate); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
String dayOfWeek = new SimpleDateFormat("EE").format(date);
If you prefer to retrieve the day ordinal (e.g., 3 for Tuesday), you can employ the following technique:
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
In the event that your input is provided as a string rather than a Date object, you can parse it using SimpleDateFormat:
Date date = new SimpleDateFormat("dd/M/yyyy").parse(dateString);
The above is the detailed content of How Can I Determine the Day of the Week for a Given Date in Java?. For more information, please follow other related articles on the PHP Chinese website!