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.field.FieldUtils; 28 import org.joda.time.format.ISODateTimeFormat; 29 30 69 public class MutableDateTime 70 extends BaseDateTime 71 implements ReadWritableDateTime, Cloneable , Serializable { 72 73 74 private static final long serialVersionUID = 2852608688135209575L; 75 76 77 public static final int ROUND_NONE = 0; 78 79 public static final int ROUND_FLOOR = 1; 80 81 public static final int ROUND_CEILING = 2; 82 83 public static final int ROUND_HALF_FLOOR = 3; 84 85 public static final int ROUND_HALF_CEILING = 4; 86 87 public static final int ROUND_HALF_EVEN = 5; 88 89 90 private DateTimeField iRoundingField; 91 92 private int iRoundingMode; 93 94 99 public MutableDateTime() { 100 super(); 101 } 102 103 111 public MutableDateTime(DateTimeZone zone) { 112 super(zone); 113 } 114 115 124 public MutableDateTime(Chronology chronology) { 125 super(chronology); 126 } 127 128 135 public MutableDateTime(long instant) { 136 super(instant); 137 } 138 139 148 public MutableDateTime(long instant, DateTimeZone zone) { 149 super(instant, zone); 150 } 151 152 162 public MutableDateTime(long instant, Chronology chronology) { 163 super(instant, chronology); 164 } 165 166 182 public MutableDateTime(Object instant) { 183 super(instant, (Chronology) null); 184 } 185 186 205 public MutableDateTime(Object instant, DateTimeZone zone) { 206 super(instant, zone); 207 } 208 209 225 public MutableDateTime(Object instant, Chronology chronology) { 226 super(instant, DateTimeUtils.getChronology(chronology)); 227 } 228 229 242 public MutableDateTime( 243 int year, 244 int monthOfYear, 245 int dayOfMonth, 246 int hourOfDay, 247 int minuteOfHour, 248 int secondOfMinute, 249 int millisOfSecond) { 250 super(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond); 251 } 252 253 268 public MutableDateTime( 269 int year, 270 int monthOfYear, 271 int dayOfMonth, 272 int hourOfDay, 273 int minuteOfHour, 274 int secondOfMinute, 275 int millisOfSecond, 276 DateTimeZone zone) { 277 super(year, monthOfYear, dayOfMonth, 278 hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond, zone); 279 } 280 281 297 public MutableDateTime( 298 int year, 299 int monthOfYear, 300 int dayOfMonth, 301 int hourOfDay, 302 int minuteOfHour, 303 int secondOfMinute, 304 int millisOfSecond, 305 Chronology chronology) { 306 super(year, monthOfYear, dayOfMonth, 307 hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond, chronology); 308 } 309 310 317 public DateTimeField getRoundingField() { 318 return iRoundingField; 319 } 320 321 327 public int getRoundingMode() { 328 return iRoundingMode; 329 } 330 331 343 public void setRounding(DateTimeField field) { 344 setRounding(field, MutableDateTime.ROUND_FLOOR); 345 } 346 347 361 public void setRounding(DateTimeField field, int mode) { 362 if (field != null && (mode < ROUND_NONE || mode > ROUND_HALF_EVEN)) { 363 throw new IllegalArgumentException ("Illegal rounding mode: " + mode); 364 } 365 iRoundingField = (mode == ROUND_NONE ? null : field); 366 iRoundingMode = (field == null ? ROUND_NONE : mode); 367 setMillis(getMillis()); 368 } 369 370 379 public void setMillis(long instant) { 380 switch (iRoundingMode) { 381 case ROUND_NONE: 382 break; 383 case ROUND_FLOOR: 384 instant = iRoundingField.roundFloor(instant); 385 break; 386 case ROUND_CEILING: 387 instant = iRoundingField.roundCeiling(instant); 388 break; 389 case ROUND_HALF_FLOOR: 390 instant = iRoundingField.roundHalfFloor(instant); 391 break; 392 case ROUND_HALF_CEILING: 393 instant = iRoundingField.roundHalfCeiling(instant); 394 break; 395 case ROUND_HALF_EVEN: 396 instant = iRoundingField.roundHalfEven(instant); 397 break; 398 } 399 400 super.setMillis(instant); 401 } 402 403 411 public void setMillis(ReadableInstant instant) { 412 long instantMillis = DateTimeUtils.getInstantMillis(instant); 413 setMillis(instantMillis); } 415 416 423 public void add(long duration) { 424 setMillis(FieldUtils.safeAdd(getMillis(), duration)); } 426 427 435 public void add(ReadableDuration duration) { 436 add(duration, 1); 437 } 438 439 448 public void add(ReadableDuration duration, int scalar) { 449 if (duration != null) { 450 add(FieldUtils.safeMultiply(duration.getMillis(), scalar)); 451 } 452 } 453 454 462 public void add(ReadablePeriod period) { 463 add(period, 1); 464 } 465 466 475 public void add(ReadablePeriod period, int scalar) { 476 if (period != null) { 477 setMillis(getChronology().add(period, getMillis(), scalar)); } 479 } 480 481 489 public void setChronology(Chronology chronology) { 490 super.setChronology(chronology); 491 } 492 493 508 public void setZone(DateTimeZone newZone) { 509 newZone = DateTimeUtils.getZone(newZone); 510 Chronology chrono = getChronology(); 511 if (chrono.getZone() != newZone) { 512 setChronology(chrono.withZone(newZone)); } 514 } 515 516 527 public void setZoneRetainFields(DateTimeZone newZone) { 528 newZone = DateTimeUtils.getZone(newZone); 529 DateTimeZone originalZone = DateTimeUtils.getZone(getZone()); 530 if (newZone == originalZone) { 531 return; 532 } 533 534 long millis = originalZone.getMillisKeepLocal(newZone, getMillis()); 535 setChronology(getChronology().withZone(newZone)); setMillis(millis); 537 } 538 539 547 public void set(DateTimeFieldType type, int value) { 548 if (type == null) { 549 throw new IllegalArgumentException ("Field must not be null"); 550 } 551 setMillis(type.getField(getChronology()).set(getMillis(), value)); 552 } 553 554 562 public void add(DurationFieldType type, int amount) { 563 if (type == null) { 564 throw new IllegalArgumentException ("Field must not be null"); 565 } 566 setMillis(type.getField(getChronology()).add(getMillis(), amount)); 567 } 568 569 576 public void setYear(final int year) { 577 setMillis(getChronology().year().set(getMillis(), year)); 578 } 579 580 586 public void addYears(final int years) { 587 setMillis(getChronology().years().add(getMillis(), years)); 588 } 589 590 597 public void setWeekyear(final int weekyear) { 598 setMillis(getChronology().weekyear().set(getMillis(), weekyear)); 599 } 600 601 607 public void addWeekyears(final int weekyears) { 608 setMillis(getChronology().weekyears().add(getMillis(), weekyears)); 609 } 610 611 618 public void setMonthOfYear(final int monthOfYear) { 619 setMillis(getChronology().monthOfYear().set(getMillis(), monthOfYear)); 620 } 621 622 628 public void addMonths(final int months) { 629 setMillis(getChronology().months().add(getMillis(), months)); 630 } 631 632 639 public void setWeekOfWeekyear(final int weekOfWeekyear) { 640 setMillis(getChronology().weekOfWeekyear().set(getMillis(), weekOfWeekyear)); 641 } 642 643 649 public void addWeeks(final int weeks) { 650 setMillis(getChronology().weeks().add(getMillis(), weeks)); 651 } 652 653 660 public void setDayOfYear(final int dayOfYear) { 661 setMillis(getChronology().dayOfYear().set(getMillis(), dayOfYear)); 662 } 663 664 670 public void setDayOfMonth(final int dayOfMonth) { 671 setMillis(getChronology().dayOfMonth().set(getMillis(), dayOfMonth)); 672 } 673 674 680 public void setDayOfWeek(final int dayOfWeek) { 681 setMillis(getChronology().dayOfWeek().set(getMillis(), dayOfWeek)); 682 } 683 684 690 public void addDays(final int days) { 691 setMillis(getChronology().days().add(getMillis(), days)); 692 } 693 694 701 public void setHourOfDay(final int hourOfDay) { 702 setMillis(getChronology().hourOfDay().set(getMillis(), hourOfDay)); 703 } 704 705 711 public void addHours(final int hours) { 712 setMillis(getChronology().hours().add(getMillis(), hours)); 713 } 714 715 722 public void setMinuteOfDay(final int minuteOfDay) { 723 setMillis(getChronology().minuteOfDay().set(getMillis(), minuteOfDay)); 724 } 725 726 732 public void setMinuteOfHour(final int minuteOfHour) { 733 setMillis(getChronology().minuteOfHour().set(getMillis(), minuteOfHour)); 734 } 735 736 742 public void addMinutes(final int minutes) { 743 setMillis(getChronology().minutes().add(getMillis(), minutes)); 744 } 745 746 753 public void setSecondOfDay(final int secondOfDay) { 754 setMillis(getChronology().secondOfDay().set(getMillis(), secondOfDay)); 755 } 756 757 763 public void setSecondOfMinute(final int secondOfMinute) { 764 setMillis(getChronology().secondOfMinute().set(getMillis(), secondOfMinute)); 765 } 766 767 773 public void addSeconds(final int seconds) { 774 setMillis(getChronology().seconds().add(getMillis(), seconds)); 775 } 776 777 784 public void setMillisOfDay(final int millisOfDay) { 785 setMillis(getChronology().millisOfDay().set(getMillis(), millisOfDay)); 786 } 787 788 794 public void setMillisOfSecond(final int millisOfSecond) { 795 setMillis(getChronology().millisOfSecond().set(getMillis(), millisOfSecond)); 796 } 797 798 806 public void addMillis(final int millis) { 807 setMillis(getChronology().millis().add(getMillis(), millis)); 808 } 809 810 818 public void setDate(final long instant) { 819 setMillis(getChronology().millisOfDay().set(instant, getMillisOfDay())); 820 } 821 822 829 public void setDate(final ReadableInstant instant) { 830 long instantMillis = DateTimeUtils.getInstantMillis(instant); 831 Chronology instantChrono = DateTimeUtils.getInstantChronology(instant); 832 DateTimeZone zone = instantChrono.getZone(); 833 if (zone != null) { 834 instantMillis = zone.getMillisKeepLocal(DateTimeZone.UTC, instantMillis); 835 } 836 setDate(instantMillis); 837 } 838 839 848 public void setDate( 849 final int year, 850 final int monthOfYear, 851 final int dayOfMonth) { 852 Chronology c = getChronology(); 853 long instantMidnight = c.getDateTimeMillis(year, monthOfYear, dayOfMonth, 0); 854 setDate(instantMidnight); 855 } 856 857 865 public void setTime(final long millis) { 866 int millisOfDay = ISOChronology.getInstanceUTC().millisOfDay().get(millis); 867 setMillis(getChronology().millisOfDay().set(getMillis(), millisOfDay)); 868 } 869 870 877 public void setTime(final ReadableInstant instant) { 878 long instantMillis = DateTimeUtils.getInstantMillis(instant); 879 Chronology instantChrono = DateTimeUtils.getInstantChronology(instant); 880 DateTimeZone zone = instantChrono.getZone(); 881 if (zone != null) { 882 instantMillis = zone.getMillisKeepLocal(DateTimeZone.UTC, instantMillis); 883 } 884 setTime(instantMillis); 885 } 886 887 897 public void setTime( 898 final int hour, 899 final int minuteOfHour, 900 final int secondOfMinute, 901 final int millisOfSecond) { 902 long instant = getChronology().getDateTimeMillis( 903 getMillis(), hour, minuteOfHour, secondOfMinute, millisOfSecond); 904 setMillis(instant); 905 } 906 907 919 public void setDateTime( 920 final int year, 921 final int monthOfYear, 922 final int dayOfMonth, 923 final int hourOfDay, 924 final int minuteOfHour, 925 final int secondOfMinute, 926 final int millisOfSecond) { 927 long instant = getChronology().getDateTimeMillis( 928 year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond); 929 setMillis(instant); 930 } 931 932 941 public Property property(DateTimeFieldType type) { 942 if (type == null) { 943 throw new IllegalArgumentException ("The DateTimeFieldType must not be null"); 944 } 945 DateTimeField field = type.getField(getChronology()); 946 if (field.isSupported() == false) { 947 throw new IllegalArgumentException ("Field '" + type + "' is not supported"); 948 } 949 return new Property(this, field); 950 } 951 952 957 public Property era() { 958 return new Property(this, getChronology().era()); 959 } 960 961 966 public Property centuryOfEra() { 967 return new Property(this, getChronology().centuryOfEra()); 968 } 969 970 975 public Property yearOfCentury() { 976 return new Property(this, getChronology().yearOfCentury()); 977 } 978 979 984 public Property yearOfEra() { 985 return new Property(this, getChronology().yearOfEra()); 986 } 987 988 993 public Property year() { 994 return new Property(this, getChronology().year()); 995 } 996 997 1002 public Property weekyear() { 1003 return new Property(this, getChronology().weekyear()); 1004 } 1005 1006 1011 public Property monthOfYear() { 1012 return new Property(this, getChronology().monthOfYear()); 1013 } 1014 1015 1020 public Property weekOfWeekyear() { 1021 return new Property(this, getChronology().weekOfWeekyear()); 1022 } 1023 1024 1029 public Property dayOfYear() { 1030 return new Property(this, getChronology().dayOfYear()); 1031 } 1032 1033 1040 public Property dayOfMonth() { 1041 return new Property(this, getChronology().dayOfMonth()); 1042 } 1043 1044 1051 public Property dayOfWeek() { 1052 return new Property(this, getChronology().dayOfWeek()); 1053 } 1054 1055 1061 public Property hourOfDay() { 1062 return new Property(this, getChronology().hourOfDay()); 1063 } 1064 1065 1070 public Property minuteOfDay() { 1071 return new Property(this, getChronology().minuteOfDay()); 1072 } 1073 1074 1079 public Property minuteOfHour() { 1080 return new Property(this, getChronology().minuteOfHour()); 1081 } 1082 1083 1088 public Property secondOfDay() { 1089 return new Property(this, getChronology().secondOfDay()); 1090 } 1091 1092 1097 public Property secondOfMinute() { 1098 return new Property(this, getChronology().secondOfMinute()); 1099 } 1100 1101 1106 public Property millisOfDay() { 1107 return new Property(this, getChronology().millisOfDay()); 1108 } 1109 1110 1115 public Property millisOfSecond() { 1116 return new Property(this, getChronology().millisOfSecond()); 1117 } 1118 1119 1125 public MutableDateTime copy() { 1126 return (MutableDateTime) clone(); 1127 } 1128 1129 1135 public Object clone() { 1136 try { 1137 return super.clone(); 1138 } catch (CloneNotSupportedException ex) { 1139 throw new InternalError ("Clone error"); 1140 } 1141 } 1142 1143 1148 public String toString() { 1149 return ISODateTimeFormat.dateTime().print(this); 1150 } 1151 1152 1172 public static final class Property extends AbstractReadableInstantFieldProperty { 1173 1174 1175 private static final long serialVersionUID = -4481126543819298617L; 1176 1177 1178 private MutableDateTime iInstant; 1179 1180 private DateTimeField iField; 1181 1182 1188 Property(MutableDateTime instant, DateTimeField field) { 1189 super(); 1190 iInstant = instant; 1191 iField = field; 1192 } 1193 1194 1197 private void writeObject(ObjectOutputStream oos) throws IOException { 1198 oos.writeObject(iInstant); 1199 oos.writeObject(iField.getType()); 1200 } 1201 1202 1205 private void readObject(ObjectInputStream oos) throws IOException , ClassNotFoundException { 1206 iInstant = (MutableDateTime) oos.readObject(); 1207 DateTimeFieldType type = (DateTimeFieldType) oos.readObject(); 1208 iField = type.getField(iInstant.getChronology()); 1209 } 1210 1211 1217 public DateTimeField getField() { 1218 return iField; 1219 } 1220 1221 1226 protected long getMillis() { 1227 return iInstant.getMillis(); 1228 } 1229 1230 1236 protected Chronology getChronology() { 1237 return iInstant.getChronology(); 1238 } 1239 1240 1245 public MutableDateTime getMutableDateTime() { 1246 return iInstant; 1247 } 1248 1249 1257 public MutableDateTime add(int value) { 1258 iInstant.setMillis(getField().add(iInstant.getMillis(), value)); 1259 return iInstant; 1260 } 1261 1262 1269 public MutableDateTime add(long value) { 1270 iInstant.setMillis(getField().add(iInstant.getMillis(), value)); 1271 return iInstant; 1272 } 1273 1274 1281 public MutableDateTime addWrapField(int value) { 1282 iInstant.setMillis(getField().addWrapField(iInstant.getMillis(), value)); 1283 return iInstant; 1284 } 1285 1286 1294 public MutableDateTime set(int value) { 1295 iInstant.setMillis(getField().set(iInstant.getMillis(), value)); 1296 return iInstant; 1297 } 1298 1299 1308 public MutableDateTime set(String text, Locale locale) { 1309 iInstant.setMillis(getField().set(iInstant.getMillis(), text, locale)); 1310 return iInstant; 1311 } 1312 1313 1321 public MutableDateTime set(String text) { 1322 set(text, null); 1323 return iInstant; 1324 } 1325 1326 1333 public MutableDateTime roundFloor() { 1334 iInstant.setMillis(getField().roundFloor(iInstant.getMillis())); 1335 return iInstant; 1336 } 1337 1338 1344 public MutableDateTime roundCeiling() { 1345 iInstant.setMillis(getField().roundCeiling(iInstant.getMillis())); 1346 return iInstant; 1347 } 1348 1349 1356 public MutableDateTime roundHalfFloor() { 1357 iInstant.setMillis(getField().roundHalfFloor(iInstant.getMillis())); 1358 return iInstant; 1359 } 1360 1361 1368 public MutableDateTime roundHalfCeiling() { 1369 iInstant.setMillis(getField().roundHalfCeiling(iInstant.getMillis())); 1370 return iInstant; 1371 } 1372 1373 1380 public MutableDateTime roundHalfEven() { 1381 iInstant.setMillis(getField().roundHalfEven(iInstant.getMillis())); 1382 return iInstant; 1383 } 1384 } 1385 1386} 1387 | Popular Tags |