1

I am trying to create a function where I will send a specific date (e.g. 2017-09-16 and it will return the Sunday of this week.

For instance, if I send 2017-09-16 it must return 2017-09-17.

I was trying to do it using Calendar.

Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

And this will return me the day of the week but I do not exactly know how to the convert it to Sunday.

  • 2
    First you need to define what's the first day of the week, as it's [not the same in all countries](https://www.timeanddate.com/calendar/days). If you use ISO definition (Monday is the first day of week), then 2017-09-17 is the right answer. But in US, the first day of week is Sunday, so the Sunday in the same week of 2017-09-16 is 2017-09-10. Or do you just want "the next Sunday" regardless of week-definitions? –  Sep 21 '17 at 16:14
  • 2
    FYI, the code in this Question uses troublesome old date-time classes that are now legacy, supplanted by the java.time classes. – Basil Bourque Sep 22 '17 at 04:23

5 Answers5

2

To find the next Sunday, use Java 8 LocalDate and either TemporalAdjusters.nextOrSame(...) or TemporalAdjusters.next(...).

LocalDate date = LocalDate.parse("2017-09-16");
date = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
System.out.println(date); // prints: 2017-09-17
Andreas
  • 154,647
  • 11
  • 152
  • 247
2

Consider avoiding Calendar and using better API's suited for date manipulation. The old classes (Date, Calendar and SimpleDateFormat) have lots of problems and design issues, and they're being replaced by the new APIs.

If you're using Java 8, consider using the new java.time API. It's easier, less bugged and less error-prone than the old APIs.

If you're using Java 6 or 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, you'll also need the ThreeTenABP (more on how to use it here).

The code below works for both. The only difference is the package names (in Java 8 is java.time and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp), but the classes and methods names are the same.

First you need to define what's the first day of the week, as it's not the same in all countries. You can use ISO's definition (Monday is the first day of the week), so your code will be like this:

LocalDate date = LocalDate.of(2017, 9, 16);
// use ISO definition (Monday is the first day of week)
WeekFields wf = WeekFields.ISO;
LocalDate sundayInSameWeek = date
    // get the first day of this week
    .with(wf.dayOfWeek(), 1)
    // get next Sunday (or the same date, if it's already Sunday)
    .with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
System.out.println(sundayInSameWeek); // 2017-09-17

The result is 2017-09-17. But if I use a different definition of week (such as US's definition, where Sunday is the first day of the week):

LocalDate date = LocalDate.of(2017, 9, 16);
// use US's definition of week (Sunday is the first day of week)
WeekFields wf = WeekFields.of(Locale.ENGLISH);
LocalDate sundayInSameWeek = date ... // all the same code

The result will be 2017-09-10, because in US, Sunday is the first day of week, so the "Sunday in the same week as 2017-09-17" is indeed 2017-09-10.


If you just want the next Sunday, regardless of week definitions, you can just use the TemporalAdjusters class:

LocalDate date = LocalDate.of(2017, 9, 16);
LocalDate nextSunday = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); // 2017-09-17

If you have a String with the date, you can parse it directly to a LocalDate:

// creates 2017-09-16
LocalDate date = LocalDate.parse("2017-09-16");
1

You can use LocalDate which is the Java 8 API for date (LocalTime for time and LocalDateTime for both)


You have juste to add the number of days missing between your date an d next sunday, which correponds to 7-dayOfWeek :

LocalDate date = LocalDate.of(2017, 9, 16);
LocalDate sundayNext = date.plusDays(7 - date.getDayOfWeek().getValue());

If you are Monday , dayOfWeek is 1 and you next sunday is in (7-1) = 6 days

If you are Tuesday, dayOfWeek is 2 and you next sunday is in (7-2) = 5 days

...

azro
  • 53,056
  • 7
  • 34
  • 70
1

In your case where Sunday is last day of week, you can subtract current day of week from 7, and add result to date.

Calendar date = Calendar.getInstance();
date.set(2017, Calendar.SEPTEMBER, 16);
date.add(Calendar.DAY_OF_MONTH, 7 - (date.get(Calendar.DAY_OF_WEEK) - 1));

System.out.println(date.get(Calendar.DAY_OF_MONTH));    // prints "17"

But I recommend to use Java 8 new API for date LocalDate as suggested in Hugo's answer.

Vladimír Bielený
  • 2,795
  • 2
  • 11
  • 16
1

You can try:

public static void main(String[] args) throws Exception {
    SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
    Date yourDate = sm.parse("2017-09-16");

    Calendar c = Calendar.getInstance();
    c.setTime(yourDate);
    int day = c.get(Calendar.DATE);
    System.out.println(day);
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
    c.set(Calendar.DATE, 8 - dayOfWeek + day);
    Date nextSunDate = c.getTime();

    System.out.println(yourDate);     //2017-09-16
    System.out.println(nextSunDate);  //2017-09-17 
}

If your date is 2017-09-17 then nextSunDate will be 2017-09-24

Tony Kru
  • 41
  • 5