1

I run this test application

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    int currentDayNumber = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);

    Log.d("currentDayNumber",String.valueOf(currentDayNumber));

}

The day on which this test was run was a Thursday, and "currentDayNumber" gave 5, which means that "DAY_OF_WEEK" (days of week numbered from 1 to 7) starts on Sunday.

This is contrary to what the Android Developer pages say (https://developer.android.com/reference/java/time/DayOfWeek), that by default the number 1 of DAY_OF_WEEK is set to Monday.

Any explanation for this discrepancy?

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Steven
  • 289
  • 2
  • 5
  • 14

1 Answers1

3

Your documentation link points to java.time.DayOfWeek, which is indeed Monday-based. But your code appears to be using java.util.Calendar which is Sunday-based. You can use LocalDate.now().getDayOfWeek() to get a java.time.DayOfWeek for the current time from the system clock in the default time-zone.

Lauri Piispanen
  • 2,067
  • 12
  • 20
  • Very thoughtful to make different dating systems that use the same verbal reference ("Day Of Week") within java start on different days. To keep developers on their toes, I suppose. Thanks for pointing this out. – Steven Sep 09 '21 at 08:32
  • @Evenness to be fair, Sunday is considered to be the first day of the week in a big chunk of the world, America included. – QBrute Sep 09 '21 at 08:59
  • @Evenness yes it can be a bit confusing... The Calendar API dates all the way back to JDK 1.1, which was released back in 1997. Time API (JSR 310) is much newer and was introduced in Java 8 in 2014. The change of weekday bias is due to Time API being built on ISO 8601 standard, where weekdays run from Monday to Sunday. – Lauri Piispanen Sep 09 '21 at 11:06
  • @Evenness This seems to happen a lot. In `Date` years were 1900-based, in `GregorianCalendar` they follow human numbers. In `GregorianCalendar` months are 0-based (contrary to human numbering), in java.time they are 1-based. I generally find it good that they put something better instead of the old confusing classes. No one should use `Calendar` and `GregorianCalendar` anymore anyway. – Ole V.V. Sep 09 '21 at 14:16