java.time
I recommend that you use java.time, the modern Java date and time API, for your date work. Arvind Kumar Avinash has already in an answer shown a nice application of it (+1).
The way I understand your question you want Monday to be the first day of the week, and you want to know which number day of the week a certain date is (e.g., July 1, 2013). Monday = 1, Tuesday = 2 and so forth up to Sunday = 7.
LocalDate ld = LocalDate.of(2013, Month.JULY, 1);
// Monday is first day of week; which number day of the week is ld then?
int numberDayOfTheWeek = ld.get(WeekFields.ISO.dayOfWeek()); // 1 through 7
System.out.println(numberDayOfTheWeek);
Output is:
1
Having Monday be the first day of the week agrees with ISO 8601, the international standard for dates and times. So the built-in WeekFields.ISO
uses this numbering. WeekFields.ISO.dayOfWeek()
gives us a TemporalField
that we then use for querying the LocalDate
object about its day of the week.
Instead of WeekFields.ISO
we may use WeekFields.of(DayOfWeek.MONDAY, 1)
to specify that Monday is the first day of the week. The second argument to of()
is the minimal number of days in the first week (of the year or month) form 1 through 7, which we don’t use here, so it doesn’t matter which number we use.
Link