1 16 package org.joda.time.chrono; 17 18 import java.io.Serializable ; 19 20 import org.joda.time.Chronology; 21 import org.joda.time.DateTimeField; 22 import org.joda.time.DateTimeFieldType; 23 import org.joda.time.DateTimeZone; 24 import org.joda.time.DurationField; 25 import org.joda.time.DurationFieldType; 26 import org.joda.time.IllegalFieldValueException; 27 import org.joda.time.ReadablePartial; 28 import org.joda.time.ReadablePeriod; 29 import org.joda.time.field.FieldUtils; 30 import org.joda.time.field.UnsupportedDateTimeField; 31 import org.joda.time.field.UnsupportedDurationField; 32 33 43 public abstract class BaseChronology 44 extends Chronology 45 implements Serializable { 46 47 48 private static final long serialVersionUID = -7310865996721419676L; 49 50 53 protected BaseChronology() { 54 super(); 55 } 56 57 63 public abstract DateTimeZone getZone(); 64 65 72 public abstract Chronology withUTC(); 73 74 81 public abstract Chronology withZone(DateTimeZone zone); 82 83 98 public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth, 99 int millisOfDay) 100 throws IllegalArgumentException 101 { 102 long instant = year().set(0, year); 103 instant = monthOfYear().set(instant, monthOfYear); 104 instant = dayOfMonth().set(instant, dayOfMonth); 105 return millisOfDay().set(instant, millisOfDay); 106 } 107 108 127 public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth, 128 int hourOfDay, int minuteOfHour, 129 int secondOfMinute, int millisOfSecond) 130 throws IllegalArgumentException 131 { 132 long instant = year().set(0, year); 133 instant = monthOfYear().set(instant, monthOfYear); 134 instant = dayOfMonth().set(instant, dayOfMonth); 135 instant = hourOfDay().set(instant, hourOfDay); 136 instant = minuteOfHour().set(instant, minuteOfHour); 137 instant = secondOfMinute().set(instant, secondOfMinute); 138 return millisOfSecond().set(instant, millisOfSecond); 139 } 140 141 158 public long getDateTimeMillis(long instant, 159 int hourOfDay, int minuteOfHour, 160 int secondOfMinute, int millisOfSecond) 161 throws IllegalArgumentException 162 { 163 instant = hourOfDay().set(instant, hourOfDay); 164 instant = minuteOfHour().set(instant, minuteOfHour); 165 instant = secondOfMinute().set(instant, secondOfMinute); 166 return millisOfSecond().set(instant, millisOfSecond); 167 } 168 169 180 public void validate(ReadablePartial partial, int[] values) { 181 int size = partial.size(); 184 for (int i = 0; i < size; i++) { 185 int value = values[i]; 186 DateTimeField field = partial.getField(i); 187 if (value < field.getMinimumValue()) { 188 throw new IllegalFieldValueException 189 (field.getType(), new Integer (value), 190 new Integer (field.getMinimumValue()), null); 191 } 192 if (value > field.getMaximumValue()) { 193 throw new IllegalFieldValueException 194 (field.getType(), new Integer (value), 195 null, new Integer (field.getMaximumValue())); 196 } 197 } 198 for (int i = 0; i < size; i++) { 200 int value = values[i]; 201 DateTimeField field = partial.getField(i); 202 if (value < field.getMinimumValue(partial, values)) { 203 throw new IllegalFieldValueException 204 (field.getType(), new Integer (value), 205 new Integer (field.getMinimumValue(partial, values)), null); 206 } 207 if (value > field.getMaximumValue(partial, values)) { 208 throw new IllegalFieldValueException 209 (field.getType(), new Integer (value), 210 null, new Integer (field.getMaximumValue(partial, values))); 211 } 212 } 213 } 214 215 222 public int[] get(ReadablePartial partial, long instant) { 223 int size = partial.size(); 224 int[] values = new int[size]; 225 for (int i = 0; i < size; i++) { 226 values[i] = partial.getFieldType(i).getField(this).get(instant); 227 } 228 return values; 229 } 230 231 238 public long set(ReadablePartial partial, long instant) { 239 for (int i = 0, isize = partial.size(); i < isize; i++) { 240 instant = partial.getFieldType(i).getField(this).set(instant, partial.getValue(i)); 241 } 242 return instant; 243 } 244 245 254 public int[] get(ReadablePeriod period, long startInstant, long endInstant) { 255 int size = period.size(); 256 int[] values = new int[size]; 257 if (startInstant != endInstant) { 258 for (int i = 0; i < size; i++) { 259 DurationField field = period.getFieldType(i).getField(this); 260 int value = field.getDifference(endInstant, startInstant); 261 startInstant = field.add(startInstant, value); 262 values[i] = value; 263 } 264 } 265 return values; 266 } 267 268 275 public int[] get(ReadablePeriod period, long duration) { 276 int size = period.size(); 277 int[] values = new int[size]; 278 if (duration != 0) { 279 long current = 0; 280 for (int i = 0; i < size; i++) { 281 DurationField field = period.getFieldType(i).getField(this); 282 if (field.isPrecise()) { 283 int value = field.getDifference(duration, current); 284 current = field.add(current, value); 285 values[i] = value; 286 } 287 } 288 } 289 return values; 290 } 291 292 300 public long add(ReadablePeriod period, long instant, int scalar) { 301 if (scalar != 0 && period != null) { 302 for (int i = 0, isize = period.size(); i < isize; i++) { 303 long value = period.getValue(i); if (value != 0) { 305 instant = period.getFieldType(i).getField(this).add(instant, value * scalar); 306 } 307 } 308 } 309 return instant; 310 } 311 312 321 public long add(long instant, long duration, int scalar) { 322 if (duration == 0 || scalar == 0) { 323 return instant; 324 } 325 long add = FieldUtils.safeMultiply(duration, scalar); 326 return FieldUtils.safeAdd(instant, add); 327 } 328 329 336 public DurationField millis() { 337 return UnsupportedDurationField.getInstance(DurationFieldType.millis()); 338 } 339 340 345 public DateTimeField millisOfSecond() { 346 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.millisOfSecond(), millis()); 347 } 348 349 354 public DateTimeField millisOfDay() { 355 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.millisOfDay(), millis()); 356 } 357 358 365 public DurationField seconds() { 366 return UnsupportedDurationField.getInstance(DurationFieldType.seconds()); 367 } 368 369 374 public DateTimeField secondOfMinute() { 375 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.secondOfMinute(), seconds()); 376 } 377 378 383 public DateTimeField secondOfDay() { 384 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.secondOfDay(), seconds()); 385 } 386 387 394 public DurationField minutes() { 395 return UnsupportedDurationField.getInstance(DurationFieldType.minutes()); 396 } 397 398 403 public DateTimeField minuteOfHour() { 404 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.minuteOfHour(), minutes()); 405 } 406 407 412 public DateTimeField minuteOfDay() { 413 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.minuteOfDay(), minutes()); 414 } 415 416 423 public DurationField hours() { 424 return UnsupportedDurationField.getInstance(DurationFieldType.hours()); 425 } 426 427 432 public DateTimeField hourOfDay() { 433 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.hourOfDay(), hours()); 434 } 435 436 441 public DateTimeField clockhourOfDay() { 442 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.clockhourOfDay(), hours()); 443 } 444 445 452 public DurationField halfdays() { 453 return UnsupportedDurationField.getInstance(DurationFieldType.halfdays()); 454 } 455 456 461 public DateTimeField hourOfHalfday() { 462 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.hourOfHalfday(), hours()); 463 } 464 465 470 public DateTimeField clockhourOfHalfday() { 471 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.clockhourOfHalfday(), hours()); 472 } 473 474 479 public DateTimeField halfdayOfDay() { 480 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.halfdayOfDay(), halfdays()); 481 } 482 483 490 public DurationField days() { 491 return UnsupportedDurationField.getInstance(DurationFieldType.days()); 492 } 493 494 503 public DateTimeField dayOfWeek() { 504 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.dayOfWeek(), days()); 505 } 506 507 512 public DateTimeField dayOfMonth() { 513 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.dayOfMonth(), days()); 514 } 515 516 521 public DateTimeField dayOfYear() { 522 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.dayOfYear(), days()); 523 } 524 525 532 public DurationField weeks() { 533 return UnsupportedDurationField.getInstance(DurationFieldType.weeks()); 534 } 535 536 541 public DateTimeField weekOfWeekyear() { 542 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.weekOfWeekyear(), weeks()); 543 } 544 545 552 public DurationField weekyears() { 553 return UnsupportedDurationField.getInstance(DurationFieldType.weekyears()); 554 } 555 556 561 public DateTimeField weekyear() { 562 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.weekyear(), weekyears()); 563 } 564 565 570 public DateTimeField weekyearOfCentury() { 571 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.weekyearOfCentury(), weekyears()); 572 } 573 574 581 public DurationField months() { 582 return UnsupportedDurationField.getInstance(DurationFieldType.months()); 583 } 584 585 590 public DateTimeField monthOfYear() { 591 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.monthOfYear(), months()); 592 } 593 594 601 public DurationField years() { 602 return UnsupportedDurationField.getInstance(DurationFieldType.years()); 603 } 604 605 610 public DateTimeField year() { 611 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.year(), years()); 612 } 613 614 619 public DateTimeField yearOfEra() { 620 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.yearOfEra(), years()); 621 } 622 623 628 public DateTimeField yearOfCentury() { 629 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.yearOfCentury(), years()); 630 } 631 632 639 public DurationField centuries() { 640 return UnsupportedDurationField.getInstance(DurationFieldType.centuries()); 641 } 642 643 648 public DateTimeField centuryOfEra() { 649 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.centuryOfEra(), centuries()); 650 } 651 652 659 public DurationField eras() { 660 return UnsupportedDurationField.getInstance(DurationFieldType.eras()); 661 } 662 663 668 public DateTimeField era() { 669 return UnsupportedDateTimeField.getInstance(DateTimeFieldType.era(), eras()); 670 } 671 672 678 public abstract String toString(); 679 680 } 681 | Popular Tags |