Here you can find the source of getEndOfMonth(Date dt)
public static Date getEndOfMonth(Date dt)
//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(); } }