Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the first Monday of a month

Tags:

java

I want to get the day on which the first Monday of a specific month/year will be.

What I have:

I basically have two int variables, one representing the year and one representing the month.

What I want to have:

I want to know the first Monday in this month, preferably as an int or Integer value.

For example:

I have 2014 and 1 (January), the first Monday in this month was the 6th, so I want to return 6.

Problems:

I thought I could do that with the Calendar but I am already having trouble setting up the calendar with only Year and Month available. Furthermore, I'm not sure how to actually return the first Monday of the month/year with Calendar.

I already tried this:

Calendar cal = Calendar.getInstance();
cal.set(this.getYear(),getMonth(), 1);
int montag = cal.getFirstDayOfWeek();
for( int j = 0; j < 7; j++ ) {
    int calc = j - montag;
    if( calc < 0 ) {
        calc += 6;
    }
    weekDays[calc].setText(getDayString(calc));
}
like image 556
Jakob Abfalter Avatar asked May 31 '14 14:05

Jakob Abfalter


People also ask

How do I get the first Monday of the month in Google Sheets?

If the first date of the month is a Monday, then output that date. Else, check if the first date of the month is less than Monday, if yes then add 1 to get the first Monday. Else if the first date of the month is greater than Monday then add (7-(current day -2)) to get the first Monday of month in google sheets.


2 Answers

Java.time

Use java.time library built into Java 8 and TemporalAdjuster. See Tutorial.

import java.time.DayOfWeek;
import java.time.LocalDate;
import static java.time.temporal.TemporalAdjusters.firstInMonth;

LocalDate now = LocalDate.now(); //2015-11-23
LocalDate firstMonday = now.with(firstInMonth(DayOfWeek.MONDAY)); //2015-11-02 (Monday)

If you need to add time information, you may use any available LocalDate to LocalDateTime conversion like

firstMonday.atStartOfDay() # 2015-11-02T00:00
like image 143
Przemek Avatar answered Sep 20 '22 16:09

Przemek


getFirstDayOfWeek() returns which day is used as the start for the current locale. Some people consider the first day Monday, others Sunday, etc.

This looks like you'll have to just set it for DAY_OF_WEEK = MONDAY and DAY_OF_WEEK_IN_MONTH = 1 as that'll give you the first Monday of the month. To do the same for the year, first set the MONTH value to JANUARY then repeat the above.

Example:

private static Calendar cacheCalendar;

public static int getFirstMonday(int year, int month) {
    cacheCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    cacheCalendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
    cacheCalendar.set(Calendar.MONTH, month);
    cacheCalendar.set(Calendar.YEAR, year);
    return cacheCalendar.get(Calendar.DATE);
}

public static int getFirstMonday(int year) {
    return getFirstMonday(year, Calendar.JANUARY);
}

Here's a simple JUnit that tests it: http://pastebin.com/YpFUkjQG

like image 24
Octavia Togami Avatar answered Sep 19 '22 16:09

Octavia Togami