2

I get a date and want to check if it is the next sunday.I found alot of code with Calendar etc, but I can't find the right code. And I don't really understand how I can know if it is the next sunday from a date.

Thanks for you help

Danny Gloudemans
  • 2,597
  • 7
  • 39
  • 57

5 Answers5

3

Break the problem down:

  1. Get today's date: new Date();
  2. Get the day of the week for today's date.
  3. Advance forward to Sunday
  4. Get that date
duffymo
  • 305,152
  • 44
  • 369
  • 561
3

First of, I recommend Joda Time as a much better Date/Time API than Calendar.

As for your processing it breaks down into easy steps:

  1. Construct DateTime objects for the two dates
  2. Check that the target date is a Sunday
  3. Check that the difference between them is between 0 and 7 days
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
0

If you look for nice Date management, check this out Joda Time

alex
  • 10,900
  • 15
  • 70
  • 100
0

this might be helpful to you , you can chek by dayOfWeek == Calendar.SUNDAY after adding one day

Community
  • 1
  • 1
Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46
0

With Lamma Date it's very easy to first obtain next Sunday, then we can use equals check if the date is next Sunday.

    Date today = new Date(2014, 7, 1);  // assume today is 2014-07-01
    Date nextSunday = today.next(DayOfWeek.SUNDAY);     // 2014-07-06
Max
  • 2,065
  • 24
  • 20
  • Make sure you've read the guidelines about self-promotion: http://stackoverflow.com/help/behavior – brasofilo Jun 29 '14 at 21:20
  • Thanks for reminding me this. IMO this one is a relevant answer which: 1) indeed solved the problem. 2) provide more code examples. Will take a look at how http://blog.stackoverflow.com/2011/05/community-promotion-ads/ works, thanks! – Max Jun 30 '14 at 07:58