]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/util/DateTimeRule.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / util / DateTimeRule.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2007-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.util;\r
8 \r
9 import java.io.Serializable;\r
10 \r
11 /**\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
14  * time in the day.\r
15  * \r
16  * @stable ICU 3.8\r
17  */\r
18 public class DateTimeRule implements Serializable {\r
19 \r
20     private static final long serialVersionUID = 2183055795738051443L;\r
21 \r
22     /**\r
23      * Date rule type defined by exact day of month.\r
24      * For example, March 14.\r
25      * \r
26      * @stable ICU 3.8\r
27      */\r
28     public static final int DOM = 0;\r
29 \r
30     /**\r
31      * Date rule type defined by day of week in month.\r
32      * For example, 2nd Sunday in March.\r
33      * \r
34      * @stable ICU 3.8\r
35      */\r
36     public static final int DOW = 1;\r
37 \r
38     /**\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
42      * \r
43      * @stable ICU 3.8\r
44      */\r
45     public static final int DOW_GEQ_DOM = 2;\r
46 \r
47     /**\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
51      * \r
52      * @stable ICU 3.8\r
53      */\r
54     public static final int DOW_LEQ_DOM = 3;\r
55     \r
56     /**\r
57      * Time rule type for local wall time.\r
58      * \r
59      * @stable ICU 3.8\r
60      */\r
61     public static final int WALL_TIME = 0;\r
62 \r
63     /**\r
64      * Time rule type for local standard time.\r
65      * \r
66      * @stable ICU 3.8\r
67      */\r
68     public static final int STANDARD_TIME = 1;\r
69 \r
70     /**\r
71      * Time rule type for coordinated universal time.\r
72      * \r
73      * @stable ICU 3.8\r
74      */\r
75     public static final int UTC_TIME = 2;\r
76 \r
77     // private stuff\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
83 \r
84     private final int timeRuleType;\r
85     private final int millisInDay;\r
86 \r
87     /**\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
91      * \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
97      * \r
98      * @stable ICU 3.8\r
99      */\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
105 \r
106         this.millisInDay = millisInDay;\r
107         this.timeRuleType = timeType;\r
108         \r
109         // not used by this rule type\r
110         this.dayOfWeek = 0;\r
111         this.weekInMonth = 0;\r
112     }\r
113 \r
114     /**\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
118      * \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
127      * \r
128      * @stable ICU 3.8\r
129      */\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
136 \r
137         this.millisInDay = millisInDay;\r
138         this.timeRuleType = timeType;\r
139 \r
140         // not used by this rule type\r
141         this.dayOfMonth = 0;\r
142     }\r
143 \r
144     /**\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
149      * \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
157      * \r
158      * @stable ICU 3.8\r
159      */\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
166 \r
167         this.millisInDay = millisInDay;\r
168         this.timeRuleType = timeType;\r
169 \r
170         // not used by this rule type\r
171         this.weekInMonth = 0;\r
172     }\r
173 \r
174     /**\r
175      * Gets the date rule type, such as <code>DOM</code>\r
176      * \r
177      * @return The date rule type.\r
178      * \r
179      * @stable ICU 3.8\r
180      */\r
181     public int getDateRuleType() {\r
182         return dateRuleType;\r
183     }\r
184 \r
185     /**\r
186      * Gets the rule month.\r
187      * \r
188      * @return The rule month.\r
189      * \r
190      * @stable ICU 3.8\r
191      */\r
192     public int getRuleMonth() {\r
193         return month;\r
194     }\r
195 \r
196     /**\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
199      * \r
200      * @return The rule day of month\r
201      * \r
202      * @stable ICU 3.8\r
203      */\r
204     public int getRuleDayOfMonth() {\r
205         return dayOfMonth;\r
206     }\r
207 \r
208     /**\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
211      * \r
212      * @return The rule day of week.\r
213      * \r
214      * @stable ICU 3.8\r
215      */\r
216     public int getRuleDayOfWeek() {\r
217         return dayOfWeek;\r
218     }\r
219 \r
220     /**\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
223      * always 0.\r
224      * \r
225      * @return The rule day of week ordinal number in the month.\r
226      * \r
227      * @stable ICU 3.8\r
228      */\r
229     public int getRuleWeekInMonth() {\r
230         return weekInMonth;\r
231     }\r
232 \r
233     /**\r
234      * Gets the time rule type\r
235      * \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
238      * \r
239      * @stable ICU 3.8\r
240      */\r
241     public int getTimeRuleType() {\r
242         return timeRuleType;\r
243     }\r
244 \r
245     /**\r
246      * Gets the rule time in the rule day.\r
247      * \r
248      * @return The time in the rule day in milliseconds.\r
249      * \r
250      * @stable ICU 3.8\r
251      */\r
252     public int getRuleMillisInDay() {\r
253         return millisInDay;\r
254     }\r
255     \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
258 \r
259     /**\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
263      * \r
264      * @stable ICU 3.8\r
265      */\r
266     public String toString() {\r
267         String sDate = null;\r
268         String sTimeRuleType = null;\r
269 \r
270         switch (dateRuleType) {\r
271         case DOM:\r
272             sDate = Integer.toString(dayOfMonth);\r
273             break;\r
274         case DOW:\r
275             sDate = Integer.toString(weekInMonth) + DOWSTR[dayOfWeek];\r
276             break;\r
277         case DOW_GEQ_DOM:\r
278             sDate = DOWSTR[dayOfWeek] + ">=" + Integer.toString(dayOfMonth);\r
279             break;\r
280         case DOW_LEQ_DOM:\r
281             sDate = DOWSTR[dayOfWeek] + "<=" + Integer.toString(dayOfMonth);\r
282             break;\r
283         }\r
284 \r
285         switch (timeRuleType) {\r
286         case WALL_TIME:\r
287             sTimeRuleType = "WALL";\r
288             break;\r
289         case STANDARD_TIME:\r
290             sTimeRuleType = "STD";\r
291             break;\r
292         case UTC_TIME:\r
293             sTimeRuleType = "UTC";\r
294             break;\r
295         }\r
296 \r
297         int time = millisInDay;\r
298         int millis = time % 1000;\r
299         time /= 1000;\r
300         int secs = time % 60;\r
301         time /= 60;\r
302         int mins = time % 60;\r
303         int hours = time / 60;\r
304 \r
305         StringBuilder buf = new StringBuilder();\r
306         buf.append("month=");\r
307         buf.append(MONSTR[month]);\r
308         buf.append(", date=");\r
309         buf.append(sDate);\r
310         buf.append(", time=");\r
311         buf.append(hours);\r
312         buf.append(":");\r
313         buf.append(mins/10);\r
314         buf.append(mins%10);\r
315         buf.append(":");\r
316         buf.append(secs/10);\r
317         buf.append(secs%10);\r
318         buf.append(".");\r
319         buf.append(millis/100);\r
320         buf.append((millis/10)%10);\r
321         buf.append(millis%10);\r
322         buf.append("(");\r
323         buf.append(sTimeRuleType);\r
324         buf.append(")");\r
325         return buf.toString();\r
326     }\r
327 }\r