programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Dates and Calendars:3rd Tuesday, etc

Ranch Hand
Posts: 221
5

Unlike most people, my life doesn't revolve around dates. Instead, I have X event on the third Tuesday of each month, Y event on the fourth Thursday, etc. I obviously am missing something pretty simplistic here, and I could really use a pointer. I thought I was on the right track, but I'm a bit perplexed. The set day_of_month and the get day_of_week work just like I thought they would. What is confusing me though is that when I set day_of_week to 5, it returns Saturday (7) and not Thursday.

Bartender
Posts: 10575
66
Robert D. Smith wrote:I obviously am missing something pretty simplistic here, and I could really use a pointer.

Actually, you're not. That sort of stuff is notoriously fiddly to do, because calendars (and particularly the Gregorian calendar) are almost Machiavellian in their complexity.

Furthermore (as you've already discovered) DAY_OF_WEEK is 1-based, whereas it would be a lot simpler if SUNDAY had been day '0'.

However, the basic concept is this (and you're pretty close):
1. Find out the DAY_OF_WEEK of the 1st of the month in question.
2. Find out the difference between that DAY_OF_WEEK, and the one you want (if it's negative, just add 7). That will give you the number of days to add to get to the first day you want.
3. Add (n-1) * 7 days to get to the nth day you want.

HIH

Winston

Java Cowboy
Sheriff
Posts: 16060
88
GregorianCalendar.DAY_OF_WEEK is a constant that indicates you want to get or set the DAY_OF_WEEK field of the calendar. It is not a day of the week itself.

In line 18 of your code:

you are just printing the value of the constant, not the day of the week value that the calendar is set to. Change it to:

Robert D. Smith
Ranch Hand
Posts: 221
5
I had meant to post this yesterday. I wanted to thank you both for your help -- I was finally able to retrieve the result(s) I was looking for.

If I could give out cows, I would have. Thank you again.