1 16 package org.joda.time; 17 18 import java.io.IOException ; 19 import java.io.ObjectInputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.io.Serializable ; 22 import java.util.Locale ; 23 24 import org.joda.time.base.BaseDateTime; 25 import org.joda.time.field.AbstractReadableInstantFieldProperty; 26 import org.joda.time.format.ISODateTimeFormat; 27 28 69 public final class DateMidnight 70 extends BaseDateTime 71 implements ReadableDateTime, Serializable { 72 73 74 private static final long serialVersionUID = 156371964018738L; 75 76 83 public DateMidnight() { 84 super(); 85 } 86 87 96 public DateMidnight(DateTimeZone zone) { 97 super(zone); 98 } 99 100 110 public DateMidnight(Chronology chronology) { 111 super(chronology); 112 } 113 114 122 public DateMidnight(long instant) { 123 super(instant); 124 } 125 126 136 public DateMidnight(long instant, DateTimeZone zone) { 137 super(instant, zone); 138 } 139 140 151 public DateMidnight(long instant, Chronology chronology) { 152 super(instant, chronology); 153 } 154 155 173 public DateMidnight(Object instant) { 174 super(instant, (Chronology) null); 175 } 176 177 198 public DateMidnight(Object instant, DateTimeZone zone) { 199 super(instant, zone); 200 } 201 202 220 public DateMidnight(Object instant, Chronology chronology) { 221 super(instant, DateTimeUtils.getChronology(chronology)); 222 } 223 224 234 public DateMidnight(int year, int monthOfYear, int dayOfMonth) { 235 super(year, monthOfYear, dayOfMonth, 0, 0, 0, 0); 236 } 237 238 250 public DateMidnight(int year, int monthOfYear, int dayOfMonth, DateTimeZone zone) { 251 super(year, monthOfYear, dayOfMonth, 0, 0, 0, 0, zone); 252 } 253 254 267 public DateMidnight(int year, int monthOfYear, int dayOfMonth, Chronology chronology) { 268 super(year, monthOfYear, dayOfMonth, 0, 0, 0, 0, chronology); 269 } 270 271 278 protected long checkInstant(long instant, Chronology chronology) { 279 return chronology.dayOfMonth().roundFloor(instant); 280 } 281 282 293 public DateMidnight withMillis(long newMillis) { 294 Chronology chrono = getChronology(); 295 newMillis = checkInstant(newMillis, chrono); 296 return (newMillis == getMillis() ? this : new DateMidnight(newMillis, chrono)); 297 } 298 299 320 public DateMidnight withChronology(Chronology newChronology) { 321 return (newChronology == getChronology() ? this : new DateMidnight(getMillis(), newChronology)); 322 } 323 324 332 public DateMidnight withZoneRetainFields(DateTimeZone newZone) { 333 newZone = DateTimeUtils.getZone(newZone); 334 DateTimeZone originalZone = DateTimeUtils.getZone(getZone()); 335 if (newZone == originalZone) { 336 return this; 337 } 338 339 long millis = originalZone.getMillisKeepLocal(newZone, getMillis()); 340 return new DateMidnight(millis, getChronology().withZone(newZone)); 341 } 342 343 356 public DateMidnight withFields(ReadablePartial partial) { 357 if (partial == null) { 358 return this; 359 } 360 return withMillis(getChronology().set(partial, getMillis())); 361 } 362 363 382 public DateMidnight withField(DateTimeFieldType fieldType, int value) { 383 if (fieldType == null) { 384 throw new IllegalArgumentException ("Field must not be null"); 385 } 386 long instant = fieldType.getField(getChronology()).set(getMillis(), value); 387 return withMillis(instant); 388 } 389 390 408 public DateMidnight withFieldAdded(DurationFieldType fieldType, int amount) { 409 if (fieldType == null) { 410 throw new IllegalArgumentException ("Field must not be null"); 411 } 412 if (amount == 0) { 413 return this; 414 } 415 long instant = fieldType.getField(getChronology()).add(getMillis(), amount); 416 return withMillis(instant); 417 } 418 419 430 public DateMidnight withDurationAdded(long durationToAdd, int scalar) { 431 if (durationToAdd == 0 || scalar == 0) { 432 return this; 433 } 434 long instant = getChronology().add(getMillis(), durationToAdd, scalar); 435 return withMillis(instant); 436 } 437 438 448 public DateMidnight withDurationAdded(ReadableDuration durationToAdd, int scalar) { 449 if (durationToAdd == null || scalar == 0) { 450 return this; 451 } 452 return withDurationAdded(durationToAdd.getMillis(), scalar); 453 } 454 455 470 public DateMidnight withPeriodAdded(ReadablePeriod period, int scalar) { 471 if (period == null || scalar == 0) { 472 return this; 473 } 474 long instant = getChronology().add(period, getMillis(), scalar); 475 return withMillis(instant); 476 } 477 478 488 public DateMidnight plus(long duration) { 489 return withDurationAdded(duration, 1); 490 } 491 492 501 public DateMidnight plus(ReadableDuration duration) { 502 return withDurationAdded(duration, 1); 503 } 504 505 518 public DateMidnight plus(ReadablePeriod period) { 519 return withPeriodAdded(period, 1); 520 } 521 522 539 public DateMidnight plusYears(int years) { 540 if (years == 0) { 541 return this; 542 } 543 long instant = getChronology().years().add(getMillis(), years); 544 return withMillis(instant); 545 } 546 547 563 public DateMidnight plusMonths(int months) { 564 if (months == 0) { 565 return this; 566 } 567 long instant = getChronology().months().add(getMillis(), months); 568 return withMillis(instant); 569 } 570 571 587 public DateMidnight plusWeeks(int weeks) { 588 if (weeks == 0) { 589 return this; 590 } 591 long instant = getChronology().weeks().add(getMillis(), weeks); 592 return withMillis(instant); 593 } 594 595 611 public DateMidnight plusDays(int days) { 612 if (days == 0) { 613 return this; 614 } 615 long instant = getChronology().days().add(getMillis(), days); 616 return withMillis(instant); 617 } 618 619 629 public DateMidnight minus(long duration) { 630 return withDurationAdded(duration, -1); 631 } 632 633 642 public DateMidnight minus(ReadableDuration duration) { 643 return withDurationAdded(duration, -1); 644 } 645 646 659 public DateMidnight minus(ReadablePeriod period) { 660 return withPeriodAdded(period, -1); 661 } 662 663 680 public DateMidnight minusYears(int years) { 681 if (years == 0) { 682 return this; 683 } 684 long instant = getChronology().years().subtract(getMillis(), years); 685 return withMillis(instant); 686 } 687 688 704 public DateMidnight minusMonths(int months) { 705 if (months == 0) { 706 return this; 707 } 708 long instant = getChronology().months().subtract(getMillis(), months); 709 return withMillis(instant); 710 } 711 712 728 public DateMidnight minusWeeks(int weeks) { 729 if (weeks == 0) { 730 return this; 731 } 732 long instant = getChronology().weeks().subtract(getMillis(), weeks); 733 return withMillis(instant); 734 } 735 736 752 public DateMidnight minusDays(int days) { 753 if (days == 0) { 754 return this; 755 } 756 long instant = getChronology().days().subtract(getMillis(), days); 757 return withMillis(instant); 758 } 759 760 768 public Property property(DateTimeFieldType type) { 769 if (type == null) { 770 throw new IllegalArgumentException ("The DateTimeFieldType must not be null"); 771 } 772 DateTimeField field = type.getField(getChronology()); 773 if (field.isSupported() == false) { 774 throw new IllegalArgumentException ("Field '" + type + "' is not supported"); 775 } 776 return new Property(this, field); 777 } 778 779 786 public YearMonthDay toYearMonthDay() { 787 return new YearMonthDay(getMillis(), getChronology()); 788 } 789 790 797 public LocalDate toLocalDate() { 798 return new LocalDate(getMillis(), getChronology()); 799 } 800 801 810 public Interval toInterval() { 811 Chronology chrono = getChronology(); 812 long start = getMillis(); 813 long end = DurationFieldType.days().getField(chrono).add(start, 1); 814 return new Interval(start, end, chrono); 815 } 816 817 830 public DateMidnight withEra(int era) { 831 return withMillis(getChronology().era().set(getMillis(), era)); 832 } 833 834 846 public DateMidnight withCenturyOfEra(int centuryOfEra) { 847 return withMillis(getChronology().centuryOfEra().set(getMillis(), centuryOfEra)); 848 } 849 850 862 public DateMidnight withYearOfEra(int yearOfEra) { 863 return withMillis(getChronology().yearOfEra().set(getMillis(), yearOfEra)); 864 } 865 866 878 public DateMidnight withYearOfCentury(int yearOfCentury) { 879 return withMillis(getChronology().yearOfCentury().set(getMillis(), yearOfCentury)); 880 } 881 882 894 public DateMidnight withYear(int year) { 895 return withMillis(getChronology().year().set(getMillis(), year)); 896 } 897 898 910 public DateMidnight withWeekyear(int weekyear) { 911 return withMillis(getChronology().weekyear().set(getMillis(), weekyear)); 912 } 913 914 926 public DateMidnight withMonthOfYear(int monthOfYear) { 927 return withMillis(getChronology().monthOfYear().set(getMillis(), monthOfYear)); 928 } 929 930 942 public DateMidnight withWeekOfWeekyear(int weekOfWeekyear) { 943 return withMillis(getChronology().weekOfWeekyear().set(getMillis(), weekOfWeekyear)); 944 } 945 946 958 public DateMidnight withDayOfYear(int dayOfYear) { 959 return withMillis(getChronology().dayOfYear().set(getMillis(), dayOfYear)); 960 } 961 962 974 public DateMidnight withDayOfMonth(int dayOfMonth) { 975 return withMillis(getChronology().dayOfMonth().set(getMillis(), dayOfMonth)); 976 } 977 978 990 public DateMidnight withDayOfWeek(int dayOfWeek) { 991 return withMillis(getChronology().dayOfWeek().set(getMillis(), dayOfWeek)); 992 } 993 994 1001 public Property era() { 1002 return new Property(this, getChronology().era()); 1003 } 1004 1005 1010 public Property centuryOfEra() { 1011 return new Property(this, getChronology().centuryOfEra()); 1012 } 1013 1014 1019 public Property yearOfCentury() { 1020 return new Property(this, getChronology().yearOfCentury()); 1021 } 1022 1023 1028 public Property yearOfEra() { 1029 return new Property(this, getChronology().yearOfEra()); 1030 } 1031 1032 1037 public Property year() { 1038 return new Property(this, getChronology().year()); 1039 } 1040 1041 1046 public Property weekyear() { 1047 return new Property(this, getChronology().weekyear()); 1048 } 1049 1050 1055 public Property monthOfYear() { 1056 return new Property(this, getChronology().monthOfYear()); 1057 } 1058 1059 1064 public Property weekOfWeekyear() { 1065 return new Property(this, getChronology().weekOfWeekyear()); 1066 } 1067 1068 1073 public Property dayOfYear() { 1074 return new Property(this, getChronology().dayOfYear()); 1075 } 1076 1077 1082 public Property dayOfMonth() { 1083 return new Property(this, getChronology().dayOfMonth()); 1084 } 1085 1086 1091 public Property dayOfWeek() { 1092 return new Property(this, getChronology().dayOfWeek()); 1093 } 1094 1095 1124 public static final class Property extends AbstractReadableInstantFieldProperty { 1125 1126 1127 private static final long serialVersionUID = 257629620L; 1128 1129 1130 private DateMidnight iInstant; 1131 1132 private DateTimeField iField; 1133 1134 1140 Property(DateMidnight instant, DateTimeField field) { 1141 super(); 1142 iInstant = instant; 1143 iField = field; 1144 } 1145 1146 1149 private void writeObject(ObjectOutputStream oos) throws IOException { 1150 oos.writeObject(iInstant); 1151 oos.writeObject(iField.getType()); 1152 } 1153 1154 1157 private void readObject(ObjectInputStream oos) throws IOException , ClassNotFoundException { 1158 iInstant = (DateMidnight) oos.readObject(); 1159 DateTimeFieldType type = (DateTimeFieldType) oos.readObject(); 1160 iField = type.getField(iInstant.getChronology()); 1161 } 1162 1163 1169 public DateTimeField getField() { 1170 return iField; 1171 } 1172 1173 1178 protected long getMillis() { 1179 return iInstant.getMillis(); 1180 } 1181 1182 1188 protected Chronology getChronology() { 1189 return iInstant.getChronology(); 1190 } 1191 1192 1197 public DateMidnight getDateMidnight() { 1198 return iInstant; 1199 } 1200 1201 1214 public DateMidnight addToCopy(int value) { 1215 return iInstant.withMillis(iField.add(iInstant.getMillis(), value)); 1216 } 1217 1218 1230 public DateMidnight addToCopy(long value) { 1231 return iInstant.withMillis(iField.add(iInstant.getMillis(), value)); 1232 } 1233 1234 1248 public DateMidnight addWrapFieldToCopy(int value) { 1249 return iInstant.withMillis(iField.addWrapField(iInstant.getMillis(), value)); 1250 } 1251 1252 1265 public DateMidnight setCopy(int value) { 1266 return iInstant.withMillis(iField.set(iInstant.getMillis(), value)); 1267 } 1268 1269 1282 public DateMidnight setCopy(String text, Locale locale) { 1283 return iInstant.withMillis(iField.set(iInstant.getMillis(), text, locale)); 1284 } 1285 1286 1298 public DateMidnight setCopy(String text) { 1299 return setCopy(text, null); 1300 } 1301 1302 1318 public DateMidnight withMaximumValue() { 1319 return setCopy(getMaximumValue()); 1320 } 1321 1322 1331 public DateMidnight withMinimumValue() { 1332 return setCopy(getMinimumValue()); 1333 } 1334 1335 1341 public DateMidnight roundFloorCopy() { 1342 return iInstant.withMillis(iField.roundFloor(iInstant.getMillis())); 1343 } 1344 1345 1350 public DateMidnight roundCeilingCopy() { 1351 return iInstant.withMillis(iField.roundCeiling(iInstant.getMillis())); 1352 } 1353 1354 1360 public DateMidnight roundHalfFloorCopy() { 1361 return iInstant.withMillis(iField.roundHalfFloor(iInstant.getMillis())); 1362 } 1363 1364 1370 public DateMidnight roundHalfCeilingCopy() { 1371 return iInstant.withMillis(iField.roundHalfCeiling(iInstant.getMillis())); 1372 } 1373 1374 1380 public DateMidnight roundHalfEvenCopy() { 1381 return iInstant.withMillis(iField.roundHalfEven(iInstant.getMillis())); 1382 } 1383 1384 } 1385} 1386 | Popular Tags |