Zeeshan
Zeeshan

Reputation: 1183

How to get Last Week of Month from Calendar in JAVA

How to get Last Week of Month from Calendar in JAVA

Upvotes: 2

Views: 4927

Answers (2)

Alderath
Alderath

Reputation: 3874

If I understood what you meant correctly, something along the lines of this would probably do it:

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);
}

Upvotes: 2

Pointy
Pointy

Reputation: 413986

You can use getActualMaximum(Calendar.DAY_OF_MONTH) to get the last day. Set the calendar day (of month) to that value and then get the day of week. Working from that, you can figure out the start of the "last week", whatever that means to you (last Sunday? last Monday? last complete week?).

Upvotes: 4

Related Questions