1 7 8 package java.sql; 9 10 39 public class Timestamp extends java.util.Date { 40 41 55 @Deprecated 56 public Timestamp(int year, int month, int date, 57 int hour, int minute, int second, int nano) { 58 super(year, month, date, hour, minute, second); 59 if (nano > 999999999 || nano < 0) { 60 throw new IllegalArgumentException ("nanos > 999999999 or < 0"); 61 } 62 nanos = nano; 63 } 64 65 77 public Timestamp(long time) { 78 super((time/1000)*1000); 79 nanos = (int)((time%1000) * 1000000); 80 if (nanos < 0) { 81 nanos = 1000000000 + nanos; 82 super.setTime(((time/1000)-1)*1000); 83 } 84 } 85 86 95 public void setTime(long time) { 96 super.setTime((time/1000)*1000); 97 nanos = (int)((time%1000) * 1000000); 98 if (nanos < 0) { 99 nanos = 1000000000 + nanos; 100 super.setTime(((time/1000)-1)*1000); 101 } 102 } 103 104 112 public long getTime() { 113 long time = super.getTime(); 114 return (time + (nanos / 1000000)); 115 } 116 117 118 121 private int nanos; 122 123 132 public static Timestamp valueOf(String s) { 133 String date_s; 134 String time_s; 135 String nanos_s; 136 int year; 137 int month; 138 int day; 139 int hour; 140 int minute; 141 int second; 142 int a_nanos = 0; 143 int firstDash; 144 int secondDash; 145 int dividingSpace; 146 int firstColon = 0; 147 int secondColon = 0; 148 int period = 0; 149 String formatError = "Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"; 150 String zeros = "000000000"; 151 152 if (s == null) throw new java.lang.IllegalArgumentException ("null string"); 153 154 s = s.trim(); 156 dividingSpace = s.indexOf(' '); 157 if (dividingSpace > 0) { 158 date_s = s.substring(0,dividingSpace); 159 time_s = s.substring(dividingSpace+1); 160 } else { 161 throw new java.lang.IllegalArgumentException (formatError); 162 } 163 164 165 firstDash = date_s.indexOf('-'); 167 secondDash = date_s.indexOf('-', firstDash+1); 168 169 if (time_s == null) 171 throw new java.lang.IllegalArgumentException (formatError); 172 firstColon = time_s.indexOf(':'); 173 secondColon = time_s.indexOf(':', firstColon+1); 174 period = time_s.indexOf('.', secondColon+1); 175 176 if ((firstDash > 0) & (secondDash > 0) & 178 (secondDash < date_s.length()-1)) { 179 year = Integer.parseInt(date_s.substring(0, firstDash)) - 1900; 180 month = 181 Integer.parseInt(date_s.substring 182 (firstDash+1, secondDash)) - 1; 183 day = Integer.parseInt(date_s.substring(secondDash+1)); 184 } else { 185 throw new java.lang.IllegalArgumentException (formatError); 186 } 187 188 if ((firstColon > 0) & (secondColon > 0) & 190 (secondColon < time_s.length()-1)) { 191 hour = Integer.parseInt(time_s.substring(0, firstColon)); 192 minute = 193 Integer.parseInt(time_s.substring(firstColon+1, secondColon)); 194 if ((period > 0) & (period < time_s.length()-1)) { 195 second = 196 Integer.parseInt(time_s.substring(secondColon+1, period)); 197 nanos_s = time_s.substring(period+1); 198 if (nanos_s.length() > 9) 199 throw new java.lang.IllegalArgumentException (formatError); 200 if (!Character.isDigit(nanos_s.charAt(0))) 201 throw new java.lang.IllegalArgumentException (formatError); 202 nanos_s = nanos_s + zeros.substring(0,9-nanos_s.length()); 203 a_nanos = Integer.parseInt(nanos_s); 204 } else if (period > 0) { 205 throw new java.lang.IllegalArgumentException (formatError); 206 } else { 207 second = Integer.parseInt(time_s.substring(secondColon+1)); 208 } 209 } else { 210 throw new java.lang.IllegalArgumentException (); 211 } 212 213 return new Timestamp (year, month, day, hour, minute, second, a_nanos); 214 } 215 216 238 public String toString () { 239 240 int year = super.getYear() + 1900; 241 int month = super.getMonth() + 1; 242 int day = super.getDate(); 243 int hour = super.getHours(); 244 int minute = super.getMinutes(); 245 int second = super.getSeconds(); 246 String yearString; 247 String monthString; 248 String dayString; 249 String hourString; 250 String minuteString; 251 String secondString; 252 String nanosString; 253 String zeros = "000000000"; 254 String yearZeros = "0000"; 255 StringBuffer timestampBuf; 256 257 if (year < 1000) { 258 yearString = "" + year; 260 yearString = yearZeros.substring(0, (4-yearString.length())) + 261 yearString; 262 } else { 263 yearString = "" + year; 264 } 265 if (month < 10) { 266 monthString = "0" + month; 267 } else { 268 monthString = Integer.toString(month); 269 } 270 if (day < 10) { 271 dayString = "0" + day; 272 } else { 273 dayString = Integer.toString(day); 274 } 275 if (hour < 10) { 276 hourString = "0" + hour; 277 } else { 278 hourString = Integer.toString(hour); 279 } 280 if (minute < 10) { 281 minuteString = "0" + minute; 282 } else { 283 minuteString = Integer.toString(minute); 284 } 285 if (second < 10) { 286 secondString = "0" + second; 287 } else { 288 secondString = Integer.toString(second); 289 } 290 if (nanos == 0) { 291 nanosString = "0"; 292 } else { 293 nanosString = Integer.toString(nanos); 294 295 nanosString = zeros.substring(0, (9-nanosString.length())) + 297 nanosString; 298 299 char[] nanosChar = new char[nanosString.length()]; 301 nanosString.getChars(0, nanosString.length(), nanosChar, 0); 302 int truncIndex = 8; 303 while (nanosChar[truncIndex] == '0') { 304 truncIndex--; 305 } 306 307 nanosString = new String (nanosChar, 0, truncIndex + 1); 308 } 309 310 timestampBuf = new StringBuffer (); 312 timestampBuf.append(yearString); 313 timestampBuf.append("-"); 314 timestampBuf.append(monthString); 315 timestampBuf.append("-"); 316 timestampBuf.append(dayString); 317 timestampBuf.append(" "); 318 timestampBuf.append(hourString); 319 timestampBuf.append(":"); 320 timestampBuf.append(minuteString); 321 timestampBuf.append(":"); 322 timestampBuf.append(secondString); 323 timestampBuf.append("."); 324 timestampBuf.append(nanosString); 325 326 return (timestampBuf.toString()); 327 } 328 329 335 public int getNanos() { 336 return nanos; 337 } 338 339 348 public void setNanos(int n) { 349 if (n > 999999999 || n < 0) { 350 throw new IllegalArgumentException ("nanos > 999999999 or < 0"); 351 } 352 nanos = n; 353 } 354 355 364 public boolean equals(Timestamp ts) { 365 if (super.equals(ts)) { 366 if (nanos == ts.nanos) { 367 return true; 368 } else { 369 return false; 370 } 371 } else { 372 return false; 373 } 374 } 375 376 393 public boolean equals(java.lang.Object ts) { 394 if (ts instanceof Timestamp ) { 395 return this.equals((Timestamp )ts); 396 } else { 397 return false; 398 } 399 } 400 401 409 public boolean before(Timestamp ts) { 410 return compareTo(ts) < 0; 411 } 412 413 421 public boolean after(Timestamp ts) { 422 return compareTo(ts) > 0; 423 } 424 425 438 public int compareTo(Timestamp ts) { 439 int i = super.compareTo(ts); 440 if (i == 0) { 441 if (nanos > ts.nanos) { 442 return 1; 443 } else if (nanos < ts.nanos) { 444 return -1; 445 } 446 } 447 return i; 448 449 } 450 451 469 public int compareTo(java.util.Date o) { 470 if(o instanceof Timestamp ) { 471 return compareTo((Timestamp )o); 475 } else { 476 Timestamp ts = new Timestamp (o.getTime()); 479 return this.compareTo(ts); 480 } 481 } 482 483 static final long serialVersionUID = 2745179027874758501L; 484 485 } 486 487 | Popular Tags |