Java Month End endOfMonth(Date date)

Here you can find the source of endOfMonth(Date date)

Description

Returns the final day of the given month of the given year.

License

Open Source License

Parameter

Parameter Description
date a parameter

Declaration

public static Date endOfMonth(Date date) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;

import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**//from   ww w . j a v a 2  s. c  om
     * Returns the final day of the given month of the given year. Takes month
     * and year parameters as integers.
     * 
     * @param month
     * @param year
     * @return
     */
    public static Date endOfMonth(int month, int year) {
        Calendar cal = new GregorianCalendar();
        cal.set(year, month - 1, 1);
        int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, maxDay);
        return cal.getTime();
    }

    /**
     * Returns the final day of the given month of the given year. Takes date
     * object as parameter.
     * 
     * @param date
     * @return
     */
    public static Date endOfMonth(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, maxDay);
        return cal.getTime();
    }
}

Related

  1. endOfMonth(Date date)
  2. getEndOfMonth(Date date)
  3. getEndOfMonth(Date dt)
  4. getEndOfMonth(long date)