code: private static Calendar getEndOfDay(Calendar calendar) {
  
          // Copy given calender only up to given month
          Calendar endOfMonth = Calendar.getInstance();
          endOfMonth.clear();
          endOfMonth.set(Calendar.YEAR,         calendar.get(Calendar.YEAR));
          endOfMonth.set(Calendar.MONTH,        calendar.get(Calendar.MONTH));
          endOfMonth.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
  
          // Advance to the last millisecond of the given day
          endOfMonth.add(Calendar.DAY_OF_MONTH, 1);
          endOfMonth.add(Calendar.MILLISECOND, -1);
  
          return endOfMonth;
  
      }
docs: Returns a new calendar representing the last millisecond of the same
  year, month, and day as <code>calendar</code>.
  
  @param calendar
  The calendar defining the year, month, and day whose end
  (last millisecond) is to be returned.
  
  @return
  A new calendar representing the last millisecond of the same year,
  month, and day as <code>calendar</code>.