You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中Calendar类DAY_OF_WEEK起始值与官方文档描述不符的问题咨询

Why the DAY_OF_WEEK Value Doesn't Match the Documentation

Ah, I see the confusion here! Let's break down exactly why this discrepancy is happening:

  • Your code uses the legacy java.util.Calendar class
    This old date-time API defines DAY_OF_WEEK with 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 returned 5—this behavior is completely correct for Calendar.
  • You’re referencing the wrong class’s documentation
    The docs you looked up belong to java.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 11:39:08