public void testWithFieldAdded_DurationFieldType_int_6() { LocalTime test = new LocalTime(10, 20, 30, 40); LocalTime result = test.withFieldAdded(DurationFieldType.hours(), 16); assertEquals(new LocalTime(10, 20, 30, 40), test); assertEquals(new LocalTime(2, 20, 30, 40), result); }
/** * Gets a type that defines the year and day fields. * * <ul> * <li>years * <li>days * </ul> * * @return the period type * @since 1.1 */ public static PeriodType yearDay() { PeriodType type = cYD; if (type == null) { type = new PeriodType( "YearDay", new DurationFieldType[] { DurationFieldType.years(), DurationFieldType.days(), }, new int[] { 0, -1, -1, 1, -1, -1, -1, -1, }); cYD = type; } return type; }
/** * Gets a type that defines all standard time fields. * * <ul> * <li>hours * <li>minutes * <li>seconds * <li>milliseconds * </ul> * * @return the period type */ public static PeriodType time() { PeriodType type = cTime; if (type == null) { type = new PeriodType( "Time", new DurationFieldType[] { DurationFieldType.hours(), DurationFieldType.minutes(), DurationFieldType.seconds(), DurationFieldType.millis(), }, new int[] { -1, -1, -1, -1, 0, 1, 2, 3, }); cTime = type; } return type; }
public void testWithFieldAdded_DurationFieldType_int_5() { LocalTime test = new LocalTime(10, 20, 30, 40); try { test.withFieldAdded(DurationFieldType.days(), 6); fail(); } catch (IllegalArgumentException ex) { } }
public void testWithFieldAdded_DurationFieldType_int_8() { LocalTime test = new LocalTime(0, 0, 0, 0); LocalTime result = test.withFieldAdded(DurationFieldType.millis(), -1); assertEquals(new LocalTime(23, 59, 59, 999), result); test = new LocalTime(0, 0, 0, 0); result = test.withFieldAdded(DurationFieldType.seconds(), -1); assertEquals(new LocalTime(23, 59, 59, 0), result); test = new LocalTime(0, 0, 0, 0); result = test.withFieldAdded(DurationFieldType.minutes(), -1); assertEquals(new LocalTime(23, 59, 0, 0), result); test = new LocalTime(0, 0, 0, 0); result = test.withFieldAdded(DurationFieldType.hours(), -1); assertEquals(new LocalTime(23, 0, 0, 0), result); }
/** * Creates a <code>Weeks</code> representing the number of whole weeks in the specified interval. * * @param interval the interval to extract weeks from, null returns zero * @return the period in weeks * @throws IllegalArgumentException if the partials are null or invalid */ public static Weeks weeksIn(ReadableInterval interval) { if (interval == null) { return Weeks.ZERO; } int amount = BaseSingleFieldPeriod.between( interval.getStart(), interval.getEnd(), DurationFieldType.weeks()); return Weeks.weeks(amount); }
/** * Gets a type that defines just the months field. * * @return the period type */ public static PeriodType months() { PeriodType type = cMonths; if (type == null) { type = new PeriodType( "Months", new DurationFieldType[] {DurationFieldType.months()}, new int[] { -1, 0, -1, -1, -1, -1, -1, -1, }); cMonths = type; } return type; }
/** * Gets a type that defines just the hours field. * * @return the period type */ public static PeriodType hours() { PeriodType type = cHours; if (type == null) { type = new PeriodType( "Hours", new DurationFieldType[] {DurationFieldType.hours()}, new int[] { -1, -1, -1, -1, 0, -1, -1, -1, }); cHours = type; } return type; }
/** * Gets a type that defines just the weeks field. * * @return the period type */ public static PeriodType weeks() { PeriodType type = cWeeks; if (type == null) { type = new PeriodType( "Weeks", new DurationFieldType[] {DurationFieldType.weeks()}, new int[] { -1, -1, 0, -1, -1, -1, -1, -1, }); cWeeks = type; } return type; }
/** * Gets a type that defines just the years field. * * @return the period type */ public static PeriodType years() { PeriodType type = cYears; if (type == null) { type = new PeriodType( "Years", new DurationFieldType[] {DurationFieldType.years()}, new int[] { 0, -1, -1, -1, -1, -1, -1, -1, }); cYears = type; } return type; }
/** * Gets a type that defines just the millis field. * * @return the period type */ public static PeriodType millis() { PeriodType type = cMillis; if (type == null) { type = new PeriodType( "Millis", new DurationFieldType[] {DurationFieldType.millis()}, new int[] { -1, -1, -1, -1, -1, -1, -1, 0, }); cMillis = type; } return type; }
/** * Gets a type that defines just the days field. * * @return the period type */ public static PeriodType days() { PeriodType type = cDays; if (type == null) { type = new PeriodType( "Days", new DurationFieldType[] {DurationFieldType.days()}, new int[] { -1, -1, -1, 0, -1, -1, -1, -1, }); cDays = type; } return type; }
/** * Gets a type that defines just the seconds field. * * @return the period type */ public static PeriodType seconds() { PeriodType type = cSeconds; if (type == null) { type = new PeriodType( "Seconds", new DurationFieldType[] {DurationFieldType.seconds()}, new int[] { -1, -1, -1, -1, -1, -1, 0, -1, }); cSeconds = type; } return type; }
public void testIsSupported_DurationFieldType() { LocalTime test = new LocalTime(10, 20, 30, 40); assertEquals(true, test.isSupported(DurationFieldType.hours())); assertEquals(true, test.isSupported(DurationFieldType.minutes())); assertEquals(true, test.isSupported(DurationFieldType.seconds())); assertEquals(true, test.isSupported(DurationFieldType.millis())); assertEquals(true, test.isSupported(DurationFieldType.halfdays())); assertEquals(false, test.isSupported(DurationFieldType.days())); assertEquals(false, test.isSupported((DurationFieldType) null)); }
/** * Gets a type that defines all standard fields. * * <ul> * <li>years * <li>months * <li>weeks * <li>days * <li>hours * <li>minutes * <li>seconds * <li>milliseconds * </ul> * * @return the period type */ public static PeriodType standard() { PeriodType type = cStandard; if (type == null) { type = new PeriodType( "Standard", new DurationFieldType[] { DurationFieldType.years(), DurationFieldType.months(), DurationFieldType.weeks(), DurationFieldType.days(), DurationFieldType.hours(), DurationFieldType.minutes(), DurationFieldType.seconds(), DurationFieldType.millis(), }, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, }); cStandard = type; } return type; }
/** * Gets a type that defines all standard fields except months and weeks. * * <ul> * <li>years * <li>days * <li>hours * <li>minutes * <li>seconds * <li>milliseconds * </ul> * * @return the period type */ public static PeriodType yearDayTime() { PeriodType type = cYDTime; if (type == null) { type = new PeriodType( "YearDayTime", new DurationFieldType[] { DurationFieldType.years(), DurationFieldType.days(), DurationFieldType.hours(), DurationFieldType.minutes(), DurationFieldType.seconds(), DurationFieldType.millis(), }, new int[] { 0, -1, -1, 1, 2, 3, 4, 5, }); cYDTime = type; } return type; }
/** * Gets the duration field type, which is <code>years</code>. * * @return the period type */ public DurationFieldType getFieldType() { return DurationFieldType.years(); }
/** * Adds to the instant specifying the duration and multiple to add. * * @param type a field type, usually obtained from DateTimeFieldType, not null * @param amount the amount to add of this duration * @throws IllegalArgumentException if the value is null or invalid * @throws ArithmeticException if the result exceeds the capacity of the instant */ public void add(DurationFieldType type, int amount) { if (type == null) { throw new IllegalArgumentException("Field must not be null"); } setMillis(type.getField(getChronology()).add(getMillis(), amount)); }
/** * Returns a copy of this time plus the specified number of millis. * * <p>This time instance is immutable and unaffected by this method call. * * <p>The following three lines are identical in effect: * * <pre> * TimeOfDay added = dt.plusMillis(6); * TimeOfDay added = dt.plus(Period.millis(6)); * TimeOfDay added = dt.withFieldAdded(DurationFieldType.millis(), 6); * </pre> * * @param millis the amount of millis to add, may be negative * @return the new time plus the increased millis * @since 1.1 */ public TimeOfDay plusMillis(int millis) { return withFieldAdded(DurationFieldType.millis(), millis); }
/** * Returns a copy of this time minus the specified number of hours. * * <p>This time instance is immutable and unaffected by this method call. * * <p>The following three lines are identical in effect: * * <pre> * TimeOfDay subtracted = dt.minusHours(6); * TimeOfDay subtracted = dt.minus(Period.hours(6)); * TimeOfDay subtracted = dt.withFieldAdded(DurationFieldType.hours(), -6); * </pre> * * @param hours the amount of hours to subtract, may be negative * @return the new time minus the increased hours * @since 1.1 */ public TimeOfDay minusHours(int hours) { return withFieldAdded(DurationFieldType.hours(), FieldUtils.safeNegate(hours)); }
/** * Returns a copy of this time minus the specified number of seconds. * * <p>This time instance is immutable and unaffected by this method call. * * <p>The following three lines are identical in effect: * * <pre> * TimeOfDay subtracted = dt.minusSeconds(6); * TimeOfDay subtracted = dt.minus(Period.seconds(6)); * TimeOfDay subtracted = dt.withFieldAdded(DurationFieldType.seconds(), -6); * </pre> * * @param seconds the amount of seconds to subtract, may be negative * @return the new time minus the increased seconds * @since 1.1 */ public TimeOfDay minusSeconds(int seconds) { return withFieldAdded(DurationFieldType.seconds(), FieldUtils.safeNegate(seconds)); }
public void testWithFieldAdded_DurationFieldType_int_4() { LocalTime test = new LocalTime(10, 20, 30, 40); LocalTime result = test.withFieldAdded(DurationFieldType.hours(), 0); assertSame(test, result); }
/** * Constructs a Partial with the specified fields and values. The fields must be specified in the * order largest to smallest. For year and weekyear fields with equal duration, year is defined as * being larger than weekyear. * * <p>The constructor uses the specified chronology. * * @param types the types to create the partial from, not null * @param values the values to store, not null * @param chronology the chronology, null means ISO * @throws IllegalArgumentException if the types or values are invalid */ public Partial(DateTimeFieldType[] types, int[] values, Chronology chronology) { super(); chronology = DateTimeUtils.getChronology(chronology).withUTC(); iChronology = chronology; if (types == null) { throw new IllegalArgumentException("Types array must not be null"); } if (values == null) { throw new IllegalArgumentException("Values array must not be null"); } if (values.length != types.length) { throw new IllegalArgumentException("Values array must be the same length as the types array"); } if (types.length == 0) { iTypes = types; iValues = values; return; } for (int i = 0; i < types.length; i++) { if (types[i] == null) { throw new IllegalArgumentException("Types array must not contain null: index " + i); } } DurationField lastUnitField = null; for (int i = 0; i < types.length; i++) { DateTimeFieldType loopType = types[i]; DurationField loopUnitField = loopType.getDurationType().getField(iChronology); if (i > 0) { if (loopUnitField.isSupported() == false) { if (lastUnitField.isSupported()) { throw new IllegalArgumentException( "Types array must be in order largest-smallest: " + types[i - 1].getName() + " < " + loopType.getName()); } else { throw new IllegalArgumentException( "Types array must not contain duplicate unsupported: " + types[i - 1].getName() + " and " + loopType.getName()); } } int compare = lastUnitField.compareTo(loopUnitField); if (compare < 0) { throw new IllegalArgumentException( "Types array must be in order largest-smallest: " + types[i - 1].getName() + " < " + loopType.getName()); } else if (compare == 0) { if (lastUnitField.equals(loopUnitField)) { DurationFieldType lastRangeType = types[i - 1].getRangeDurationType(); DurationFieldType loopRangeType = loopType.getRangeDurationType(); if (lastRangeType == null) { if (loopRangeType == null) { throw new IllegalArgumentException( "Types array must not contain duplicate: " + types[i - 1].getName() + " and " + loopType.getName()); } } else { if (loopRangeType == null) { throw new IllegalArgumentException( "Types array must be in order largest-smallest: " + types[i - 1].getName() + " < " + loopType.getName()); } DurationField lastRangeField = lastRangeType.getField(iChronology); DurationField loopRangeField = loopRangeType.getField(iChronology); if (lastRangeField.compareTo(loopRangeField) < 0) { throw new IllegalArgumentException( "Types array must be in order largest-smallest: " + types[i - 1].getName() + " < " + loopType.getName()); } if (lastRangeField.compareTo(loopRangeField) == 0) { throw new IllegalArgumentException( "Types array must not contain duplicate: " + types[i - 1].getName() + " and " + loopType.getName()); } } } else { if (lastUnitField.isSupported() && lastUnitField.getType() != DurationFieldType.YEARS_TYPE) { throw new IllegalArgumentException( "Types array must be in order largest-smallest," + " for year-based fields, years is defined as being largest: " + types[i - 1].getName() + " < " + loopType.getName()); } } } } lastUnitField = loopUnitField; } iTypes = (DateTimeFieldType[]) types.clone(); chronology.validate(this, values); iValues = (int[]) values.clone(); }
/** * Gets a period type that contains the duration types of the array. * * <p>Only the 8 standard duration field types are supported. * * @param types the types to include in the array. * @return the period type * @since 1.1 */ public static synchronized PeriodType forFields(DurationFieldType[] types) { if (types == null || types.length == 0) { throw new IllegalArgumentException("Types array must not be null or empty"); } for (int i = 0; i < types.length; i++) { if (types[i] == null) { throw new IllegalArgumentException("Types array must not contain null"); } } Map<PeriodType, PeriodType> cache = cTypes; if (cTypes.isEmpty()) { cache.put(standard(), standard()); cache.put(yearMonthDayTime(), yearMonthDayTime()); cache.put(yearMonthDay(), yearMonthDay()); cache.put(yearWeekDayTime(), yearWeekDayTime()); cache.put(yearWeekDay(), yearWeekDay()); cache.put(yearDayTime(), yearDayTime()); cache.put(yearDay(), yearDay()); cache.put(dayTime(), dayTime()); cache.put(time(), time()); cache.put(years(), years()); cache.put(months(), months()); cache.put(weeks(), weeks()); cache.put(days(), days()); cache.put(hours(), hours()); cache.put(minutes(), minutes()); cache.put(seconds(), seconds()); cache.put(millis(), millis()); } PeriodType inPartType = new PeriodType(null, types, null); Object cached = cache.get(inPartType); if (cached instanceof PeriodType) { return (PeriodType) cached; } if (cached != null) { throw new IllegalArgumentException("PeriodType does not support fields: " + cached); } PeriodType type = standard(); List<DurationFieldType> list = new ArrayList<DurationFieldType>(Arrays.asList(types)); if (list.remove(DurationFieldType.years()) == false) { type = type.withYearsRemoved(); } if (list.remove(DurationFieldType.months()) == false) { type = type.withMonthsRemoved(); } if (list.remove(DurationFieldType.weeks()) == false) { type = type.withWeeksRemoved(); } if (list.remove(DurationFieldType.days()) == false) { type = type.withDaysRemoved(); } if (list.remove(DurationFieldType.hours()) == false) { type = type.withHoursRemoved(); } if (list.remove(DurationFieldType.minutes()) == false) { type = type.withMinutesRemoved(); } if (list.remove(DurationFieldType.seconds()) == false) { type = type.withSecondsRemoved(); } if (list.remove(DurationFieldType.millis()) == false) { type = type.withMillisRemoved(); } if (list.size() > 0) { // cache.put(inPartType, list); throw new IllegalArgumentException("PeriodType does not support fields: " + list); } // recheck cache in case initial array order was wrong PeriodType checkPartType = new PeriodType(null, type.iTypes, null); PeriodType checkedType = (PeriodType) cache.get(checkPartType); if (checkedType != null) { cache.put(inPartType, checkedType); return checkedType; } cache.put(inPartType, type); return type; }
/** * Returns a copy of this time minus the specified number of millis. * * <p>This time instance is immutable and unaffected by this method call. * * <p>The following three lines are identical in effect: * * <pre> * TimeOfDay subtracted = dt.minusMillis(6); * TimeOfDay subtracted = dt.minus(Period.millis(6)); * TimeOfDay subtracted = dt.withFieldAdded(DurationFieldType.millis(), -6); * </pre> * * @param millis the amount of millis to subtract, may be negative * @return the new time minus the increased millis * @since 1.1 */ public TimeOfDay minusMillis(int millis) { return withFieldAdded(DurationFieldType.millis(), FieldUtils.safeNegate(millis)); }
/** * Creates a <code>Years</code> representing the number of whole years between the two specified * datetimes. This method corectly handles any daylight savings time changes that may occur during * the interval. * * @param start the start instant, must not be null * @param end the end instant, must not be null * @return the period in years * @throws IllegalArgumentException if the instants are null or invalid */ public static Years yearsBetween(ReadableInstant start, ReadableInstant end) { int amount = BaseSingleFieldPeriod.between(start, end, DurationFieldType.years()); return Years.years(amount); }
/** * Gets the duration field type, which is <code>weeks</code>. * * @return the period type */ public DurationFieldType getFieldType() { return DurationFieldType.weeks(); }
/** * Returns a copy of this time plus the specified number of seconds. * * <p>This time instance is immutable and unaffected by this method call. * * <p>The following three lines are identical in effect: * * <pre> * TimeOfDay added = dt.plusSeconds(6); * TimeOfDay added = dt.plus(Period.seconds(6)); * TimeOfDay added = dt.withFieldAdded(DurationFieldType.seconds(), 6); * </pre> * * @param seconds the amount of seconds to add, may be negative * @return the new time plus the increased seconds * @since 1.1 */ public TimeOfDay plusSeconds(int seconds) { return withFieldAdded(DurationFieldType.seconds(), seconds); }
/** * Creates a <code>Weeks</code> representing the number of whole weeks between the two specified * datetimes. * * @param start the start instant, must not be null * @param end the end instant, must not be null * @return the period in weeks * @throws IllegalArgumentException if the instants are null or invalid */ public static Weeks weeksBetween(ReadableInstant start, ReadableInstant end) { int amount = BaseSingleFieldPeriod.between(start, end, DurationFieldType.weeks()); return Weeks.weeks(amount); }
/** * Returns a copy of this time plus the specified number of hours. * * <p>This time instance is immutable and unaffected by this method call. * * <p>The following three lines are identical in effect: * * <pre> * TimeOfDay added = dt.plusHours(6); * TimeOfDay added = dt.plus(Period.hours(6)); * TimeOfDay added = dt.withFieldAdded(DurationFieldType.hours(), 6); * </pre> * * @param hours the amount of hours to add, may be negative * @return the new time plus the increased hours * @since 1.1 */ public TimeOfDay plusHours(int hours) { return withFieldAdded(DurationFieldType.hours(), hours); }