1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 /* 3 ******************************************************************************* 4 * Copyright (C) 2007-2010, International Business Machines Corporation and * 5 * others. All Rights Reserved. * 6 ******************************************************************************* 7 */ 8 package android.icu.util; 9 10 import java.io.Serializable; 11 12 /** 13 * <code>DateTimeRule</code> is a class representing a time in a year by 14 * a rule specified by month, day of month, day of week and 15 * time in the day. 16 * 17 * @hide Only a subset of ICU is exposed in Android 18 */ 19 public class DateTimeRule implements Serializable { 20 21 private static final long serialVersionUID = 2183055795738051443L; 22 23 /** 24 * Date rule type defined by exact day of month. 25 * For example, March 14. 26 */ 27 public static final int DOM = 0; 28 29 /** 30 * Date rule type defined by day of week in month. 31 * For example, 2nd Sunday in March. 32 */ 33 public static final int DOW = 1; 34 35 /** 36 * Date rule type defined by first day of week on or 37 * after exact day of month. 38 * For example, 1st Monday on or after March 15. 39 */ 40 public static final int DOW_GEQ_DOM = 2; 41 42 /** 43 * Date rule type defined by last day of week on or 44 * before exact day of month. 45 * For example, last Saturday on or before March 15. 46 */ 47 public static final int DOW_LEQ_DOM = 3; 48 49 /** 50 * Time rule type for local wall time. 51 */ 52 public static final int WALL_TIME = 0; 53 54 /** 55 * Time rule type for local standard time. 56 */ 57 public static final int STANDARD_TIME = 1; 58 59 /** 60 * Time rule type for coordinated universal time. 61 */ 62 public static final int UTC_TIME = 2; 63 64 // private stuff 65 private final int dateRuleType; 66 private final int month; 67 private final int dayOfMonth; 68 private final int dayOfWeek; 69 private final int weekInMonth; 70 71 private final int timeRuleType; 72 private final int millisInDay; 73 74 /** 75 * Constructs a <code>DateTimeRule</code> by the day of month and 76 * the time rule. The date rule type for an instance created by 77 * this constructor is <code>DOM</code>. 78 * 79 * @param month The rule month, for example, <code>Calendar.JANUARY</code> 80 * @param dayOfMonth The day of month, 1-based. 81 * @param millisInDay The milliseconds in the rule date. 82 * @param timeType The time type, <code>WALL_TIME</code> or <code>STANDARD_TIME</code> 83 * or <code>UTC_TIME</code>. 84 */ DateTimeRule(int month, int dayOfMonth, int millisInDay, int timeType)85 public DateTimeRule(int month, int dayOfMonth, 86 int millisInDay, int timeType) { 87 dateRuleType = DOM; 88 this.month = month; 89 this.dayOfMonth = dayOfMonth; 90 91 this.millisInDay = millisInDay; 92 this.timeRuleType = timeType; 93 94 // not used by this rule type 95 this.dayOfWeek = 0; 96 this.weekInMonth = 0; 97 } 98 99 /** 100 * Constructs a <code>DateTimeRule</code> by the day of week and its oridinal 101 * number and the time rule. The date rule type for an instance created 102 * by this constructor is <code>DOW</code>. 103 * 104 * @param month The rule month, for example, <code>Calendar.JANUARY</code>. 105 * @param weekInMonth The ordinal number of the day of week. Negative number 106 * may be used for specifying a rule date counted from the 107 * end of the rule month. 108 * @param dayOfWeek The day of week, for example, <code>Calendar.SUNDAY</code>. 109 * @param millisInDay The milliseconds in the rule date. 110 * @param timeType The time type, <code>WALL_TIME</code> or <code>STANDARD_TIME</code> 111 * or <code>UTC_TIME</code>. 112 */ DateTimeRule(int month, int weekInMonth, int dayOfWeek, int millisInDay, int timeType)113 public DateTimeRule(int month, int weekInMonth, int dayOfWeek, 114 int millisInDay, int timeType) { 115 dateRuleType = DOW; 116 this.month = month; 117 this.weekInMonth = weekInMonth; 118 this.dayOfWeek = dayOfWeek; 119 120 this.millisInDay = millisInDay; 121 this.timeRuleType = timeType; 122 123 // not used by this rule type 124 this.dayOfMonth = 0; 125 } 126 127 /** 128 * Constructs a <code>DateTimeRule</code> by the first/last day of week 129 * on or after/before the day of month and the time rule. The date rule 130 * type for an instance created by this constructor is either 131 * <code>DOM_GEQ_DOM</code> or <code>DOM_LEQ_DOM</code>. 132 * 133 * @param month The rule month, for example, <code>Calendar.JANUARY</code> 134 * @param dayOfMonth The day of month, 1-based. 135 * @param dayOfWeek The day of week, for example, <code>Calendar.SUNDAY</code>. 136 * @param after true if the rule date is on or after the day of month. 137 * @param millisInDay The milliseconds in the rule date. 138 * @param timeType The time type, <code>WALL_TIME</code> or <code>STANDARD_TIME</code> 139 * or <code>UTC_TIME</code>. 140 */ DateTimeRule(int month, int dayOfMonth, int dayOfWeek, boolean after, int millisInDay, int timeType)141 public DateTimeRule(int month, int dayOfMonth, int dayOfWeek, boolean after, 142 int millisInDay, int timeType) { 143 this.dateRuleType = after ? DOW_GEQ_DOM : DOW_LEQ_DOM; 144 this.month = month; 145 this.dayOfMonth = dayOfMonth; 146 this.dayOfWeek = dayOfWeek; 147 148 this.millisInDay = millisInDay; 149 this.timeRuleType = timeType; 150 151 // not used by this rule type 152 this.weekInMonth = 0; 153 } 154 155 /** 156 * Gets the date rule type, such as <code>DOM</code> 157 * 158 * @return The date rule type. 159 */ getDateRuleType()160 public int getDateRuleType() { 161 return dateRuleType; 162 } 163 164 /** 165 * Gets the rule month. 166 * 167 * @return The rule month. 168 */ getRuleMonth()169 public int getRuleMonth() { 170 return month; 171 } 172 173 /** 174 * Gets the rule day of month. When the date rule type 175 * is <code>DOW</code>, the value is always 0. 176 * 177 * @return The rule day of month 178 */ getRuleDayOfMonth()179 public int getRuleDayOfMonth() { 180 return dayOfMonth; 181 } 182 183 /** 184 * Gets the rule day of week. When the date rule type 185 * is <code>DOM</code>, the value is always 0. 186 * 187 * @return The rule day of week. 188 */ getRuleDayOfWeek()189 public int getRuleDayOfWeek() { 190 return dayOfWeek; 191 } 192 193 /** 194 * Gets the rule day of week ordinal number in the month. 195 * When the date rule type is not <code>DOW</code>, the value is 196 * always 0. 197 * 198 * @return The rule day of week ordinal number in the month. 199 */ getRuleWeekInMonth()200 public int getRuleWeekInMonth() { 201 return weekInMonth; 202 } 203 204 /** 205 * Gets the time rule type 206 * 207 * @return The time rule type, either <code>WALL_TIME</code> or <code>STANDARD_TIME</code> 208 * or <code>UTC_TIME</code>. 209 */ getTimeRuleType()210 public int getTimeRuleType() { 211 return timeRuleType; 212 } 213 214 /** 215 * Gets the rule time in the rule day. 216 * 217 * @return The time in the rule day in milliseconds. 218 */ getRuleMillisInDay()219 public int getRuleMillisInDay() { 220 return millisInDay; 221 } 222 223 private static final String[] DOWSTR = {"", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 224 private static final String[] MONSTR = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 225 226 /** 227 * Returns a <code>String</code> representation of this <code>DateTimeRule</code> object. 228 * This method is used for debugging purpose only. The string representation can be changed 229 * in future version of ICU without any notice. 230 */ toString()231 public String toString() { 232 String sDate = null; 233 String sTimeRuleType = null; 234 235 switch (dateRuleType) { 236 case DOM: 237 sDate = Integer.toString(dayOfMonth); 238 break; 239 case DOW: 240 sDate = Integer.toString(weekInMonth) + DOWSTR[dayOfWeek]; 241 break; 242 case DOW_GEQ_DOM: 243 sDate = DOWSTR[dayOfWeek] + ">=" + Integer.toString(dayOfMonth); 244 break; 245 case DOW_LEQ_DOM: 246 sDate = DOWSTR[dayOfWeek] + "<=" + Integer.toString(dayOfMonth); 247 break; 248 } 249 250 switch (timeRuleType) { 251 case WALL_TIME: 252 sTimeRuleType = "WALL"; 253 break; 254 case STANDARD_TIME: 255 sTimeRuleType = "STD"; 256 break; 257 case UTC_TIME: 258 sTimeRuleType = "UTC"; 259 break; 260 } 261 262 int time = millisInDay; 263 int millis = time % 1000; 264 time /= 1000; 265 int secs = time % 60; 266 time /= 60; 267 int mins = time % 60; 268 int hours = time / 60; 269 270 StringBuilder buf = new StringBuilder(); 271 buf.append("month="); 272 buf.append(MONSTR[month]); 273 buf.append(", date="); 274 buf.append(sDate); 275 buf.append(", time="); 276 buf.append(hours); 277 buf.append(":"); 278 buf.append(mins/10); 279 buf.append(mins%10); 280 buf.append(":"); 281 buf.append(secs/10); 282 buf.append(secs%10); 283 buf.append("."); 284 buf.append(millis/100); 285 buf.append((millis/10)%10); 286 buf.append(millis%10); 287 buf.append("("); 288 buf.append(sTimeRuleType); 289 buf.append(")"); 290 return buf.toString(); 291 } 292 } 293