Danny Gloudemans
Danny Gloudemans

Reputation: 2677

Java check if date is next sunday

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

Upvotes: 2

Views: 934

Answers (5)

Max
Max

Reputation: 2095

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

Upvotes: 0

Nirmal- thInk beYond
Nirmal- thInk beYond

Reputation: 12054

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

Upvotes: 0

OrangeDog
OrangeDog

Reputation: 38759

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

Upvotes: 3

duffymo
duffymo

Reputation: 308743

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

Upvotes: 3

alex
alex

Reputation: 11400

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

Upvotes: 0

Related Questions