DayOfWeekInMonthRule.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * -------------------------
  28. * DayOfWeekInMonthRule.java
  29. * -------------------------
  30. * (C) Copyright 2000-2003, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * $Id: DayOfWeekInMonthRule.java,v 1.5 2005/11/16 15:58:40 taqua Exp $
  36. *
  37. * Changes (from 26-Oct-2001)
  38. * --------------------------
  39. * 26-Oct-2001 : Changed package to com.jrefinery.date.*;
  40. * 03-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41. * 01-Jun-2005 : Removed the explicit clonable declaration, it is declared
  42. * in the super class.
  43. */
  44. package org.jfree.date;
  45. /**
  46. * An annual date rule that specifies the nth day of the week in a given month
  47. * (for example, the third Wednesday in June, or the last Friday in November).
  48. *
  49. * @author David Gilbert
  50. */
  51. public class DayOfWeekInMonthRule extends AnnualDateRule {
  52. /** FIRST, SECOND, THIRD, FOURTH or LAST. */
  53. private int count;
  54. /** The day of the week (SerialDate.MONDAY, SerialDate.TUESDAY...). */
  55. private int dayOfWeek;
  56. /** The month (1 to 12, or SerialDate.JANUARY, SerialDate.FEBRUARY...). */
  57. private int month;
  58. /**
  59. * Default constructor: builds a rule for the first Monday in January by default.
  60. */
  61. public DayOfWeekInMonthRule() {
  62. this(1, SerialDate.MONDAY, MonthConstants.JANUARY);
  63. }
  64. /**
  65. * Standard constructor: builds a rule with the specified attributes.
  66. *
  67. * @param count one of: FIRST, SECOND, THIRD, FOURTH or LAST.
  68. * @param dayOfWeek the day-of-the-week (SerialDate.MONDAY, SerialDate.TUESDAY, etc.).
  69. * @param month the month (SerialDate.JANUARY, SerialDate.FEBRUARY, etc.).
  70. */
  71. public DayOfWeekInMonthRule(final int count, final int dayOfWeek, final int month) {
  72. this.count = count;
  73. this.dayOfWeek = dayOfWeek;
  74. this.month = month;
  75. }
  76. /**
  77. * Returns the 'count' for this rule (one of FIRST, SECOND, THIRD, FOURTH and LAST).
  78. *
  79. * @return the 'count'.
  80. */
  81. public int getCount() {
  82. return this.count;
  83. }
  84. /**
  85. * Sets the 'count' for this rule (one of FIRST, SECOND, THIRD, FOURTH and LAST).
  86. *
  87. * @param count the 'count'.
  88. */
  89. public void setCount(final int count) {
  90. this.count = count;
  91. }
  92. /**
  93. * Returns the day-of-the-week for this rule (SerialDate.MONDAY, SerialDate.TUESDAY, etc.).
  94. *
  95. * @return the day-of-the-week.
  96. */
  97. public int getDayOfWeek() {
  98. return this.dayOfWeek;
  99. }
  100. /**
  101. * Sets the day-of-the-week for this rule.
  102. *
  103. * @param dayOfWeek the day-of-the-week.
  104. */
  105. public void setDayOfWeek(final int dayOfWeek) {
  106. this.dayOfWeek = dayOfWeek;
  107. }
  108. /**
  109. * Returns the month for this rule.
  110. *
  111. * @return the month.
  112. */
  113. public int getMonth() {
  114. return this.month;
  115. }
  116. /**
  117. * Sets the month for this rule.
  118. *
  119. * @param month the month (SerialDate.JANUARY, SerialDate.FEBRUARY, etc.).
  120. */
  121. public void setMonth(final int month) {
  122. this.month = month;
  123. }
  124. /**
  125. * Return the date for this rule, given the year.
  126. *
  127. * @param year the year.
  128. *
  129. * @return the date generated by the rule for the given year.
  130. */
  131. public SerialDate getDate(final int year) {
  132. SerialDate result;
  133. if (this.count != SerialDate.LAST_WEEK_IN_MONTH) {
  134. // start at the beginning of the month
  135. result = SerialDate.createInstance(1, this.month, year);
  136. while (result.getDayOfWeek() != this.dayOfWeek) {
  137. result = SerialDate.addDays(1, result);
  138. }
  139. result = SerialDate.addDays(7 * (this.count - 1), result);
  140. }
  141. else {
  142. // start at the end of the month and work backwards...
  143. result = SerialDate.createInstance(1, this.month, year);
  144. result = result.getEndOfCurrentMonth(result);
  145. while (result.getDayOfWeek() != this.dayOfWeek) {
  146. result = SerialDate.addDays(-1, result);
  147. }
  148. }
  149. return result;
  150. }
  151. }