Java Day of Month getLastdayOfMonth(int month, int year)

Here you can find the source of getLastdayOfMonth(int month, int year)

Description

get Lastday Of Month

License

Open Source License

Declaration

public static Calendar getLastdayOfMonth(int month, int year) 

Method Source Code

//package com.java2s;
/*// w w  w . ja v a2  s. c  om
 * @ (#) CalendarUtils.java
 * 
 * Copyright (c) 2010 ClickDiagnostics Inc. All Rights Reserved. This software is the
 * confidential and proprietary information of ClickDiagnostics ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with ClickDiagnostics.
 */

import java.util.Calendar;

public class Main {
    public static Calendar getLastdayOfMonth(int month, int year) {
        Calendar cal = Calendar.getInstance();

        int days = 30;
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            days = 31;
        } else if (month == 2) {
            // need to calculate leap year
            days = 28;
        }
        cal.set(Calendar.DAY_OF_MONTH, days);
        cal.set(Calendar.MONTH, month - 1); // In calendar, Month start from
        // 0
        // Not in 1
        cal.set(Calendar.YEAR, year);
        return cal;
    }
}

Related

  1. getLastDayOfMonth(Date dt)
  2. getLastDayOfMonth(Date firstDate)
  3. getLastDayOfMonth(Date now)
  4. getLastDayOfMonth(Date sDate)
  5. getLastDayOfMonth(final Date date)
  6. getLastDayOfMonth(int year, int month)
  7. getLastDayOfMonth(int year, int month)
  8. getLastDayOfMonth(int year, int month)
  9. getLastDayOfMonth(int year, int month)