How to get the last week of the month from the calendar in JAVA

How to get the last week of the month from the calendar in JAVA

+3
source share
2 answers

You can use getActualMaximum(Calendar.DAY_OF_MONTH)to get the last day. Set the calendar day (month) to this value, and then get the day of the week. By working with this, you can figure out the beginning of the "last week", no matter what it means to you (last Sunday to last Monday? Last full week?).

+4
source

If I understood what you meant correctly, something like this would probably do this:

public static int getLastWeekInMonth(int year, int month) {
    Calendar lastDayOfMonth = new GregorianCalendar();
    //Set the date to the day before the 1:st day of the next month
    lastDayOfMonth.set(year, month+1, 0);
    return lastDayOfMonth.get(Calendar.WEEK_OF_YEAR);
}
+2
source

Source: https://habr.com/ru/post/1757147/


All Articles