tl;dr
Legacy way to determine day-of-week number defined ISO 8601 (Monday-Sunday, 1-7):
int dayOfWeekISO = cal.get( Calendar.DAY_OF_WEEK ) - 1 ;
if( dayOfWeekISO == 0 ) { // For Sunday ( 1 - 1 = 0), add 7.
dayOfWeekISO = ( dayOfWeekISO + 1 ) ;
}
Modern way:
LocalDate.of( 2017 , Month.JULY , 30 ) // Date-only value.
.getDayOfWeek() // `DayOfWeek.SUNDAY` enum object.
.getValue() // 7 for Sunday. (1 = Monday, per ISO 8601 standard.)
7
Details
The accepted answer by Maia is correct: Those int
constants on Calendar
define the week as 1-7 for Sunday-Saturday, per the custom in the United States.
DAY_OF_WEEK
public static final int DAY_OF_WEEK
Field number for get and set indicating the day of the week. This field takes values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.
If you absolutely cannot add a library, then you must adjust from the Calendar
definition of a week being Sunday-Saturday 1-7. To put Money in the first place, subtract 1, then check for 0 to which you add 7.
int dayOfWeekISO = cal.get( Calendar.DAY_OF_WEEK ) - 1 ;
if( dayOfWeekISO == 0 ) { // For Sunday ( 1 - 1 = 0), add 7.
dayOfWeekISO = ( dayOfWeekISO + 1 ) ;
}
By the way to go the other way, from ISO 8601 style to US-style (Sunday first), calculate modulo 7 then plus 1.
( DayOfWeek.SUNDAY.getValue() % 7 ) + 1
Like this:
System.out.println( ( DayOfWeek.SUNDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.MONDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.TUESDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.WEDNESDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.THURSDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.FRIDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.SATURDAY.getValue() % 7 ) + 1 ) ;
1
2
3
4
5
6
7
But do consider adding a library of modern date-time classes. Well worth the bother of adding a jar to your project. Those old date-time classes really are wretched.
Avoid legacy date-time classes
The Calendar
class is part of the troublesome old legacy date-time classes, now supplanted by java.time classes.
java.time
The java.time.DayOfWeek
enum is standard, representing a week defined by ISO 8601 where a week runs from Monday to Sunday, numbered 1-7.
The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate ld = LocalDate.of( 2017 , Month.JULY , 30 ) ;
DayOfWeek dow = ld.getDayOfWeek() ;
int dowNumber = dow.getValue() ;
String output = dow.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ;
See this code run live at IdeOne.com.
ld.toString(): 2017-07-30
dow.toString(): SUNDAY
dowNumber: 7
output: Sunday
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.