DateUtils { public static Date getLastDayOfYear(Date date) { Calendar lastDayOfYear = Calendar.getInstance(); lastDayOfYear.setTime(DateUtils.getLastDayOfMonth(date)); lastDayOfYear.set(Calendar.MONTH, lastDayOfYear.getMaximum(Calendar.MONTH)); return lastDayOfYear.getTime(); }  static Date timeToDate(int hour, int minutes); static Date minHourInDate(Date d); static Date addDays2Date(Date d, int days); static Date maxHourInDate(Date d); static Date getFirstDayOfMonth(Date date); static Date getLastDayOfMonth(Date date); static Date getFirstDayOfYear(Date date); static Date getLastDayOfYear(Date date);  }
@Test public void shouldReturnLastDayOfYear() { final Date testDate = new GregorianCalendar(2019, Calendar.JULY, 17).getTime(); final Date result = DateUtils.getLastDayOfYear(testDate); Date expectedDate = new GregorianCalendar(2019, Calendar.DECEMBER, 31, 23, 59, 59).getTime(); Calendar calendar = Calendar.getInstance(); calendar.setTime(expectedDate); calendar.set(Calendar.MILLISECOND, calendar.getMaximum(Calendar.MILLISECOND)); expectedDate = calendar.getTime(); assertThat(result, is(expectedDate)); }