On Dec 21, 2008, at 6:34 PM, Stephen Colebourne wrote: > Joda-Time doesn't have anything to help with this particular > calculation > (JSR-310 does).
I'm new to Joda-Time. Perhaps some of my assumptions were wrong. I thought JSR-310 was all based on Joda-Time and assumed that anything new in JSR-310 would be tested by adding it to Joda-Time. When you say JSR-310 has it, do you mean that it's in the spec or that there is different library I could download now that has it? Here's the implementation I came up with. If it looks useful, feel free to add it to Joda-Time under whatever name you think is best. /** * Gets the nth given weekday of a given month. * For example, to find the 4th Thursday of the month of November in 2008, * getNthWeekday(2008, 11, DateTimeConstants.THURSDAY, 4) * @param year the year * @param month the month * @param weekday the day of the week (such as Thursday) * @param n the number of that day within the month (such as the 4th) * @return the LocalDate */ private static LocalDate getNthWeekday( int year, int month, int weekday, int n) { // Get the 1st day of the month. LocalDate ld = new LocalDate(year, month, 1); // Determine the number of days to advance to get to // the first requested weekday of the month. int firstWeekday = ld.getDayOfWeek(); int diff = (7 - firstWeekday + weekday) % 7; return ld.plusDays(diff).plusWeeks(n - 1); } > The withDayfWeek() method adjusts the day within the current Monday to > Sunday week. > > Stephen > > > Kevin Bourrillion wrote: >> I was wondering the same thing just yesterday. >> >> >> On Fri, Dec 19, 2008 at 7:02 AM, Mark Volkmann <m...@ociweb.com >> <mailto:m...@ociweb.com>> wrote: >> >> LocalDate ld = new LocalDate(year, 11, 1); >> if (ld.getDayOfWeek() != DateTimeConstants.THURSDAY) { >> >> >> At this point, 'd.plusDays(THURSDAY - d.getDayOfWeek() + 7) % 7)' >> should >> get you straight to the first Thursday, right? I don't know what's >> the >> best way, though. I also couldn't find an explanation of what >> .withDayOfWeek() does -- does it always move backward? >> >> K >> >> // Move back to the previous Thursday. >> ld = ld.withDayOfWeek(DateTimeConstants.THURSDAY); >> >> if (ld.monthOfYear().get() != >> DateTimeConstants.NOVEMBER) { >> ld = ld.plusWeeks(1); // move forward into >> November >> } >> } >> >> return ld.plusWeeks(3); // to get to 4th Thursday >> } >> } >> >> // Now call that method like this. >> LocalDate thanksgiving = getThanksgiving(year); >> if (thanksgiving.isBefore(today)) { >> thanksgiving = getThanksgiving(year + 1); >> } --- Mark Volkmann ------------------------------------------------------------------------------ _______________________________________________ Joda-interest mailing list Joda-interest@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/joda-interest