2 *******************************************************************************
\r
3 * Copyright (C) 2007-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.util;
\r
9 import java.io.Serializable;
\r
12 * <code>DateTimeRule</code> is a class representing a time in a year by
\r
13 * a rule specified by month, day of month, day of week and
\r
18 public class DateTimeRule implements Serializable {
\r
20 private static final long serialVersionUID = 2183055795738051443L;
\r
23 * Date rule type defined by exact day of month.
\r
24 * For example, March 14.
\r
28 public static final int DOM = 0;
\r
31 * Date rule type defined by day of week in month.
\r
32 * For example, 2nd Sunday in March.
\r
36 public static final int DOW = 1;
\r
39 * Date rule type defined by first day of week on or
\r
40 * after exact day of month.
\r
41 * For example, 1st Monday on or after March 15.
\r
45 public static final int DOW_GEQ_DOM = 2;
\r
48 * Date rule type defined by last day of week on or
\r
49 * before exact day of month.
\r
50 * For example, last Saturday on or before March 15.
\r
54 public static final int DOW_LEQ_DOM = 3;
\r
57 * Time rule type for local wall time.
\r
61 public static final int WALL_TIME = 0;
\r
64 * Time rule type for local standard time.
\r
68 public static final int STANDARD_TIME = 1;
\r
71 * Time rule type for coordinated universal time.
\r
75 public static final int UTC_TIME = 2;
\r
78 private final int dateRuleType;
\r
79 private final int month;
\r
80 private final int dayOfMonth;
\r
81 private final int dayOfWeek;
\r
82 private final int weekInMonth;
\r
84 private final int timeRuleType;
\r
85 private final int millisInDay;
\r
88 * Constructs a <code>DateTimeRule</code> by the day of month and
\r
89 * the time rule. The date rule type for an instance created by
\r
90 * this constructor is <code>DOM</code>.
\r
92 * @param month The rule month, for example, <code>Calendar.JANUARY</code>
\r
93 * @param dayOfMonth The day of month, 1-based.
\r
94 * @param millisInDay The milliseconds in the rule date.
\r
95 * @param timeType The time type, <code>WALL_TIME</code> or <code>STANDARD_TIME</code>
\r
96 * or <code>UTC_TIME</code>.
\r
100 public DateTimeRule(int month, int dayOfMonth,
\r
101 int millisInDay, int timeType) {
\r
102 dateRuleType = DOM;
\r
103 this.month = month;
\r
104 this.dayOfMonth = dayOfMonth;
\r
106 this.millisInDay = millisInDay;
\r
107 this.timeRuleType = timeType;
\r
109 // not used by this rule type
\r
110 this.dayOfWeek = 0;
\r
111 this.weekInMonth = 0;
\r
115 * Constructs a <code>DateTimeRule</code> by the day of week and its oridinal
\r
116 * number and the time rule. The date rule type for an instance created
\r
117 * by this constructor is <code>DOW</code>.
\r
119 * @param month The rule month, for example, <code>Calendar.JANUARY</code>.
\r
120 * @param weekInMonth The ordinal number of the day of week. Negative number
\r
121 * may be used for specifying a rule date counted from the
\r
122 * end of the rule month.
\r
123 * @param dayOfWeek The day of week, for example, <code>Calendar.SUNDAY</code>.
\r
124 * @param millisInDay The milliseconds in the rule date.
\r
125 * @param timeType The time type, <code>WALL_TIME</code> or <code>STANDARD_TIME</code>
\r
126 * or <code>UTC_TIME</code>.
\r
130 public DateTimeRule(int month, int weekInMonth, int dayOfWeek,
\r
131 int millisInDay, int timeType) {
\r
132 dateRuleType = DOW;
\r
133 this.month = month;
\r
134 this.weekInMonth = weekInMonth;
\r
135 this.dayOfWeek = dayOfWeek;
\r
137 this.millisInDay = millisInDay;
\r
138 this.timeRuleType = timeType;
\r
140 // not used by this rule type
\r
141 this.dayOfMonth = 0;
\r
145 * Constructs a <code>DateTimeRule</code> by the first/last day of week
\r
146 * on or after/before the day of month and the time rule. The date rule
\r
147 * type for an instance created by this constructor is either
\r
148 * <code>DOM_GEQ_DOM</code> or <code>DOM_LEQ_DOM</code>.
\r
150 * @param month The rule month, for example, <code>Calendar.JANUARY</code>
\r
151 * @param dayOfMonth The day of month, 1-based.
\r
152 * @param dayOfWeek The day of week, for example, <code>Calendar.SUNDAY</code>.
\r
153 * @param after true if the rule date is on or after the day of month.
\r
154 * @param millisInDay The milliseconds in the rule date.
\r
155 * @param timeType The time type, <code>WALL_TIME</code> or <code>STANDARD_TIME</code>
\r
156 * or <code>UTC_TIME</code>.
\r
160 public DateTimeRule(int month, int dayOfMonth, int dayOfWeek, boolean after,
\r
161 int millisInDay, int timeType) {
\r
162 this.dateRuleType = after ? DOW_GEQ_DOM : DOW_LEQ_DOM;
\r
163 this.month = month;
\r
164 this.dayOfMonth = dayOfMonth;
\r
165 this.dayOfWeek = dayOfWeek;
\r
167 this.millisInDay = millisInDay;
\r
168 this.timeRuleType = timeType;
\r
170 // not used by this rule type
\r
171 this.weekInMonth = 0;
\r
175 * Gets the date rule type, such as <code>DOM</code>
\r
177 * @return The date rule type.
\r
181 public int getDateRuleType() {
\r
182 return dateRuleType;
\r
186 * Gets the rule month.
\r
188 * @return The rule month.
\r
192 public int getRuleMonth() {
\r
197 * Gets the rule day of month. When the date rule type
\r
198 * is <code>DOW</code>, the value is always 0.
\r
200 * @return The rule day of month
\r
204 public int getRuleDayOfMonth() {
\r
209 * Gets the rule day of week. When the date rule type
\r
210 * is <code>DOM</code>, the value is always 0.
\r
212 * @return The rule day of week.
\r
216 public int getRuleDayOfWeek() {
\r
221 * Gets the rule day of week ordinal number in the month.
\r
222 * When the date rule type is not <code>DOW</code>, the value is
\r
225 * @return The rule day of week ordinal number in the month.
\r
229 public int getRuleWeekInMonth() {
\r
230 return weekInMonth;
\r
234 * Gets the time rule type
\r
236 * @return The time rule type, either <code>WALL_TIME</code> or <code>STANDARD_TIME</code>
\r
237 * or <code>UTC_TIME</code>.
\r
241 public int getTimeRuleType() {
\r
242 return timeRuleType;
\r
246 * Gets the rule time in the rule day.
\r
248 * @return The time in the rule day in milliseconds.
\r
252 public int getRuleMillisInDay() {
\r
253 return millisInDay;
\r
256 private static final String[] DOWSTR = {"", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
\r
257 private static final String[] MONSTR = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
\r
260 * Returns a <code>String</code> representation of this <code>DateTimeRule</code> object.
\r
261 * This method is used for debugging purpose only. The string representation can be changed
\r
262 * in future version of ICU without any notice.
\r
266 public String toString() {
\r
267 String sDate = null;
\r
268 String sTimeRuleType = null;
\r
270 switch (dateRuleType) {
\r
272 sDate = Integer.toString(dayOfMonth);
\r
275 sDate = Integer.toString(weekInMonth) + DOWSTR[dayOfWeek];
\r
278 sDate = DOWSTR[dayOfWeek] + ">=" + Integer.toString(dayOfMonth);
\r
281 sDate = DOWSTR[dayOfWeek] + "<=" + Integer.toString(dayOfMonth);
\r
285 switch (timeRuleType) {
\r
287 sTimeRuleType = "WALL";
\r
289 case STANDARD_TIME:
\r
290 sTimeRuleType = "STD";
\r
293 sTimeRuleType = "UTC";
\r
297 int time = millisInDay;
\r
298 int millis = time % 1000;
\r
300 int secs = time % 60;
\r
302 int mins = time % 60;
\r
303 int hours = time / 60;
\r
305 StringBuilder buf = new StringBuilder();
\r
306 buf.append("month=");
\r
307 buf.append(MONSTR[month]);
\r
308 buf.append(", date=");
\r
310 buf.append(", time=");
\r
313 buf.append(mins/10);
\r
314 buf.append(mins%10);
\r
316 buf.append(secs/10);
\r
317 buf.append(secs%10);
\r
319 buf.append(millis/100);
\r
320 buf.append((millis/10)%10);
\r
321 buf.append(millis%10);
\r
323 buf.append(sTimeRuleType);
\r
325 return buf.toString();
\r