DateUtil { public static Date getYrMonthLastDay(final Date d) { Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.add(Calendar.MONTH, 1); Date nextMonthFirstDay = DateUtil.getYrMonthFirstDay(calendar.getTime()); calendar.setTime(nextMonthFirstDay); calendar.add(Calendar.DAY_OF_MONTH, -1); return calendar.getTime(); }  static Date string2Date(final String ds); static Date string2Date(final String date, final String format); static String date2String(final Date date); static String date2String(final Date date, final String format); static String getCurrentTimestamp(); static long getCurrentTime(); static Date getCurrentDate(); static int daysBetween(final Date startDate); static Date getYrMonthLastDay(final Date d); static Date getYrMonthFirstDay(final Date d); static int daysBetween(final Date startDate, final Date endDate);  }
@Test public void getYrMonthLastDay() { Date t1 = DateUtil.string2Date("2018-02-20"); Date t2 = DateUtil.getYrMonthLastDay(t1); String str = DateUtil.date2String(t2, DateConstants.DATE_FORMAT_10); Assert.equals(str, "2018-02-28", ErrorCodeDef.SYSTEM_ERROR_10001); }