1 16 17 package com.google.gwt.i18n.client; 18 19 import java.util.Date ; 20 21 27 class DateRecord extends Date { 28 29 34 private static final long serialVersionUID = -1278816193740448162L; 35 36 public static final int AM = 0; 37 public static final int PM = 1; 38 39 private static final int JS_START_YEAR = 1900; 40 41 private int era; 42 private int year; 43 private int month; 44 private int dayOfMonth; 45 private int ampm; 46 private int hours; 47 private int minutes; 48 private int seconds; 49 private int milliseconds; 50 51 private int tzOffset; 52 private int dayOfWeek; 53 private boolean ambiguousYear; 54 55 59 public DateRecord() { 60 era = -1; 61 ambiguousYear = false; 62 year = Integer.MIN_VALUE; 63 month = -1; 64 dayOfMonth = -1; 65 ampm = -1; 66 hours = -1; 67 minutes = -1; 68 seconds = -1; 69 milliseconds = -1; 70 dayOfWeek = -1; 71 tzOffset = Integer.MIN_VALUE; 72 }; 73 74 84 public boolean calcDate(Date date) { 85 if (this.era == 0 && this.year > 0) { 87 this.year = -(this.year - 1); 88 } 89 90 if (this.year > Integer.MIN_VALUE) { 91 date.setYear(this.year - JS_START_YEAR); 92 } 93 94 int orgDayOfMonth = date.getDate(); 101 date.setDate(1); 102 103 if (this.month >= 0) { 104 date.setMonth(this.month); 105 } 106 107 if (this.dayOfMonth >= 0) { 108 date.setDate(this.dayOfMonth); 109 } else { 110 date.setDate(orgDayOfMonth); 111 } 112 113 if (this.hours < 0) { 115 this.hours = date.getHours(); 116 } 117 118 if (this.ampm > 0) { 119 if (this.hours < 12) { 120 this.hours += 12; 121 } 122 } 123 date.setHours(this.hours); 124 125 if (this.minutes >= 0) { 126 date.setMinutes(this.minutes); 127 } 128 129 if (this.seconds >= 0) { 130 date.setSeconds(this.seconds); 131 } 132 133 if (this.milliseconds >= 0) { 134 date.setTime(date.getTime() / 1000 * 1000 + this.milliseconds); 135 } 136 137 if (this.tzOffset > Integer.MIN_VALUE) { 139 int offset = date.getTimezoneOffset(); 140 date.setTime(date.getTime() + (this.tzOffset - offset) * 60 * 1000); 141 } 143 144 if (this.ambiguousYear) { Date defaultCenturyStart = new Date (); 147 defaultCenturyStart.setYear(defaultCenturyStart.getYear() - 80); 148 if (date.before(defaultCenturyStart)) { 149 date.setYear(defaultCenturyStart.getYear() + 100); 150 } 151 } 152 153 if (this.dayOfWeek >= 0) { 158 if (this.dayOfMonth == -1) { 159 int adjustment = (7 + this.dayOfWeek - date.getDay()) % 7; 161 if (adjustment > 3) { 162 adjustment -= 7; 163 } 164 int orgMonth = date.getMonth(); 165 date.setDate(date.getDate() + adjustment); 166 167 if (date.getMonth() != orgMonth) { 171 date.setDate(date.getDate() + (adjustment > 0 ? -7 : 7)); 172 } 173 } else { 174 if (date.getDay() != this.dayOfWeek) { 175 return false; 176 } 177 } 178 } 179 return true; 180 } 181 182 189 public void setAmbiguousYear(boolean ambiguousYear) { 190 this.ambiguousYear = ambiguousYear; 191 } 192 193 198 public void setAmpm(int ampm) { 199 this.ampm = ampm; 200 } 201 202 207 public void setDayOfMonth(int day) { 208 this.dayOfMonth = day; 209 } 210 211 216 public void setDayOfWeek(int dayOfWeek) { 217 this.dayOfWeek = dayOfWeek; 218 } 219 220 225 public void setEra(int era) { 226 this.era = era; 227 } 228 229 234 public void setHours(int hours) { 235 this.hours = hours; 236 } 237 238 243 public void setMilliseconds(int milliseconds) { 244 this.milliseconds = milliseconds; 245 } 246 247 252 public void setMinutes(int minutes) { 253 this.minutes = minutes; 254 } 255 256 261 public void setMonth(int month) { 262 this.month = month; 263 } 264 265 270 public void setSeconds(int seconds) { 271 this.seconds = seconds; 272 } 273 274 279 public void setTzOffset(int tzOffset) { 280 this.tzOffset = tzOffset; 281 } 282 283 288 public void setYear(int value) { 289 this.year = value; 290 } 291 } 292 | Popular Tags |