Java Month End getEndOfMonth(Date dt)

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

Description

Return the End (last day of year) of the given Date Hour, minutes, seconds are set to 23:59:59

License

Open Source License

Declaration

public static Date getEndOfMonth(Date dt) 

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 {
    /** Return the End (last day of year) of the given Date
     *  Hour, minutes, seconds are set to 23:59:59
     * @return//  w  ww  .  j a  v a2s . c  o  m
     */
    public static Date getEndOfMonth(Date dt) {
        if (dt == null)
            return null;
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(getStartOfMonth(dt));
        // Add a month
        gc.add(Calendar.MONTH, 1);
        // Take a day for last of of month
        gc.add(Calendar.DATE, -1);
        gc.set(Calendar.HOUR_OF_DAY, 23);
        gc.set(Calendar.MINUTE, 59);
        gc.set(Calendar.SECOND, 59);
        gc.set(Calendar.MILLISECOND, 999);
        return gc.getTime();
    }

    /**
     * Return the first day of the month from the Date given Hour, minutes,
     * seconds are set to 0:00:00
     * 
     * @return
     */
    public static Date getStartOfMonth(Date dt) {
        if (dt == null)
            return null;
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(dt);
        gc.set(Calendar.DAY_OF_MONTH, 1);
        gc.set(Calendar.HOUR_OF_DAY, 0);
        gc.set(Calendar.MINUTE, 0);
        gc.set(Calendar.SECOND, 0);
        gc.set(Calendar.MILLISECOND, 0);
        return gc.getTime();
    }
}

Related

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