1 16 package org.joda.time; 17 18 import java.io.Serializable ; 19 20 import org.joda.time.base.BasePeriod; 21 22 52 public final class Period 53 extends BasePeriod 54 implements ReadablePeriod, Serializable { 55 56 60 public static final Period ZERO = new Period(); 61 62 63 private static final long serialVersionUID = 741052353876488155L; 64 65 79 public static Period years(int years) { 80 return new Period(new int[] {years, 0, 0, 0, 0, 0, 0, 0, 0}, PeriodType.standard()); 81 } 82 83 96 public static Period months(int months) { 97 return new Period(new int[] {0, months, 0, 0, 0, 0, 0, 0}, PeriodType.standard()); 98 } 99 100 113 public static Period weeks(int weeks) { 114 return new Period(new int[] {0, 0, weeks, 0, 0, 0, 0, 0}, PeriodType.standard()); 115 } 116 117 130 public static Period days(int days) { 131 return new Period(new int[] {0, 0, 0, days, 0, 0, 0, 0}, PeriodType.standard()); 132 } 133 134 147 public static Period hours(int hours) { 148 return new Period(new int[] {0, 0, 0, 0, hours, 0, 0, 0}, PeriodType.standard()); 149 } 150 151 164 public static Period minutes(int minutes) { 165 return new Period(new int[] {0, 0, 0, 0, 0, minutes, 0, 0}, PeriodType.standard()); 166 } 167 168 181 public static Period seconds(int seconds) { 182 return new Period(new int[] {0, 0, 0, 0, 0, 0, seconds, 0}, PeriodType.standard()); 183 } 184 185 195 public static Period millis(int millis) { 196 return new Period(new int[] {0, 0, 0, 0, 0, 0, 0, millis}, PeriodType.standard()); 197 } 198 199 226 public static Period fieldDifference(ReadablePartial start, ReadablePartial end) { 227 if (start == null || end == null) { 228 throw new IllegalArgumentException ("ReadablePartial objects must not be null"); 229 } 230 if (start.size() != end.size()) { 231 throw new IllegalArgumentException ("ReadablePartial objects must have the same set of fields"); 232 } 233 DurationFieldType[] types = new DurationFieldType[start.size()]; 234 int[] values = new int[start.size()]; 235 for (int i = 0, isize = start.size(); i < isize; i++) { 236 if (start.getFieldType(i) != end.getFieldType(i)) { 237 throw new IllegalArgumentException ("ReadablePartial objects must have the same set of fields"); 238 } 239 types[i] = start.getFieldType(i).getDurationType(); 240 if (i > 0 && types[i - 1] == types[i]) { 241 throw new IllegalArgumentException ("ReadablePartial objects must not have overlapping fields"); 242 } 243 values[i] = end.getValue(i) - start.getValue(i); 244 } 245 return new Period(values, PeriodType.forFields(types)); 246 } 247 248 267 public Period() { 268 super(0L, null, null); 269 } 270 271 281 public Period(int hours, int minutes, int seconds, int millis) { 282 super(0, 0, 0, 0, hours, minutes, seconds, millis, PeriodType.standard()); 283 } 284 285 297 public Period(int years, int months, int weeks, int days, 298 int hours, int minutes, int seconds, int millis) { 299 super(years, months, weeks, days, hours, minutes, seconds, millis, PeriodType.standard()); 300 } 301 302 320 public Period(int years, int months, int weeks, int days, 321 int hours, int minutes, int seconds, int millis, PeriodType type) { 322 super(years, months, weeks, days, hours, minutes, seconds, millis, type); 323 } 324 325 351 public Period(long duration) { 352 super(duration, null, null); 353 } 354 355 371 public Period(long duration, PeriodType type) { 372 super(duration, type, null); 373 } 374 375 392 public Period(long duration, Chronology chronology) { 393 super(duration, null, chronology); 394 } 395 396 413 public Period(long duration, PeriodType type, Chronology chronology) { 414 super(duration, type, chronology); 415 } 416 417 424 public Period(long startInstant, long endInstant) { 425 super(startInstant, endInstant, null, null); 426 } 427 428 435 public Period(long startInstant, long endInstant, PeriodType type) { 436 super(startInstant, endInstant, type, null); 437 } 438 439 447 public Period(long startInstant, long endInstant, Chronology chrono) { 448 super(startInstant, endInstant, null, chrono); 449 } 450 451 459 public Period(long startInstant, long endInstant, PeriodType type, Chronology chrono) { 460 super(startInstant, endInstant, type, chrono); 461 } 462 463 470 public Period(ReadableInstant startInstant, ReadableInstant endInstant) { 471 super(startInstant, endInstant, null); 472 } 473 474 481 public Period(ReadableInstant startInstant, ReadableInstant endInstant, PeriodType type) { 482 super(startInstant, endInstant, type); 483 } 484 485 506 public Period(ReadablePartial start, ReadablePartial end) { 507 super(start, end, null); 508 } 509 510 532 public Period(ReadablePartial start, ReadablePartial end, PeriodType type) { 533 super(start, end, type); 534 } 535 536 542 public Period(ReadableInstant startInstant, ReadableDuration duration) { 543 super(startInstant, duration, null); 544 } 545 546 553 public Period(ReadableInstant startInstant, ReadableDuration duration, PeriodType type) { 554 super(startInstant, duration, type); 555 } 556 557 563 public Period(ReadableDuration duration, ReadableInstant endInstant) { 564 super(duration, endInstant, null); 565 } 566 567 574 public Period(ReadableDuration duration, ReadableInstant endInstant, PeriodType type) { 575 super(duration, endInstant, type); 576 } 577 578 586 public Period(Object period) { 587 super(period, null, null); 588 } 589 590 599 public Period(Object period, PeriodType type) { 600 super(period, type, null); 601 } 602 603 612 public Period(Object period, Chronology chrono) { 613 super(period, null, chrono); 614 } 615 616 626 public Period(Object period, PeriodType type, Chronology chrono) { 627 super(period, type, chrono); 628 } 629 630 636 private Period(int[] values, PeriodType type) { 637 super(values, type); 638 } 639 640 647 public Period toPeriod() { 648 return this; 649 } 650 651 657 public int getYears() { 658 return getPeriodType().getIndexedField(this, PeriodType.YEAR_INDEX); 659 } 660 661 666 public int getMonths() { 667 return getPeriodType().getIndexedField(this, PeriodType.MONTH_INDEX); 668 } 669 670 675 public int getWeeks() { 676 return getPeriodType().getIndexedField(this, PeriodType.WEEK_INDEX); 677 } 678 679 684 public int getDays() { 685 return getPeriodType().getIndexedField(this, PeriodType.DAY_INDEX); 686 } 687 688 694 public int getHours() { 695 return getPeriodType().getIndexedField(this, PeriodType.HOUR_INDEX); 696 } 697 698 703 public int getMinutes() { 704 return getPeriodType().getIndexedField(this, PeriodType.MINUTE_INDEX); 705 } 706 707 712 public int getSeconds() { 713 return getPeriodType().getIndexedField(this, PeriodType.SECOND_INDEX); 714 } 715 716 721 public int getMillis() { 722 return getPeriodType().getIndexedField(this, PeriodType.MILLI_INDEX); 723 } 724 725 736 public Period withPeriodType(PeriodType type) { 737 type = DateTimeUtils.getPeriodType(type); 738 if (type.equals(getPeriodType())) { 739 return this; 740 } 741 return new Period(this, type); 742 } 743 744 754 public Period withFields(ReadablePeriod period) { 755 if (period == null) { 756 return this; 757 } 758 int[] newValues = getValues(); newValues = super.mergePeriodInto(newValues, period); 760 return new Period(newValues, getPeriodType()); 761 } 762 763 774 public Period withField(DurationFieldType field, int value) { 775 if (field == null) { 776 throw new IllegalArgumentException ("Field must not be null"); 777 } 778 int[] newValues = getValues(); super.setFieldInto(newValues, field, value); 780 return new Period(newValues, getPeriodType()); 781 } 782 783 793 public Period withFieldAdded(DurationFieldType field, int value) { 794 if (field == null) { 795 throw new IllegalArgumentException ("Field must not be null"); 796 } 797 if (value == 0) { 798 return this; 799 } 800 int[] newValues = getValues(); super.addFieldInto(newValues, field, value); 802 return new Period(newValues, getPeriodType()); 803 } 804 805 815 public Period withYears(int years) { 816 int[] values = getValues(); getPeriodType().setIndexedField(this, PeriodType.YEAR_INDEX, values, years); 818 return new Period(values, getPeriodType()); 819 } 820 821 830 public Period withMonths(int months) { 831 int[] values = getValues(); getPeriodType().setIndexedField(this, PeriodType.MONTH_INDEX, values, months); 833 return new Period(values, getPeriodType()); 834 } 835 836 845 public Period withWeeks(int weeks) { 846 int[] values = getValues(); getPeriodType().setIndexedField(this, PeriodType.WEEK_INDEX, values, weeks); 848 return new Period(values, getPeriodType()); 849 } 850 851 860 public Period withDays(int days) { 861 int[] values = getValues(); getPeriodType().setIndexedField(this, PeriodType.DAY_INDEX, values, days); 863 return new Period(values, getPeriodType()); 864 } 865 866 875 public Period withHours(int hours) { 876 int[] values = getValues(); getPeriodType().setIndexedField(this, PeriodType.HOUR_INDEX, values, hours); 878 return new Period(values, getPeriodType()); 879 } 880 881 890 public Period withMinutes(int minutes) { 891 int[] values = getValues(); getPeriodType().setIndexedField(this, PeriodType.MINUTE_INDEX, values, minutes); 893 return new Period(values, getPeriodType()); 894 } 895 896 905 public Period withSeconds(int seconds) { 906 int[] values = getValues(); getPeriodType().setIndexedField(this, PeriodType.SECOND_INDEX, values, seconds); 908 return new Period(values, getPeriodType()); 909 } 910 911 920 public Period withMillis(int millis) { 921 int[] values = getValues(); getPeriodType().setIndexedField(this, PeriodType.MILLI_INDEX, values, millis); 923 return new Period(values, getPeriodType()); 924 } 925 926 936 public Period plusYears(int years) { 937 if (years == 0) { 938 return this; 939 } 940 int[] values = getValues(); getPeriodType().addIndexedField(this, PeriodType.YEAR_INDEX, values, years); 942 return new Period(values, getPeriodType()); 943 } 944 945 954 public Period plusMonths(int months) { 955 if (months == 0) { 956 return this; 957 } 958 int[] values = getValues(); getPeriodType().addIndexedField(this, PeriodType.MONTH_INDEX, values, months); 960 return new Period(values, getPeriodType()); 961 } 962 963 972 public Period plusWeeks(int weeks) { 973 if (weeks == 0) { 974 return this; 975 } 976 int[] values = getValues(); getPeriodType().addIndexedField(this, PeriodType.WEEK_INDEX, values, weeks); 978 return new Period(values, getPeriodType()); 979 } 980 981 990 public Period plusDays(int days) { 991 if (days == 0) { 992 return this; 993 } 994 int[] values = getValues(); getPeriodType().addIndexedField(this, PeriodType.DAY_INDEX, values, days); 996 return new Period(values, getPeriodType()); 997 } 998 999 1008 public Period plusHours(int hours) { 1009 if (hours == 0) { 1010 return this; 1011 } 1012 int[] values = getValues(); getPeriodType().addIndexedField(this, PeriodType.HOUR_INDEX, values, hours); 1014 return new Period(values, getPeriodType()); 1015 } 1016 1017 1026 public Period plusMinutes(int minutes) { 1027 if (minutes == 0) { 1028 return this; 1029 } 1030 int[] values = getValues(); getPeriodType().addIndexedField(this, PeriodType.MINUTE_INDEX, values, minutes); 1032 return new Period(values, getPeriodType()); 1033 } 1034 1035 1044 public Period plusSeconds(int seconds) { 1045 if (seconds == 0) { 1046 return this; 1047 } 1048 int[] values = getValues(); getPeriodType().addIndexedField(this, PeriodType.SECOND_INDEX, values, seconds); 1050 return new Period(values, getPeriodType()); 1051 } 1052 1053 1062 public Period plusMillis(int millis) { 1063 if (millis == 0) { 1064 return this; 1065 } 1066 int[] values = getValues(); getPeriodType().addIndexedField(this, PeriodType.MILLI_INDEX, values, millis); 1068 return new Period(values, getPeriodType()); 1069 } 1070 1071 1081 public Period minusYears(int years) { 1082 return plusYears(-years); 1083 } 1084 1085 1094 public Period minusMonths(int months) { 1095 return plusMonths(-months); 1096 } 1097 1098 1107 public Period minusWeeks(int weeks) { 1108 return plusWeeks(-weeks); 1109 } 1110 1111 1120 public Period minusDays(int days) { 1121 return plusDays(-days); 1122 } 1123 1124 1133 public Period minusHours(int hours) { 1134 return plusHours(-hours); 1135 } 1136 1137 1146 public Period minusMinutes(int minutes) { 1147 return plusMinutes(-minutes); 1148 } 1149 1150 1159 public Period minusSeconds(int seconds) { 1160 return plusSeconds(-seconds); 1161 } 1162 1163 1172 public Period minusMillis(int millis) { 1173 return plusMillis(-millis); 1174 } 1175 1176} 1177 | Popular Tags |