Search code examples
javadayofweekdatedayofmonth

Is there a function to provide the first day of the month?


In Java I can get the first day of the month by this way:

GregorianCalendar date;
// Some code
int first = new GregorianCalendar(date.get(Calendar.YEAR),
            date.get(Calendar.MONTH), 1).get(Calendar.DAY_OF_WEEK);

I'm wondering if there a function to do that directly, because I can't find a function that grabs this vital information.

Something similar to what I'm trying to do here:

 for (int i = 1; i < days_in_month + first; i++) {
     // Some code
     if (i - first + 1 == date.get(Calendar.DAY_OF_MONTH)) {
         // Do something
     }
 }

Solution

  • Simply put, no. This is the correct way to get something like this.

    This is literally the functionality they put in Java for making calculations like this possible. Any sort of "direct function" would simply do this internally (which I suggest you do.)