tl;dr
To get an integer number representing the day-of-week (1-7 for Monday-Sunday), for the first day of a moment’s year-month:
myGregCal // Avoid troublesome legacy date-time classes such as `GregorianCalendar`.
.toZonedDateTime() // Convert from legacy class to modern java.time class, a `ZonedDateTime` object.
.toLocalDate() // Extract a date-only object.
.with( TemporalAdjusters.firstDayOfMonth() ) // Adjust into first day of the same year-month.
.getDayOfWeek() // Extract a `DayOfWeek` enum object for Monday, Tuesday, etc. Consider using this object directly rather than taking the next step to extract a `int` number.
.getValue() // Extract an `int` integer number, 1-7 for Monday-Sunday.
… yields a number 1-7 for Monday-Sunday.
Tip: Better to skip the last step, and use DayOfWeek
object instead of int
:
myGregCal.toZonedDateTime().toLocalDate().with( TemporalAdjusters.firstDayOfMonth() ).getDayOfWeek() // Render a `DayOfWeek` enum object.
java.time
The Answer by Oleksandr may be correct, and wins points for using the modern java.time classes rather than the legacy date-time classes. But there is a simpler take on such code. Also, the Question appears to want to go a step further, to get the day-of-week for the first-of-month.
Per the comment by Andreas, you could call GregorianCalendar::toZonedDateTime
as a easy way to convert from the old legacy class GregorianCalendar
to the modern ZonedDateTime
class. The old classes gained new methods to facilitate converting between the legacy classes and the modern java.time classes.
ZonedDateTime zdt = myGregCal.toZonedDateTime() ; // Convert from legacy class to modern class.
From there extract a LocalDate
object. The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate ld = zdt.toLocalDate() ;
Adjust the value to the first day of the same month. Use a TemporalAdjuster
implementation found in the TemporalAdjusters
class.
LocalDate firstOfMonth = ld.with( TemporalAdjusters.firstDayOfMonth() ) ;
Determine the day-of-week for that first-of-month date via a DayOfWeek
enum object. That enum pre-defines 7 objects, one for each day of the week: MONDAY, TUESDAY, etc.
DayOfWeek dow = firstOfMonth.getDayOfWeek() ;
Consider using that DayOfWeek
enum object instead of a mere clumsy int
number to represent your day-of-week. Using objects rather than a int
primitive makes your code more self-documenting, provides type-safety, and ensures valid values.
But if you insist, you may extract an int
1-7 for Monday-Sunday per the ISO 8601 standard.
int dowNumber = dow.getValue() ;
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
, GregorianCalendar
, & 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.