Get the end of a month


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

public class Main {
  public static void main(String[] argv) {
    System.out.println(getEndOfMonth(new Date()));
  }

  public static Date getEndOfMonth(Date day) {
    return getEndOfMonth(day, Calendar.getInstance());
  }

  public static Date getEndOfMonth(Date day, Calendar cal) {
    if (day == null)
      day = new Date();
    cal.setTime(day);

    // set time to end of day
    cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
    cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));

    // set time to first day of month
    cal.set(Calendar.DAY_OF_MONTH, 1);

    // add one month
    cal.add(Calendar.MONTH, 1);

    // back up one day
    cal.add(Calendar.DAY_OF_MONTH, -1);

    return cal.getTime();
  }
}
Home 
  Java Book 
    Runnable examples  

Date Get:
  1. Day of Week
  2. Number of days in a month
  3. Month of year
  4. Current month name
  5. Full date time
  6. Get all attributes from a Calendar
  7. Get year, month, day, minute, hour, second, milli second from java.util.Date through Calendar
  8. Get age from a birthday date
  9. Get Monday
  10. Get next Sunday
  11. Get today's date
  12. Get the last day of a month
  13. Get date of yesterday
  14. Get Day-of-Week for a Particular Date
  15. Get date of last week
  16. Get date of last month
  17. Get last Date of This Month
  18. Get Month in a leap year
  19. Get start of a month
  20. Get the end of a month
  21. Get noon of a day
  22. Get days since a date
  23. Get TimeZone
  24. Get Last day fo previous Month
  25. Is an hour between an interval
  26. Is a Year a Leap Year