How to set the date to next Sunday

Here is the code that would set the date to next Sunday

	public Date setDateToNextSundayZero(Date date) {

		int dayOfWeek = date.getDay();
		int daysToAdd = 7 - dayOfWeek;

		date = DateUtils.setBeginDateFromDate(date, -daysToAdd);

		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		return calendar.getTime();

The solution is simple. Use the day of the week. And finding next Sunday is then easy as shown above.

Leave a Comment