Java Day of Month getLastDayOfMonth(Date date)

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

Description

get Last Day Of Month

License

Open Source License

Declaration

public static Date getLastDayOfMonth(Date date) 

Method Source Code

//package com.java2s;
/**/*from www.  j  a v a  2  s. co m*/
 * Copyright (c) 2013 M. Alexander Nugent Consulting <i@alexnugent.name>
 *
 * M. Alexander Nugent Consulting Research License Agreement
 * Non-Commercial Academic Use Only
 *
 * This Software is proprietary. By installing, copying, or otherwise using this
 * Software, you agree to be bound by the terms of this license. If you do not agree,
 * do not install, copy, or use the Software. The Software is protected by copyright
 * and other intellectual property laws.
 *
 * You may use the Software for non-commercial academic purpose, subject to the following
 * restrictions. You may copy and use the Software for peer-review and methods verification
 * only. You may not create derivative works of the Software. You may not use or distribute
 * the Software or any derivative works in any form for commercial or non-commercial purposes.
 *
 * Violators will be prosecuted to the full extent of the law.
 *
 * All rights reserved. No warranty, explicit or implicit, provided.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARR?ANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

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

public class Main {
    public static Date getLastDayOfMonth(Date date) {

        date = addMonths(date, 1);
        return getLastDayOfPreviousMonth(date);
    }

    /**
     * Add months to a date. Use - sign to subtract months
     * 
     * @param date
     * @param months
     * @return
     */
    public static Date addMonths(Date date, int months) {

        Calendar lCalendar = Calendar.getInstance();
        lCalendar.setTime(date);
        lCalendar.add(Calendar.MONTH, months);
        return lCalendar.getTime();
    }

    public static Date getLastDayOfPreviousMonth(Date date) {

        Calendar lCalendar = Calendar.getInstance();
        lCalendar.setTime(date);

        lCalendar.set(Calendar.DAY_OF_MONTH, 1);
        Date lTmpDate = lCalendar.getTime();

        return addDays(lTmpDate, -1);
    }

    /**
     * Add days to a date. Use - sign to subtract days
     * 
     * @param date
     * @param days
     * @return
     */
    public static Date addDays(Date date, int days) {

        Calendar lCalendar = Calendar.getInstance();
        lCalendar.setTime(date);
        lCalendar.add(Calendar.DAY_OF_YEAR, days);
        return lCalendar.getTime();
    }
}

Related

  1. getLastDayOfMonth()
  2. getLastDayOfMonth()
  3. getLastDayOfMonth(Calendar c)
  4. getLastDayOfMonth(Date currDate)
  5. getLastDayOfMonth(Date date)
  6. getLastDayOfMonth(Date date)
  7. getLastDayOfMonth(Date date)
  8. getLastDayOfMonth(Date date)
  9. getLastDayOfMonth(Date date)