Java Day of Month getLastDayOfMonth(Date dt)

Here you can find the source of getLastDayOfMonth(Date dt)

Description

Get last day of month (2007-01-12 = 2007-01-31)

License

Apache License

Parameter

Parameter Description
Date a parameter

Return

Date

Declaration

public static Date getLastDayOfMonth(Date dt) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**/*ww w .j  a v a  2s .  c  o m*/
     * Get last day of month (2007-01-12 = 2007-01-31)
     *
     * @param Date
     * @return Date 
     */
    public static Date getLastDayOfMonth(Date dt) {

        if (null == dt) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dt);

        int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        Calendar rtnCalendar = Calendar.getInstance();
        rtnCalendar.clear();
        rtnCalendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        rtnCalendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        rtnCalendar.set(Calendar.DAY_OF_MONTH, day);

        return rtnCalendar.getTime();
    }
}

Related

  1. getLastDayOfMonth(Date date)
  2. getLastDayOfMonth(Date date)
  3. getLastDayOfMonth(Date date)
  4. getLastDayOfMonth(Date date)
  5. getLastDayOfMonth(Date date)
  6. getLastDayOfMonth(Date firstDate)
  7. getLastDayOfMonth(Date now)
  8. getLastDayOfMonth(Date sDate)
  9. getLastDayOfMonth(final Date date)