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.chrono.ISOChronology; 26 import org.joda.time.field.AbstractReadableInstantFieldProperty; 27 import org.joda.time.format.ISODateTimeFormat; 28 29 65 public final class DateTime 66 extends BaseDateTime 67 implements ReadableDateTime, Serializable { 68 69 70 private static final long serialVersionUID = -5171125899451703815L; 71 72 77 public DateTime() { 78 super(); 79 } 80 81 89 public DateTime(DateTimeZone zone) { 90 super(zone); 91 } 92 93 102 public DateTime(Chronology chronology) { 103 super(chronology); 104 } 105 106 113 public DateTime(long instant) { 114 super(instant); 115 } 116 117 126 public DateTime(long instant, DateTimeZone zone) { 127 super(instant, zone); 128 } 129 130 140 public DateTime(long instant, Chronology chronology) { 141 super(instant, chronology); 142 } 143 144 161 public DateTime(Object instant) { 162 super(instant, (Chronology) null); 163 } 164 165 185 public DateTime(Object instant, DateTimeZone zone) { 186 super(instant, zone); 187 } 188 189 206 public DateTime(Object instant, Chronology chronology) { 207 super(instant, DateTimeUtils.getChronology(chronology)); 208 } 209 210 223 public DateTime( 224 int year, 225 int monthOfYear, 226 int dayOfMonth, 227 int hourOfDay, 228 int minuteOfHour, 229 int secondOfMinute, 230 int millisOfSecond) { 231 super(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond); 232 } 233 234 249 public DateTime( 250 int year, 251 int monthOfYear, 252 int dayOfMonth, 253 int hourOfDay, 254 int minuteOfHour, 255 int secondOfMinute, 256 int millisOfSecond, 257 DateTimeZone zone) { 258 super(year, monthOfYear, dayOfMonth, 259 hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond, zone); 260 } 261 262 278 public DateTime( 279 int year, 280 int monthOfYear, 281 int dayOfMonth, 282 int hourOfDay, 283 int minuteOfHour, 284 int secondOfMinute, 285 int millisOfSecond, 286 Chronology chronology) { 287 super(year, monthOfYear, dayOfMonth, 288 hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond, chronology); 289 } 290 291 297 public DateTime toDateTime() { 298 return this; 299 } 300 301 307 public DateTime toDateTimeISO() { 308 if (getChronology() == ISOChronology.getInstance()) { 309 return this; 310 } 311 return super.toDateTimeISO(); 312 } 313 314 320 public DateTime toDateTime(DateTimeZone zone) { 321 zone = DateTimeUtils.getZone(zone); 322 if (getZone() == zone) { 323 return this; 324 } 325 return super.toDateTime(zone); 326 } 327 328 334 public DateTime toDateTime(Chronology chronology) { 335 chronology = DateTimeUtils.getChronology(chronology); 336 if (getChronology() == chronology) { 337 return this; 338 } 339 return super.toDateTime(chronology); 340 } 341 342 352 public DateTime withMillis(long newMillis) { 353 return (newMillis == getMillis() ? this : new DateTime(newMillis, getChronology())); 354 } 355 356 365 public DateTime withChronology(Chronology newChronology) { 366 newChronology = DateTimeUtils.getChronology(newChronology); 367 return (newChronology == getChronology() ? this : new DateTime(getMillis(), newChronology)); 368 } 369 370 388 public DateTime withZone(DateTimeZone newZone) { 389 return withChronology(getChronology().withZone(newZone)); 390 } 391 392 409 public DateTime withZoneRetainFields(DateTimeZone newZone) { 410 newZone = DateTimeUtils.getZone(newZone); 411 DateTimeZone originalZone = DateTimeUtils.getZone(getZone()); 412 if (newZone == originalZone) { 413 return this; 414 } 415 416 long millis = originalZone.getMillisKeepLocal(newZone, getMillis()); 417 return new DateTime(millis, getChronology().withZone(newZone)); 418 } 419 420 437 public DateTime withDate(int year, int monthOfYear, int dayOfMonth) { 438 Chronology chrono = getChronology(); 439 long instant = getMillis(); 440 instant = chrono.year().set(instant, year); 441 instant = chrono.monthOfYear().set(instant, monthOfYear); 442 instant = chrono.dayOfMonth().set(instant, dayOfMonth); 443 return withMillis(instant); 444 } 445 446 463 public DateTime withTime(int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond) { 464 Chronology chrono = getChronology(); 465 long instant = getMillis(); 466 instant = chrono.hourOfDay().set(instant, hourOfDay); 467 instant = chrono.minuteOfHour().set(instant, minuteOfHour); 468 instant = chrono.secondOfMinute().set(instant, secondOfMinute); 469 instant = chrono.millisOfSecond().set(instant, millisOfSecond); 470 return withMillis(instant); 471 } 472 473 486 public DateTime withFields(ReadablePartial partial) { 487 if (partial == null) { 488 return this; 489 } 490 return withMillis(getChronology().set(partial, getMillis())); 491 } 492 493 512 public DateTime withField(DateTimeFieldType fieldType, int value) { 513 if (fieldType == null) { 514 throw new IllegalArgumentException ("Field must not be null"); 515 } 516 long instant = fieldType.getField(getChronology()).set(getMillis(), value); 517 return withMillis(instant); 518 } 519 520 538 public DateTime withFieldAdded(DurationFieldType fieldType, int amount) { 539 if (fieldType == null) { 540 throw new IllegalArgumentException ("Field must not be null"); 541 } 542 if (amount == 0) { 543 return this; 544 } 545 long instant = fieldType.getField(getChronology()).add(getMillis(), amount); 546 return withMillis(instant); 547 } 548 549 560 public DateTime withDurationAdded(long durationToAdd, int scalar) { 561 if (durationToAdd == 0 || scalar == 0) { 562 return this; 563 } 564 long instant = getChronology().add(getMillis(), durationToAdd, scalar); 565 return withMillis(instant); 566 } 567 568 578 public DateTime withDurationAdded(ReadableDuration durationToAdd, int scalar) { 579 if (durationToAdd == null || scalar == 0) { 580 return this; 581 } 582 return withDurationAdded(durationToAdd.getMillis(), scalar); 583 } 584 585 600 public DateTime withPeriodAdded(ReadablePeriod period, int scalar) { 601 if (period == null || scalar == 0) { 602 return this; 603 } 604 long instant = getChronology().add(period, getMillis(), scalar); 605 return withMillis(instant); 606 } 607 608 618 public DateTime plus(long duration) { 619 return withDurationAdded(duration, 1); 620 } 621 622 631 public DateTime plus(ReadableDuration duration) { 632 return withDurationAdded(duration, 1); 633 } 634 635 648 public DateTime plus(ReadablePeriod period) { 649 return withPeriodAdded(period, 1); 650 } 651 652 669 public DateTime plusYears(int years) { 670 if (years == 0) { 671 return this; 672 } 673 long instant = getChronology().years().add(getMillis(), years); 674 return withMillis(instant); 675 } 676 677 693 public DateTime plusMonths(int months) { 694 if (months == 0) { 695 return this; 696 } 697 long instant = getChronology().months().add(getMillis(), months); 698 return withMillis(instant); 699 } 700 701 717 public DateTime plusWeeks(int weeks) { 718 if (weeks == 0) { 719 return this; 720 } 721 long instant = getChronology().weeks().add(getMillis(), weeks); 722 return withMillis(instant); 723 } 724 725 741 public DateTime plusDays(int days) { 742 if (days == 0) { 743 return this; 744 } 745 long instant = getChronology().days().add(getMillis(), days); 746 return withMillis(instant); 747 } 748 749 765 public DateTime plusHours(int hours) { 766 if (hours == 0) { 767 return this; 768 } 769 long instant = getChronology().hours().add(getMillis(), hours); 770 return withMillis(instant); 771 } 772 773 789 public DateTime plusMinutes(int minutes) { 790 if (minutes == 0) { 791 return this; 792 } 793 long instant = getChronology().minutes().add(getMillis(), minutes); 794 return withMillis(instant); 795 } 796 797 813 public DateTime plusSeconds(int seconds) { 814 if (seconds == 0) { 815 return this; 816 } 817 long instant = getChronology().seconds().add(getMillis(), seconds); 818 return withMillis(instant); 819 } 820 821 837 public DateTime plusMillis(int millis) { 838 if (millis == 0) { 839 return this; 840 } 841 long instant = getChronology().millis().add(getMillis(), millis); 842 return withMillis(instant); 843 } 844 845 855 public DateTime minus(long duration) { 856 return withDurationAdded(duration, -1); 857 } 858 859 868 public DateTime minus(ReadableDuration duration) { 869 return withDurationAdded(duration, -1); 870 } 871 872 885 public DateTime minus(ReadablePeriod period) { 886 return withPeriodAdded(period, -1); 887 } 888 889 906 public DateTime minusYears(int years) { 907 if (years == 0) { 908 return this; 909 } 910 long instant = getChronology().years().subtract(getMillis(), years); 911 return withMillis(instant); 912 } 913 914 930 public DateTime minusMonths(int months) { 931 if (months == 0) { 932 return this; 933 } 934 long instant = getChronology().months().subtract(getMillis(), months); 935 return withMillis(instant); 936 } 937 938 954 public DateTime minusWeeks(int weeks) { 955 if (weeks == 0) { 956 return this; 957 } 958 long instant = getChronology().weeks().subtract(getMillis(), weeks); 959 return withMillis(instant); 960 } 961 962 978 public DateTime minusDays(int days) { 979 if (days == 0) { 980 return this; 981 } 982 long instant = getChronology().days().subtract(getMillis(), days); 983 return withMillis(instant); 984 } 985 986 1002 public DateTime minusHours(int hours) { 1003 if (hours == 0) { 1004 return this; 1005 } 1006 long instant = getChronology().hours().subtract(getMillis(), hours); 1007 return withMillis(instant); 1008 } 1009 1010 1026 public DateTime minusMinutes(int minutes) { 1027 if (minutes == 0) { 1028 return this; 1029 } 1030 long instant = getChronology().minutes().subtract(getMillis(), minutes); 1031 return withMillis(instant); 1032 } 1033 1034 1050 public DateTime minusSeconds(int seconds) { 1051 if (seconds == 0) { 1052 return this; 1053 } 1054 long instant = getChronology().seconds().subtract(getMillis(), seconds); 1055 return withMillis(instant); 1056 } 1057 1058 1074 public DateTime minusMillis(int millis) { 1075 if (millis == 0) { 1076 return this; 1077 } 1078 long instant = getChronology().millis().subtract(getMillis(), millis); 1079 return withMillis(instant); 1080 } 1081 1082 1090 public Property property(DateTimeFieldType type) { 1091 if (type == null) { 1092 throw new IllegalArgumentException ("The DateTimeFieldType must not be null"); 1093 } 1094 DateTimeField field = type.getField(getChronology()); 1095 if (field.isSupported() == false) { 1096 throw new IllegalArgumentException ("Field '" + type + "' is not supported"); 1097 } 1098 return new Property(this, field); 1099 } 1100 1101 1108 public DateMidnight toDateMidnight() { 1109 return new DateMidnight(getMillis(), getChronology()); 1110 } 1111 1112 1118 public YearMonthDay toYearMonthDay() { 1119 return new YearMonthDay(getMillis(), getChronology()); 1120 } 1121 1122 1128 public TimeOfDay toTimeOfDay() { 1129 return new TimeOfDay(getMillis(), getChronology()); 1130 } 1131 1132 1139 public LocalDateTime toLocalDateTime() { 1140 return new LocalDateTime(getMillis(), getChronology()); 1141 } 1142 1143 1150 public LocalDate toLocalDate() { 1151 return new LocalDate(getMillis(), getChronology()); 1152 } 1153 1154 1161 public LocalTime toLocalTime() { 1162 return new LocalTime(getMillis(), getChronology()); 1163 } 1164 1165 1178 public DateTime withEra(int era) { 1179 return withMillis(getChronology().era().set(getMillis(), era)); 1180 } 1181 1182 1194 public DateTime withCenturyOfEra(int centuryOfEra) { 1195 return withMillis(getChronology().centuryOfEra().set(getMillis(), centuryOfEra)); 1196 } 1197 1198 1210 public DateTime withYearOfEra(int yearOfEra) { 1211 return withMillis(getChronology().yearOfEra().set(getMillis(), yearOfEra)); 1212 } 1213 1214 1226 public DateTime withYearOfCentury(int yearOfCentury) { 1227 return withMillis(getChronology().yearOfCentury().set(getMillis(), yearOfCentury)); 1228 } 1229 1230 1242 public DateTime withYear(int year) { 1243 return withMillis(getChronology().year().set(getMillis(), year)); 1244 } 1245 1246 1258 public DateTime withWeekyear(int weekyear) { 1259 return withMillis(getChronology().weekyear().set(getMillis(), weekyear)); 1260 } 1261 1262 1274 public DateTime withMonthOfYear(int monthOfYear) { 1275 return withMillis(getChronology().monthOfYear().set(getMillis(), monthOfYear)); 1276 } 1277 1278 1290 public DateTime withWeekOfWeekyear(int weekOfWeekyear) { 1291 return withMillis(getChronology().weekOfWeekyear().set(getMillis(), weekOfWeekyear)); 1292 } 1293 1294 1306 public DateTime withDayOfYear(int dayOfYear) { 1307 return withMillis(getChronology().dayOfYear().set(getMillis(), dayOfYear)); 1308 } 1309 1310 1322 public DateTime withDayOfMonth(int dayOfMonth) { 1323 return withMillis(getChronology().dayOfMonth().set(getMillis(), dayOfMonth)); 1324 } 1325 1326 1338 public DateTime withDayOfWeek(int dayOfWeek) { 1339 return withMillis(getChronology().dayOfWeek().set(getMillis(), dayOfWeek)); 1340 } 1341 1342 1355 public DateTime withHourOfDay(int hour) { 1356 return withMillis(getChronology().hourOfDay().set(getMillis(), hour)); 1357 } 1358 1359 1371 public DateTime withMinuteOfHour(int minute) { 1372 return withMillis(getChronology().minuteOfHour().set(getMillis(), minute)); 1373 } 1374 1375 1387 public DateTime withSecondOfMinute(int second) { 1388 return withMillis(getChronology().secondOfMinute().set(getMillis(), second)); 1389 } 1390 1391 1403 public DateTime withMillisOfSecond(int millis) { 1404 return withMillis(getChronology().millisOfSecond().set(getMillis(), millis)); 1405 } 1406 1407 1419 public DateTime withMillisOfDay(int millis) { 1420 return withMillis(getChronology().millisOfDay().set(getMillis(), millis)); 1421 } 1422 1423 1430 public Property era() { 1431 return new Property(this, getChronology().era()); 1432 } 1433 1434 1439 public Property centuryOfEra() { 1440 return new Property(this, getChronology().centuryOfEra()); 1441 } 1442 1443 1448 public Property yearOfCentury() { 1449 return new Property(this, getChronology().yearOfCentury()); 1450 } 1451 1452 1457 public Property yearOfEra() { 1458 return new Property(this, getChronology().yearOfEra()); 1459 } 1460 1461 1466 public Property year() { 1467 return new Property(this, getChronology().year()); 1468 } 1469 1470 1475 public Property weekyear() { 1476 return new Property(this, getChronology().weekyear()); 1477 } 1478 1479 1484 public Property monthOfYear() { 1485 return new Property(this, getChronology().monthOfYear()); 1486 } 1487 1488 1493 public Property weekOfWeekyear() { 1494 return new Property(this, getChronology().weekOfWeekyear()); 1495 } 1496 1497 1502 public Property dayOfYear() { 1503 return new Property(this, getChronology().dayOfYear()); 1504 } 1505 1506 1511 public Property dayOfMonth() { 1512 return new Property(this, getChronology().dayOfMonth()); 1513 } 1514 1515 1520 public Property dayOfWeek() { 1521 return new Property(this, getChronology().dayOfWeek()); 1522 } 1523 1524 1531 public Property hourOfDay() { 1532 return new Property(this, getChronology().hourOfDay()); 1533 } 1534 1535 1540 public Property minuteOfDay() { 1541 return new Property(this, getChronology().minuteOfDay()); 1542 } 1543 1544 1549 public Property minuteOfHour() { 1550 return new Property(this, getChronology().minuteOfHour()); 1551 } 1552 1553 1558 public Property secondOfDay() { 1559 return new Property(this, getChronology().secondOfDay()); 1560 } 1561 1562 1567 public Property secondOfMinute() { 1568 return new Property(this, getChronology().secondOfMinute()); 1569 } 1570 1571 1576 public Property millisOfDay() { 1577 return new Property(this, getChronology().millisOfDay()); 1578 } 1579 1580 1585 public Property millisOfSecond() { 1586 return new Property(this, getChronology().millisOfSecond()); 1587 } 1588 1589 1619 public static final class Property extends AbstractReadableInstantFieldProperty { 1620 1621 1622 private static final long serialVersionUID = -6983323811635733510L; 1623 1624 1625 private DateTime iInstant; 1626 1627 private DateTimeField iField; 1628 1629 1635 Property(DateTime instant, DateTimeField field) { 1636 super(); 1637 iInstant = instant; 1638 iField = field; 1639 } 1640 1641 1644 private void writeObject(ObjectOutputStream oos) throws IOException { 1645 oos.writeObject(iInstant); 1646 oos.writeObject(iField.getType()); 1647 } 1648 1649 1652 private void readObject(ObjectInputStream oos) throws IOException , ClassNotFoundException { 1653 iInstant = (DateTime) oos.readObject(); 1654 DateTimeFieldType type = (DateTimeFieldType) oos.readObject(); 1655 iField = type.getField(iInstant.getChronology()); 1656 } 1657 1658 1664 public DateTimeField getField() { 1665 return iField; 1666 } 1667 1668 1673 protected long getMillis() { 1674 return iInstant.getMillis(); 1675 } 1676 1677 1683 protected Chronology getChronology() { 1684 return iInstant.getChronology(); 1685 } 1686 1687 1692 public DateTime getDateTime() { 1693 return iInstant; 1694 } 1695 1696 1709 public DateTime addToCopy(int value) { 1710 return iInstant.withMillis(iField.add(iInstant.getMillis(), value)); 1711 } 1712 1713 1725 public DateTime addToCopy(long value) { 1726 return iInstant.withMillis(iField.add(iInstant.getMillis(), value)); 1727 } 1728 1729 1743 public DateTime addWrapFieldToCopy(int value) { 1744 return iInstant.withMillis(iField.addWrapField(iInstant.getMillis(), value)); 1745 } 1746 1747 1760 public DateTime setCopy(int value) { 1761 return iInstant.withMillis(iField.set(iInstant.getMillis(), value)); 1762 } 1763 1764 1777 public DateTime setCopy(String text, Locale locale) { 1778 return iInstant.withMillis(iField.set(iInstant.getMillis(), text, locale)); 1779 } 1780 1781 1793 public DateTime setCopy(String text) { 1794 return setCopy(text, null); 1795 } 1796 1797 1813 public DateTime withMaximumValue() { 1814 return setCopy(getMaximumValue()); 1815 } 1816 1817 1826 public DateTime withMinimumValue() { 1827 return setCopy(getMinimumValue()); 1828 } 1829 1830 1836 public DateTime roundFloorCopy() { 1837 return iInstant.withMillis(iField.roundFloor(iInstant.getMillis())); 1838 } 1839 1840 1845 public DateTime roundCeilingCopy() { 1846 return iInstant.withMillis(iField.roundCeiling(iInstant.getMillis())); 1847 } 1848 1849 1855 public DateTime roundHalfFloorCopy() { 1856 return iInstant.withMillis(iField.roundHalfFloor(iInstant.getMillis())); 1857 } 1858 1859 1865 public DateTime roundHalfCeilingCopy() { 1866 return iInstant.withMillis(iField.roundHalfCeiling(iInstant.getMillis())); 1867 } 1868 1869 1876 public DateTime roundHalfEvenCopy() { 1877 return iInstant.withMillis(iField.roundHalfEven(iInstant.getMillis())); 1878 } 1879 } 1880 1881} 1882 | Popular Tags |