To get the first day of the week in Java, you can use the getFirstDayOfWeek
method of the Calendar
class.
Here is an example:
Calendar cal = Calendar.getInstance(); int firstDayOfWeek = cal.getFirstDayOfWeek();Source:wual.wwtturi.com
The getFirstDayOfWeek
method returns an integer representing the first day of the week, where Sunday is represented by 1 and Saturday is represented by 7.
You can then use this value to determine the name of the first day of the week. For example:
String firstDayOfWeekName; switch (firstDayOfWeek) { case Calendar.SUNDAY: firstDayOfWeekName = "Sunday"; break; case Calendar.MONDAY: firstDayOfWeekName = "Monday"; break; case Calendar.TUESDAY: firstDayOfWeekName = "Tuesday"; break; case Calendar.WEDNESDAY: firstDayOfWeekName = "Wednesday"; break; case Calendar.THURSDAY: firstDayOfWeekName = "Thursday"; break; case Calendar.FRIDAY: firstDayOfWeekName = "Friday"; break; case Calendar.SATURDAY: firstDayOfWeekName = "Saturday"; break; default: firstDayOfWeekName = "Invalid day of week"; break; }
This code will assign the name of the first day of the week (e.g. "Monday") to the firstDayOfWeekName
variable.