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 68 public final class TimeOfDay 69 extends BasePartial 70 implements ReadablePartial, Serializable { 71 74 75 private static final long serialVersionUID = 3633353405803318660L; 76 77 private static final DateTimeFieldType[] FIELD_TYPES = new DateTimeFieldType[] { 78 DateTimeFieldType.hourOfDay(), 79 DateTimeFieldType.minuteOfHour(), 80 DateTimeFieldType.secondOfMinute(), 81 DateTimeFieldType.millisOfSecond(), 82 }; 83 84 85 public static final TimeOfDay MIDNIGHT = new TimeOfDay(0, 0, 0, 0); 86 87 88 public static final int HOUR_OF_DAY = 0; 89 90 public static final int MINUTE_OF_HOUR = 1; 91 92 public static final int SECOND_OF_MINUTE = 2; 93 94 public static final int MILLIS_OF_SECOND = 3; 95 96 116 public static TimeOfDay fromCalendarFields(Calendar calendar) { 117 if (calendar == null) { 118 throw new IllegalArgumentException ("The calendar must not be null"); 119 } 120 return new TimeOfDay( 121 calendar.get(Calendar.HOUR_OF_DAY), 122 calendar.get(Calendar.MINUTE), 123 calendar.get(Calendar.SECOND), 124 calendar.get(Calendar.MILLISECOND) 125 ); 126 } 127 128 146 public static TimeOfDay fromDateFields(Date date) { 147 if (date == null) { 148 throw new IllegalArgumentException ("The date must not be null"); 149 } 150 return new TimeOfDay( 151 date.getHours(), 152 date.getMinutes(), 153 date.getSeconds(), 154 (int) (date.getTime() % 1000) 155 ); 156 } 157 158 169 public static TimeOfDay fromMillisOfDay(long millisOfDay) { 170 return fromMillisOfDay(millisOfDay, null); 171 } 172 173 184 public static TimeOfDay fromMillisOfDay(long millisOfDay, Chronology chrono) { 185 chrono = DateTimeUtils.getChronology(chrono); 186 chrono = chrono.withUTC(); 187 return new TimeOfDay(millisOfDay, chrono); 188 } 189 190 200 public TimeOfDay() { 201 super(); 202 } 203 204 215 public TimeOfDay(DateTimeZone zone) { 216 super(ISOChronology.getInstance(zone)); 217 } 218 219 229 public TimeOfDay(Chronology chronology) { 230 super(chronology); 231 } 232 233 243 public TimeOfDay(long instant) { 244 super(instant); 245 } 246 247 258 public TimeOfDay(long instant, Chronology chronology) { 259 super(instant, chronology); 260 } 261 262 278 public TimeOfDay(Object instant) { 279 super(instant, null, ISODateTimeFormat.timeParser()); 280 } 281 282 303 public TimeOfDay(Object instant, Chronology chronology) { 304 super(instant, DateTimeUtils.getChronology(chronology), ISODateTimeFormat.timeParser()); 305 } 306 307 318 public TimeOfDay(int hourOfDay, int minuteOfHour) { 319 this(hourOfDay, minuteOfHour, 0, 0, null); 320 } 321 322 333 public TimeOfDay(int hourOfDay, int minuteOfHour, Chronology chronology) { 334 this(hourOfDay, minuteOfHour, 0, 0, chronology); 335 } 336 337 349 public TimeOfDay(int hourOfDay, int minuteOfHour, int secondOfMinute) { 350 this(hourOfDay, minuteOfHour, secondOfMinute, 0, null); 351 } 352 353 365 public TimeOfDay(int hourOfDay, int minuteOfHour, int secondOfMinute, Chronology chronology) { 366 this(hourOfDay, minuteOfHour, secondOfMinute, 0, chronology); 367 } 368 369 382 public TimeOfDay(int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond) { 383 this(hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond, null); 384 } 385 386 399 public TimeOfDay(int hourOfDay, int minuteOfHour, 400 int secondOfMinute, int millisOfSecond, Chronology chronology) { 401 super(new int[] {hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond}, chronology); 402 } 403 404 410 TimeOfDay(TimeOfDay partial, int[] values) { 411 super(partial, values); 412 } 413 414 420 TimeOfDay(TimeOfDay partial, Chronology chrono) { 421 super(partial, chrono); 422 } 423 424 430 public int size() { 431 return 4; 432 } 433 434 443 protected DateTimeField getField(int index, Chronology chrono) { 444 switch (index) { 445 case HOUR_OF_DAY: 446 return chrono.hourOfDay(); 447 case MINUTE_OF_HOUR: 448 return chrono.minuteOfHour(); 449 case SECOND_OF_MINUTE: 450 return chrono.secondOfMinute(); 451 case MILLIS_OF_SECOND: 452 return chrono.millisOfSecond(); 453 default: 454 throw new IndexOutOfBoundsException ("Invalid index: " + index); 455 } 456 } 457 458 465 public DateTimeFieldType getFieldType(int index) { 466 return FIELD_TYPES[index]; 467 } 468 469 476 public DateTimeFieldType[] getFieldTypes() { 477 return (DateTimeFieldType[]) FIELD_TYPES.clone(); 478 } 479 480 495 public TimeOfDay withChronologyRetainFields(Chronology newChronology) { 496 newChronology = DateTimeUtils.getChronology(newChronology); 497 newChronology = newChronology.withUTC(); 498 if (newChronology == getChronology()) { 499 return this; 500 } else { 501 TimeOfDay newTimeOfDay = new TimeOfDay(this, newChronology); 502 newChronology.validate(newTimeOfDay, getValues()); 503 return newTimeOfDay; 504 } 505 } 506 507 525 public TimeOfDay withField(DateTimeFieldType fieldType, int value) { 526 int index = indexOfSupported(fieldType); 527 if (value == getValue(index)) { 528 return this; 529 } 530 int[] newValues = getValues(); 531 newValues = getField(index).set(this, index, newValues, value); 532 return new TimeOfDay(this, newValues); 533 } 534 535 554 public TimeOfDay withFieldAdded(DurationFieldType fieldType, int amount) { 555 int index = indexOfSupported(fieldType); 556 if (amount == 0) { 557 return this; 558 } 559 int[] newValues = getValues(); 560 newValues = getField(index).addWrapPartial(this, index, newValues, amount); 561 return new TimeOfDay(this, newValues); 562 } 563 564 581 public TimeOfDay withPeriodAdded(ReadablePeriod period, int scalar) { 582 if (period == null || scalar == 0) { 583 return this; 584 } 585 int[] newValues = getValues(); 586 for (int i = 0; i < period.size(); i++) { 587 DurationFieldType fieldType = period.getFieldType(i); 588 int index = indexOf(fieldType); 589 if (index >= 0) { 590 newValues = getField(index).addWrapPartial(this, index, newValues, 591 FieldUtils.safeMultiply(period.getValue(i), scalar)); 592 } 593 } 594 return new TimeOfDay(this, newValues); 595 } 596 597 612 public TimeOfDay plus(ReadablePeriod period) { 613 return withPeriodAdded(period, 1); 614 } 615 616 633 public TimeOfDay plusHours(int hours) { 634 return withFieldAdded(DurationFieldType.hours(), hours); 635 } 636 637 653 public TimeOfDay plusMinutes(int minutes) { 654 return withFieldAdded(DurationFieldType.minutes(), minutes); 655 } 656 657 673 public TimeOfDay plusSeconds(int seconds) { 674 return withFieldAdded(DurationFieldType.seconds(), seconds); 675 } 676 677 693 public TimeOfDay plusMillis(int millis) { 694 return withFieldAdded(DurationFieldType.millis(), millis); 695 } 696 697 712 public TimeOfDay minus(ReadablePeriod period) { 713 return withPeriodAdded(period, -1); 714 } 715 716 733 public TimeOfDay minusHours(int hours) { 734 return withFieldAdded(DurationFieldType.hours(), FieldUtils.safeNegate(hours)); 735 } 736 737 753 public TimeOfDay minusMinutes(int minutes) { 754 return withFieldAdded(DurationFieldType.minutes(), FieldUtils.safeNegate(minutes)); 755 } 756 757 773 public TimeOfDay minusSeconds(int seconds) { 774 return withFieldAdded(DurationFieldType.seconds(), FieldUtils.safeNegate(seconds)); 775 } 776 777 793 public TimeOfDay minusMillis(int millis) { 794 return withFieldAdded(DurationFieldType.millis(), FieldUtils.safeNegate(millis)); 795 } 796 797 806 public Property property(DateTimeFieldType type) { 807 return new Property(this, indexOfSupported(type)); 808 } 809 810 817 public LocalTime toLocalTime() { 818 return new LocalTime(getHourOfDay(), getMinuteOfHour(), 819 getSecondOfMinute(), getMillisOfSecond(), getChronology()); 820 } 821 822 830 public DateTime toDateTimeToday() { 831 return toDateTimeToday(null); 832 } 833 834 845 public DateTime toDateTimeToday(DateTimeZone zone) { 846 Chronology chrono = getChronology().withZone(zone); 847 long instantMillis = DateTimeUtils.currentTimeMillis(); 848 long resolved = chrono.set(this, instantMillis); 849 return new DateTime(resolved, chrono); 850 } 851 852 858 public int getHourOfDay() { 859 return getValue(HOUR_OF_DAY); 860 } 861 862 867 public int getMinuteOfHour() { 868 return getValue(MINUTE_OF_HOUR); 869 } 870 871 876 public int getSecondOfMinute() { 877 return getValue(SECOND_OF_MINUTE); 878 } 879 880 885 public int getMillisOfSecond() { 886 return getValue(MILLIS_OF_SECOND); 887 } 888 889 902 public TimeOfDay withHourOfDay(int hour) { 903 int[] newValues = getValues(); 904 newValues = getChronology().hourOfDay().set(this, HOUR_OF_DAY, newValues, hour); 905 return new TimeOfDay(this, newValues); 906 } 907 908 920 public TimeOfDay withMinuteOfHour(int minute) { 921 int[] newValues = getValues(); 922 newValues = getChronology().minuteOfHour().set(this, MINUTE_OF_HOUR, newValues, minute); 923 return new TimeOfDay(this, newValues); 924 } 925 926 938 public TimeOfDay withSecondOfMinute(int second) { 939 int[] newValues = getValues(); 940 newValues = getChronology().secondOfMinute().set(this, SECOND_OF_MINUTE, newValues, second); 941 return new TimeOfDay(this, newValues); 942 } 943 944 956 public TimeOfDay withMillisOfSecond(int millis) { 957 int[] newValues = getValues(); 958 newValues = getChronology().millisOfSecond().set(this, MILLIS_OF_SECOND, newValues, millis); 959 return new TimeOfDay(this, newValues); 960 } 961 962 968 public Property hourOfDay() { 969 return new Property(this, HOUR_OF_DAY); 970 } 971 972 977 public Property minuteOfHour() { 978 return new Property(this, MINUTE_OF_HOUR); 979 } 980 981 986 public Property secondOfMinute() { 987 return new Property(this, SECOND_OF_MINUTE); 988 } 989 990 995 public Property millisOfSecond() { 996 return new Property(this, MILLIS_OF_SECOND); 997 } 998 999 1005 public String toString() { 1006 return ISODateTimeFormat.tTime().print(this); 1007 } 1008 1009 1018 public static class Property extends AbstractPartialFieldProperty implements Serializable { 1019 1020 1021 private static final long serialVersionUID = 5598459141741063833L; 1022 1023 1024 private final TimeOfDay iTimeOfDay; 1025 1026 private final int iFieldIndex; 1027 1028 1034 Property(TimeOfDay partial, int fieldIndex) { 1035 super(); 1036 iTimeOfDay = partial; 1037 iFieldIndex = fieldIndex; 1038 } 1039 1040 1045 public DateTimeField getField() { 1046 return iTimeOfDay.getField(iFieldIndex); 1047 } 1048 1049 1054 protected ReadablePartial getReadablePartial() { 1055 return iTimeOfDay; 1056 } 1057 1058 1063 public TimeOfDay getTimeOfDay() { 1064 return iTimeOfDay; 1065 } 1066 1067 1072 public int get() { 1073 return iTimeOfDay.getValue(iFieldIndex); 1074 } 1075 1076 1096 public TimeOfDay addToCopy(int valueToAdd) { 1097 int[] newValues = iTimeOfDay.getValues(); 1098 newValues = getField().addWrapPartial(iTimeOfDay, iFieldIndex, newValues, valueToAdd); 1099 return new TimeOfDay(iTimeOfDay, newValues); 1100 } 1101 1102 1122 public TimeOfDay addNoWrapToCopy(int valueToAdd) { 1123 int[] newValues = iTimeOfDay.getValues(); 1124 newValues = getField().add(iTimeOfDay, iFieldIndex, newValues, valueToAdd); 1125 return new TimeOfDay(iTimeOfDay, newValues); 1126 } 1127 1128 1146 public TimeOfDay addWrapFieldToCopy(int valueToAdd) { 1147 int[] newValues = iTimeOfDay.getValues(); 1148 newValues = getField().addWrapField(iTimeOfDay, iFieldIndex, newValues, valueToAdd); 1149 return new TimeOfDay(iTimeOfDay, newValues); 1150 } 1151 1152 1163 public TimeOfDay setCopy(int value) { 1164 int[] newValues = iTimeOfDay.getValues(); 1165 newValues = getField().set(iTimeOfDay, iFieldIndex, newValues, value); 1166 return new TimeOfDay(iTimeOfDay, newValues); 1167 } 1168 1169 1180 public TimeOfDay setCopy(String text, Locale locale) { 1181 int[] newValues = iTimeOfDay.getValues(); 1182 newValues = getField().set(iTimeOfDay, iFieldIndex, newValues, text, locale); 1183 return new TimeOfDay(iTimeOfDay, newValues); 1184 } 1185 1186 1196 public TimeOfDay setCopy(String text) { 1197 return setCopy(text, null); 1198 } 1199 1200 1210 public TimeOfDay withMaximumValue() { 1211 return setCopy(getMaximumValue()); 1212 } 1213 1214 1223 public TimeOfDay withMinimumValue() { 1224 return setCopy(getMinimumValue()); 1225 } 1226 } 1227 1228} 1229 | Popular Tags |