Get next Sunday


import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] argv) {
    System.out.println(getSunday(new Date()));
  }

  public static Date getSunday(Date today) {
    Calendar cal = Calendar.getInstance();

    cal.setTime(today);

    int dow = cal.get(Calendar.DAY_OF_WEEK);

    while (dow != Calendar.SUNDAY) {
      int date = cal.get(Calendar.DATE);

      int month = cal.get(Calendar.MONTH);

      int year = cal.get(Calendar.YEAR);

      if (date == getMonthLastDate(month, year)) {

        if (month == Calendar.DECEMBER) {
          month = Calendar.JANUARY;

          cal.set(Calendar.YEAR, year + 1);
        } else {
          month++;
        }

        cal.set(Calendar.MONTH, month);

        date = 1;
      } else {
        date++;
      }

      cal.set(Calendar.DATE, date);

      dow = cal.get(Calendar.DAY_OF_WEEK);
    }

    return cal.getTime();
  }

  private static int getMonthLastDate(int month, int year) {
    switch (month) {
    case Calendar.JANUARY:
    case Calendar.MARCH:
    case Calendar.MAY:
    case Calendar.JULY:
    case Calendar.AUGUST:
    case Calendar.OCTOBER:
    case Calendar.DECEMBER:
      return 31;

    case Calendar.APRIL:
    case Calendar.JUNE:
    case Calendar.SEPTEMBER:
    case Calendar.NOVEMBER:
      return 30;

    default: // Calendar.FEBRUARY
      return year % 4 == 0 ? 29 : 28;
    }
  }
}
Home 
  Java Book 
    Runnable examples  

Date Get:
  1. Day of Week
  2. Number of days in a month
  3. Month of year
  4. Current month name
  5. Full date time
  6. Get all attributes from a Calendar
  7. Get year, month, day, minute, hour, second, milli second from java.util.Date through Calendar
  8. Get age from a birthday date
  9. Get Monday
  10. Get next Sunday
  11. Get today's date
  12. Get the last day of a month
  13. Get date of yesterday
  14. Get Day-of-Week for a Particular Date
  15. Get date of last week
  16. Get date of last month
  17. Get last Date of This Month
  18. Get Month in a leap year
  19. Get start of a month
  20. Get the end of a month
  21. Get noon of a day
  22. Get days since a date
  23. Get TimeZone
  24. Get Last day fo previous Month
  25. Is an hour between an interval
  26. Is a Year a Leap Year