Android中Calendar类DAY_OF_WEEK起始值与官方文档描述不符的问题咨询
Ah, I see the confusion here! Let's break down exactly why this discrepancy is happening:
Your code uses the legacy
java.util.Calendarclass
This old date-time API definesDAY_OF_WEEKwith Sunday as the first day of the week. The numbering works like this:- 1 = Sunday
- 2 = Monday
- 3 = Tuesday
- 4 = Wednesday
- 5 = Thursday
- 6 = Friday
- 7 = Saturday
That’s why your test on Thursday returned5—this behavior is completely correct forCalendar.
You’re referencing the wrong class’s documentation
The docs you looked up belong tojava.time.DayOfWeek, part of the modern date-time API introduced in Java 8 (supported in Android API 26+, or via desugaring for older versions). This enum uses a different, ISO-standard numbering where:- 1 = Monday
- 2 = Tuesday
- 3 = Wednesday
- 4 = Thursday
- 5 = Friday
- 6 = Saturday
- 7 = Sunday
Quick Example to Show the Difference
If you switched to the modern API, your code would return 4 for Thursday instead:
import java.time.DayOfWeek; import java.time.LocalDate; // Inside your onCreate method DayOfWeek currentDay = LocalDate.now().getDayOfWeek(); Log.d("currentDayNumber", String.valueOf(currentDay.getValue())); // Outputs 4 for Thursday
The big takeaway here is always double-check which package your date-time class comes from—these two APIs have intentional differences in their day numbering schemes!
内容的提问来源于stack exchange,提问作者Steven

