1 16 package org.joda.time; 17 18 import java.io.Serializable ; 19 import java.util.Calendar ; 20 import java.util.Date ; 21 import java.util.Locale ; 22 23 import org.joda.time.base.BasePartial; 24 import org.joda.time.chrono.ISOChronology; 25 import org.joda.time.field.AbstractPartialFieldProperty; 26 import org.joda.time.field.FieldUtils; 27 import org.joda.time.format.ISODateTimeFormat; 28 29 67 public final class YearMonthDay 68 extends BasePartial 69 implements ReadablePartial, Serializable { 70 71 72 private static final long serialVersionUID = 797544782896179L; 73 74 private static final DateTimeFieldType[] FIELD_TYPES = new DateTimeFieldType[] { 75 DateTimeFieldType.year(), 76 DateTimeFieldType.monthOfYear(), 77 DateTimeFieldType.dayOfMonth(), 78 }; 79 80 81 public static final int YEAR = 0; 82 83 public static final int MONTH_OF_YEAR = 1; 84 85 public static final int DAY_OF_MONTH = 2; 86 87 107 public static YearMonthDay fromCalendarFields(Calendar calendar) { 108 if (calendar == null) { 109 throw new IllegalArgumentException ("The calendar must not be null"); 110 } 111 return new YearMonthDay( 112 calendar.get(Calendar.YEAR), 113 calendar.get(Calendar.MONTH) + 1, 114 calendar.get(Calendar.DAY_OF_MONTH) 115 ); 116 } 117 118 134 public static YearMonthDay fromDateFields(Date date) { 135 if (date == null) { 136 throw new IllegalArgumentException ("The date must not be null"); 137 } 138 return new YearMonthDay( 139 date.getYear() + 1900, 140 date.getMonth() + 1, 141 date.getDate() 142 ); 143 } 144 145 154 public YearMonthDay() { 155 super(); 156 } 157 158 169 public YearMonthDay(DateTimeZone zone) { 170 super(ISOChronology.getInstance(zone)); 171 } 172 173 183 public YearMonthDay(Chronology chronology) { 184 super(chronology); 185 } 186 187 197 public YearMonthDay(long instant) { 198 super(instant); 199 } 200 201 212 public YearMonthDay(long instant, Chronology chronology) { 213 super(instant, chronology); 214 } 215 216 232 public YearMonthDay(Object instant) { 233 super(instant, null, ISODateTimeFormat.dateOptionalTimeParser()); 234 } 235 236 257 public YearMonthDay(Object instant, Chronology chronology) { 258 super(instant, DateTimeUtils.getChronology(chronology), ISODateTimeFormat.dateOptionalTimeParser()); 259 } 260 261 273 public YearMonthDay(int year, int monthOfYear, int dayOfMonth) { 274 this(year, monthOfYear, dayOfMonth, null); 275 } 276 277 289 public YearMonthDay(int year, int monthOfYear, int dayOfMonth, Chronology chronology) { 290 super(new int[] {year, monthOfYear, dayOfMonth}, chronology); 291 } 292 293 299 YearMonthDay(YearMonthDay partial, int[] values) { 300 super(partial, values); 301 } 302 303 309 YearMonthDay(YearMonthDay partial, Chronology chrono) { 310 super(partial, chrono); 311 } 312 313 319 public int size() { 320 return 3; 321 } 322 323 332 protected DateTimeField getField(int index, Chronology chrono) { 333 switch (index) { 334 case YEAR: 335 return chrono.year(); 336 case MONTH_OF_YEAR: 337 return chrono.monthOfYear(); 338 case DAY_OF_MONTH: 339 return chrono.dayOfMonth(); 340 default: 341 throw new IndexOutOfBoundsException ("Invalid index: " + index); 342 } 343 } 344 345 352 public DateTimeFieldType getFieldType(int index) { 353 return FIELD_TYPES[index]; 354 } 355 356 363 public DateTimeFieldType[] getFieldTypes() { 364 return (DateTimeFieldType[]) FIELD_TYPES.clone(); 365 } 366 367 382 public YearMonthDay withChronologyRetainFields(Chronology newChronology) { 383 newChronology = DateTimeUtils.getChronology(newChronology); 384 newChronology = newChronology.withUTC(); 385 if (newChronology == getChronology()) { 386 return this; 387 } else { 388 YearMonthDay newYearMonthDay = new YearMonthDay(this, newChronology); 389 newChronology.validate(newYearMonthDay, getValues()); 390 return newYearMonthDay; 391 } 392 } 393 394 412 public YearMonthDay withField(DateTimeFieldType fieldType, int value) { 413 int index = indexOfSupported(fieldType); 414 if (value == getValue(index)) { 415 return this; 416 } 417 int[] newValues = getValues(); 418 newValues = getField(index).set(this, index, newValues, value); 419 return new YearMonthDay(this, newValues); 420 } 421 422 440 public YearMonthDay withFieldAdded(DurationFieldType fieldType, int amount) { 441 int index = indexOfSupported(fieldType); 442 if (amount == 0) { 443 return this; 444 } 445 int[] newValues = getValues(); 446 newValues = getField(index).add(this, index, newValues, amount); 447 return new YearMonthDay(this, newValues); 448 } 449 450 466 public YearMonthDay withPeriodAdded(ReadablePeriod period, int scalar) { 467 if (period == null || scalar == 0) { 468 return this; 469 } 470 int[] newValues = getValues(); 471 for (int i = 0; i < period.size(); i++) { 472 DurationFieldType fieldType = period.getFieldType(i); 473 int index = indexOf(fieldType); 474 if (index >= 0) { 475 newValues = getField(index).add(this, index, newValues, 476 FieldUtils.safeMultiply(period.getValue(i), scalar)); 477 } 478 } 479 return new YearMonthDay(this, newValues); 480 } 481 482 496 public YearMonthDay plus(ReadablePeriod period) { 497 return withPeriodAdded(period, 1); 498 } 499 500 517 public YearMonthDay plusYears(int years) { 518 return withFieldAdded(DurationFieldType.years(), years); 519 } 520 521 537 public YearMonthDay plusMonths(int months) { 538 return withFieldAdded(DurationFieldType.months(), months); 539 } 540 541 557 public YearMonthDay plusDays(int days) { 558 return withFieldAdded(DurationFieldType.days(), days); 559 } 560 561 575 public YearMonthDay minus(ReadablePeriod period) { 576 return withPeriodAdded(period, -1); 577 } 578 579 596 public YearMonthDay minusYears(int years) { 597 return withFieldAdded(DurationFieldType.years(), FieldUtils.safeNegate(years)); 598 } 599 600 616 public YearMonthDay minusMonths(int months) { 617 return withFieldAdded(DurationFieldType.months(), FieldUtils.safeNegate(months)); 618 } 619 620 636 public YearMonthDay minusDays(int days) { 637 return withFieldAdded(DurationFieldType.days(), FieldUtils.safeNegate(days)); 638 } 639 640 649 public Property property(DateTimeFieldType type) { 650 return new Property(this, indexOfSupported(type)); 651 } 652 653 660 public LocalDate toLocalDate() { 661 return new LocalDate(getYear(), getMonthOfYear(), getDayOfMonth(), getChronology()); 662 } 663 664 671 public DateTime toDateTimeAtMidnight() { 672 return toDateTimeAtMidnight(null); 673 } 674 675 685 public DateTime toDateTimeAtMidnight(DateTimeZone zone) { 686 Chronology chrono = getChronology().withZone(zone); 687 return new DateTime(getYear(), getMonthOfYear(), getDayOfMonth(), 0, 0, 0, 0, chrono); 688 } 689 690 698 public DateTime toDateTimeAtCurrentTime() { 699 return toDateTimeAtCurrentTime(null); 700 } 701 702 713 public DateTime toDateTimeAtCurrentTime(DateTimeZone zone) { 714 Chronology chrono = getChronology().withZone(zone); 715 long instantMillis = DateTimeUtils.currentTimeMillis(); 716 long resolved = chrono.set(this, instantMillis); 717 return new DateTime(resolved, chrono); 718 } 719 720 726 public DateMidnight toDateMidnight() { 727 return toDateMidnight(null); 728 } 729 730 736 public DateMidnight toDateMidnight(DateTimeZone zone) { 737 Chronology chrono = getChronology().withZone(zone); 738 return new DateMidnight(getYear(), getMonthOfYear(), getDayOfMonth(), chrono); 739 } 740 741 754 public DateTime toDateTime(TimeOfDay time) { 755 return toDateTime(time, null); 756 } 757 758 771 public DateTime toDateTime(TimeOfDay time, DateTimeZone zone) { 772 Chronology chrono = getChronology().withZone(zone); 773 long instant = DateTimeUtils.currentTimeMillis(); 774 instant = chrono.set(this, instant); 775 if (time != null) { 776 instant = chrono.set(time, instant); 777 } 778 return new DateTime(instant, chrono); 779 } 780 781 788 public Interval toInterval() { 789 return toInterval(null); 790 } 791 792 798 public Interval toInterval(DateTimeZone zone) { 799 zone = DateTimeUtils.getZone(zone); 800 return toDateMidnight(zone).toInterval(); 801 } 802 803 809 public int getYear() { 810 return getValue(YEAR); 811 } 812 813 818 public int getMonthOfYear() { 819 return getValue(MONTH_OF_YEAR); 820 } 821 822 827 public int getDayOfMonth() { 828 return getValue(DAY_OF_MONTH); 829 } 830 831 844 public YearMonthDay withYear(int year) { 845 int[] newValues = getValues(); 846 newValues = getChronology().year().set(this, YEAR, newValues, year); 847 return new YearMonthDay(this, newValues); 848 } 849 850 862 public YearMonthDay withMonthOfYear(int monthOfYear) { 863 int[] newValues = getValues(); 864 newValues = getChronology().monthOfYear().set(this, MONTH_OF_YEAR, newValues, monthOfYear); 865 return new YearMonthDay(this, newValues); 866 } 867 868 880 public YearMonthDay withDayOfMonth(int dayOfMonth) { 881 int[] newValues = getValues(); 882 newValues = getChronology().dayOfMonth().set(this, DAY_OF_MONTH, newValues, dayOfMonth); 883 return new YearMonthDay(this, newValues); 884 } 885 886 892 public Property year() { 893 return new Property(this, YEAR); 894 } 895 896 901 public Property monthOfYear() { 902 return new Property(this, MONTH_OF_YEAR); 903 } 904 905 910 public Property dayOfMonth() { 911 return new Property(this, DAY_OF_MONTH); 912 } 913 914 920 public String toString() { 921 return ISODateTimeFormat.yearMonthDay().print(this); 922 } 923 924 933 public static class Property extends AbstractPartialFieldProperty implements Serializable { 934 935 936 private static final long serialVersionUID = 5727734012190224363L; 937 938 939 private final YearMonthDay iYearMonthDay; 940 941 private final int iFieldIndex; 942 943 949 Property(YearMonthDay partial, int fieldIndex) { 950 super(); 951 iYearMonthDay = partial; 952 iFieldIndex = fieldIndex; 953 } 954 955 960 public DateTimeField getField() { 961 return iYearMonthDay.getField(iFieldIndex); 962 } 963 964 969 protected ReadablePartial getReadablePartial() { 970 return iYearMonthDay; 971 } 972 973 978 public YearMonthDay getYearMonthDay() { 979 return iYearMonthDay; 980 } 981 982 987 public int get() { 988 return iYearMonthDay.getValue(iFieldIndex); 989 } 990 991 1009 public YearMonthDay addToCopy(int valueToAdd) { 1010 int[] newValues = iYearMonthDay.getValues(); 1011 newValues = getField().add(iYearMonthDay, iFieldIndex, newValues, valueToAdd); 1012 return new YearMonthDay(iYearMonthDay, newValues); 1013 } 1014 1015 1033 public YearMonthDay addWrapFieldToCopy(int valueToAdd) { 1034 int[] newValues = iYearMonthDay.getValues(); 1035 newValues = getField().addWrapField(iYearMonthDay, iFieldIndex, newValues, valueToAdd); 1036 return new YearMonthDay(iYearMonthDay, newValues); 1037 } 1038 1039 1050 public YearMonthDay setCopy(int value) { 1051 int[] newValues = iYearMonthDay.getValues(); 1052 newValues = getField().set(iYearMonthDay, iFieldIndex, newValues, value); 1053 return new YearMonthDay(iYearMonthDay, newValues); 1054 } 1055 1056 1067 public YearMonthDay setCopy(String text, Locale locale) { 1068 int[] newValues = iYearMonthDay.getValues(); 1069 newValues = getField().set(iYearMonthDay, iFieldIndex, newValues, text, locale); 1070 return new YearMonthDay(iYearMonthDay, newValues); 1071 } 1072 1073 1083 public YearMonthDay setCopy(String text) { 1084 return setCopy(text, null); 1085 } 1086 1087 1103 public YearMonthDay withMaximumValue() { 1104 return setCopy(getMaximumValue()); 1105 } 1106 1107 1116 public YearMonthDay withMinimumValue() { 1117 return setCopy(getMinimumValue()); 1118 } 1119 } 1120 1121} 1122 | Popular Tags |