Java Code Examples for org.joda.time.field.FieldUtils#safeAdd()

The following examples show how to use org.joda.time.field.FieldUtils#safeAdd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Cardumen_00189_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified offset to UTC in hours and minutes.
 * This method assumes 60 minutes in an hour, and standard length minutes.
 * <p>
 * This factory is a convenient way of constructing zones with a fixed offset.
 * The minutes value is always positive and in the range 0 to 59.
 * If constructed with the values (-2, 30), the resulting zone is '-02:30'.
 * 
 * @param hoursOffset  the offset in hours from UTC
 * @param minutesOffset  the offset in minutes from UTC, must be between 0 and 59 inclusive
 * @return the DateTimeZone object for the offset
 * @throws IllegalArgumentException if the offset or minute is too large or too small
 */
public static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) throws IllegalArgumentException {
    if (hoursOffset == 0 && minutesOffset == 0) {
        return DateTimeZone.UTC;
    }
    if (minutesOffset < 0 || minutesOffset > 59) {
        throw new IllegalArgumentException("Minutes out of range: " + minutesOffset);
    }
    int offset = 0;
    try {
        int hoursInMinutes = FieldUtils.safeMultiply(hoursOffset, 60);
        if (hoursInMinutes < 0) {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, -minutesOffset);
        } else {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, minutesOffset);
        }
        offset = FieldUtils.safeMultiply(minutesOffset, DateTimeConstants.MILLIS_PER_MINUTE);
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException("Offset is too large");
    }
    return forOffsetMillis(offset);
}
 
Example 2
Source File: Time_17_DateTimeZone_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified offset to UTC in hours and minutes.
 * This method assumes 60 minutes in an hour, and standard length minutes.
 * <p>
 * This factory is a convenient way of constructing zones with a fixed offset.
 * The minutes value is always positive and in the range 0 to 59.
 * If constructed with the values (-2, 30), the resulting zone is '-02:30'.
 * 
 * @param hoursOffset  the offset in hours from UTC
 * @param minutesOffset  the offset in minutes from UTC, must be between 0 and 59 inclusive
 * @return the DateTimeZone object for the offset
 * @throws IllegalArgumentException if the offset or minute is too large or too small
 */
public static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) throws IllegalArgumentException {
    if (hoursOffset == 0 && minutesOffset == 0) {
        return DateTimeZone.UTC;
    }
    if (minutesOffset < 0 || minutesOffset > 59) {
        throw new IllegalArgumentException("Minutes out of range: " + minutesOffset);
    }
    int offset = 0;
    try {
        int hoursInMinutes = FieldUtils.safeMultiply(hoursOffset, 60);
        if (hoursInMinutes < 0) {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, -minutesOffset);
        } else {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, minutesOffset);
        }
        offset = FieldUtils.safeMultiply(minutesOffset, DateTimeConstants.MILLIS_PER_MINUTE);
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException("Offset is too large");
    }
    return forOffsetMillis(offset);
}
 
Example 3
Source File: Cardumen_00239_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified offset to UTC in hours and minutes.
 * This method assumes 60 minutes in an hour, and standard length minutes.
 * <p>
 * This factory is a convenient way of constructing zones with a fixed offset.
 * The minutes value is always positive and in the range 0 to 59.
 * If constructed with the values (-2, 30), the resulting zone is '-02:30'.
 * 
 * @param hoursOffset  the offset in hours from UTC
 * @param minutesOffset  the offset in minutes from UTC, must be between 0 and 59 inclusive
 * @return the DateTimeZone object for the offset
 * @throws IllegalArgumentException if the offset or minute is too large or too small
 */
public static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) throws IllegalArgumentException {
    if (hoursOffset == 0 && minutesOffset == 0) {
        return DateTimeZone.UTC;
    }
    if (minutesOffset < 0 || minutesOffset > 59) {
        throw new IllegalArgumentException("Minutes out of range: " + minutesOffset);
    }
    int offset = 0;
    try {
        int hoursInMinutes = FieldUtils.safeMultiply(hoursOffset, 60);
        if (hoursInMinutes < 0) {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, -minutesOffset);
        } else {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, minutesOffset);
        }
        offset = FieldUtils.safeMultiply(minutesOffset, DateTimeConstants.MILLIS_PER_MINUTE);
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException("Offset is too large");
    }
    return forOffsetMillis(offset);
}
 
Example 4
Source File: Time_19_DateTimeZone_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified offset to UTC in hours and minutes.
 * This method assumes 60 minutes in an hour, and standard length minutes.
 * <p>
 * This factory is a convenient way of constructing zones with a fixed offset.
 * The minutes value is always positive and in the range 0 to 59.
 * If constructed with the values (-2, 30), the resulting zone is '-02:30'.
 * 
 * @param hoursOffset  the offset in hours from UTC
 * @param minutesOffset  the offset in minutes from UTC, must be between 0 and 59 inclusive
 * @return the DateTimeZone object for the offset
 * @throws IllegalArgumentException if the offset or minute is too large or too small
 */
public static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) throws IllegalArgumentException {
    if (hoursOffset == 0 && minutesOffset == 0) {
        return DateTimeZone.UTC;
    }
    if (minutesOffset < 0 || minutesOffset > 59) {
        throw new IllegalArgumentException("Minutes out of range: " + minutesOffset);
    }
    int offset = 0;
    try {
        int hoursInMinutes = FieldUtils.safeMultiply(hoursOffset, 60);
        if (hoursInMinutes < 0) {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, -minutesOffset);
        } else {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, minutesOffset);
        }
        offset = FieldUtils.safeMultiply(minutesOffset, DateTimeConstants.MILLIS_PER_MINUTE);
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException("Offset is too large");
    }
    return forOffsetMillis(offset);
}
 
Example 5
Source File: Nopol2017_0089_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified offset to UTC in hours and minutes.
 * This method assumes 60 minutes in an hour, and standard length minutes.
 * <p>
 * This factory is a convenient way of constructing zones with a fixed offset.
 * The minutes value is always positive and in the range 0 to 59.
 * If constructed with the values (-2, 30), the resulting zone is '-02:30'.
 * 
 * @param hoursOffset  the offset in hours from UTC
 * @param minutesOffset  the offset in minutes from UTC, must be between 0 and 59 inclusive
 * @return the DateTimeZone object for the offset
 * @throws IllegalArgumentException if the offset or minute is too large or too small
 */
public static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) throws IllegalArgumentException {
    if (hoursOffset == 0 && minutesOffset == 0) {
        return DateTimeZone.UTC;
    }
    if (minutesOffset < 0 || minutesOffset > 59) {
        throw new IllegalArgumentException("Minutes out of range: " + minutesOffset);
    }
    int offset = 0;
    try {
        int hoursInMinutes = FieldUtils.safeMultiply(hoursOffset, 60);
        if (hoursInMinutes < 0) {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, -minutesOffset);
        } else {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, minutesOffset);
        }
        offset = FieldUtils.safeMultiply(minutesOffset, DateTimeConstants.MILLIS_PER_MINUTE);
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException("Offset is too large");
    }
    return forOffsetMillis(offset);
}
 
Example 6
Source File: BasePeriod.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds the fields from another period.
 * 
 * @param values  the array of values to update
 * @param period  the period to add from, not null
 * @return the updated values
 * @throws IllegalArgumentException if an unsupported field's value is non-zero
 */
protected int[] addPeriodInto(int[] values, ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        DurationFieldType type = period.getFieldType(i);
        int value = period.getValue(i);
        if (value != 0) {
            int index = indexOf(type);
            if (index == -1) {
                throw new IllegalArgumentException(
                    "Period does not support field '" + type.getName() + "'");
            } else {
                values[index] = FieldUtils.safeAdd(getValue(index), value);
            }
        }
    }
    return values;
}
 
Example 7
Source File: Cardumen_00282_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified offset to UTC in hours and minutes.
 * This method assumes 60 minutes in an hour, and standard length minutes.
 * <p>
 * This factory is a convenient way of constructing zones with a fixed offset.
 * The minutes value is always positive and in the range 0 to 59.
 * If constructed with the values (-2, 30), the resulting zone is '-02:30'.
 * 
 * @param hoursOffset  the offset in hours from UTC
 * @param minutesOffset  the offset in minutes from UTC, must be between 0 and 59 inclusive
 * @return the DateTimeZone object for the offset
 * @throws IllegalArgumentException if the offset or minute is too large or too small
 */
public static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) throws IllegalArgumentException {
    if (hoursOffset == 0 && minutesOffset == 0) {
        return DateTimeZone.UTC;
    }
    if (minutesOffset < 0 || minutesOffset > 59) {
        throw new IllegalArgumentException("Minutes out of range: " + minutesOffset);
    }
    int offset = 0;
    try {
        int hoursInMinutes = FieldUtils.safeMultiply(hoursOffset, 60);
        if (hoursInMinutes < 0) {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, -minutesOffset);
        } else {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, minutesOffset);
        }
        offset = FieldUtils.safeMultiply(minutesOffset, DateTimeConstants.MILLIS_PER_MINUTE);
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException("Offset is too large");
    }
    return forOffsetMillis(offset);
}
 
Example 8
Source File: Cardumen_0070_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified offset to UTC in hours and minutes.
 * This method assumes 60 minutes in an hour, and standard length minutes.
 * <p>
 * This factory is a convenient way of constructing zones with a fixed offset.
 * The minutes value is always positive and in the range 0 to 59.
 * If constructed with the values (-2, 30), the resulting zone is '-02:30'.
 * 
 * @param hoursOffset  the offset in hours from UTC
 * @param minutesOffset  the offset in minutes from UTC, must be between 0 and 59 inclusive
 * @return the DateTimeZone object for the offset
 * @throws IllegalArgumentException if the offset or minute is too large or too small
 */
public static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) throws IllegalArgumentException {
    if (hoursOffset == 0 && minutesOffset == 0) {
        return DateTimeZone.UTC;
    }
    if (minutesOffset < 0 || minutesOffset > 59) {
        throw new IllegalArgumentException("Minutes out of range: " + minutesOffset);
    }
    int offset = 0;
    try {
        int hoursInMinutes = FieldUtils.safeMultiply(hoursOffset, 60);
        if (hoursInMinutes < 0) {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, -minutesOffset);
        } else {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, minutesOffset);
        }
        offset = FieldUtils.safeMultiply(minutesOffset, DateTimeConstants.MILLIS_PER_MINUTE);
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException("Offset is too large");
    }
    return forOffsetMillis(offset);
}
 
Example 9
Source File: BaseSingleFieldPeriod.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new instance representing the number of complete standard length units
 * in the specified period.
 * <p>
 * This factory method converts all fields from the period to hours using standardised
 * durations for each field. Only those fields which have a precise duration in
 * the ISO UTC chronology can be converted.
 * <ul>
 * <li>One week consists of 7 days.
 * <li>One day consists of 24 hours.
 * <li>One hour consists of 60 minutes.
 * <li>One minute consists of 60 seconds.
 * <li>One second consists of 1000 milliseconds.
 * </ul>
 * Months and Years are imprecise and periods containing these values cannot be converted.
 *
 * @param period  the period to get the number of hours from, must not be null
 * @param millisPerUnit  the number of milliseconds in one standard unit of this period
 * @throws IllegalArgumentException if the period contains imprecise duration values
 */
protected static int standardPeriodIn(ReadablePeriod period, long millisPerUnit) {
    if (period == null) {
        return 0;
    }
    Chronology iso = ISOChronology.getInstanceUTC();
    long duration = 0L;
    for (int i = 0; i < period.size(); i++) {
        int value = period.getValue(i);
        if (value != 0) {
            DurationField field = period.getFieldType(i).getField(iso);
            if (field.isPrecise() == false) {
                throw new IllegalArgumentException(
                        "Cannot convert period to duration as " + field.getName() +
                        " is not precise in the period " + period);
            }
            duration = FieldUtils.safeAdd(duration, FieldUtils.safeMultiply(field.getUnitMillis(), value));
        }
    }
    return FieldUtils.safeToInt(duration / millisPerUnit);
}
 
Example 10
Source File: Cardumen_00137_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified offset to UTC in hours and minutes.
 * This method assumes 60 minutes in an hour, and standard length minutes.
 * <p>
 * This factory is a convenient way of constructing zones with a fixed offset.
 * The minutes value is always positive and in the range 0 to 59.
 * If constructed with the values (-2, 30), the resulting zone is '-02:30'.
 * 
 * @param hoursOffset  the offset in hours from UTC
 * @param minutesOffset  the offset in minutes from UTC, must be between 0 and 59 inclusive
 * @return the DateTimeZone object for the offset
 * @throws IllegalArgumentException if the offset or minute is too large or too small
 */
public static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) throws IllegalArgumentException {
    if (hoursOffset == 0 && minutesOffset == 0) {
        return DateTimeZone.UTC;
    }
    if (minutesOffset < 0 || minutesOffset > 59) {
        throw new IllegalArgumentException("Minutes out of range: " + minutesOffset);
    }
    int offset = 0;
    try {
        int hoursInMinutes = FieldUtils.safeMultiply(hoursOffset, 60);
        if (hoursInMinutes < 0) {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, -minutesOffset);
        } else {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, minutesOffset);
        }
        offset = FieldUtils.safeMultiply(minutesOffset, DateTimeConstants.MILLIS_PER_MINUTE);
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException("Offset is too large");
    }
    return forOffsetMillis(offset);
}
 
Example 11
Source File: Cardumen_00137_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified offset to UTC in hours and minutes.
 * This method assumes 60 minutes in an hour, and standard length minutes.
 * <p>
 * This factory is a convenient way of constructing zones with a fixed offset.
 * The minutes value is always positive and in the range 0 to 59.
 * If constructed with the values (-2, 30), the resulting zone is '-02:30'.
 * 
 * @param hoursOffset  the offset in hours from UTC
 * @param minutesOffset  the offset in minutes from UTC, must be between 0 and 59 inclusive
 * @return the DateTimeZone object for the offset
 * @throws IllegalArgumentException if the offset or minute is too large or too small
 */
public static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) throws IllegalArgumentException {
    if (hoursOffset == 0 && minutesOffset == 0) {
        return DateTimeZone.UTC;
    }
    if (minutesOffset < 0 || minutesOffset > 59) {
        throw new IllegalArgumentException("Minutes out of range: " + minutesOffset);
    }
    int offset = 0;
    try {
        int hoursInMinutes = FieldUtils.safeMultiply(hoursOffset, 60);
        if (hoursInMinutes < 0) {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, -minutesOffset);
        } else {
            minutesOffset = FieldUtils.safeAdd(hoursInMinutes, minutesOffset);
        }
        offset = FieldUtils.safeMultiply(minutesOffset, DateTimeConstants.MILLIS_PER_MINUTE);
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException("Offset is too large");
    }
    return forOffsetMillis(offset);
}
 
Example 12
Source File: Time_22_BasePeriod_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Creates a period from the given start point and duration.
 *
 * @param startInstant  the interval start, null means now
 * @param duration  the duration of the interval, null means zero-length
 * @param type  which set of fields this period supports, null means standard
 */
protected BasePeriod(ReadableInstant startInstant, ReadableDuration duration, PeriodType type) {
    super();
    type = checkPeriodType(type);
    long startMillis = DateTimeUtils.getInstantMillis(startInstant);
    long durationMillis = DateTimeUtils.getDurationMillis(duration);
    long endMillis = FieldUtils.safeAdd(startMillis, durationMillis);
    Chronology chrono = DateTimeUtils.getInstantChronology(startInstant);
    iType = type;
    iValues = chrono.get(this, startMillis, endMillis);
}
 
Example 13
Source File: BasicYearDateTimeField.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public long add(long instant, int years) {
    if (years == 0) {
        return instant;
    }
    int thisYear = get(instant);
    int newYear = FieldUtils.safeAdd(thisYear, years);
    return set(instant, newYear);
}
 
Example 14
Source File: BasePeriod.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds the value of a field in this period.
 * 
 * @param values  the array of values to update
 * @param field  the field to set
 * @param value  the value to set
 * @throws IllegalArgumentException if field is is null or not supported.
 */
protected void addFieldInto(int[] values, DurationFieldType field, int value) {
    int index = indexOf(field);
    if (index == -1) {
        if (value != 0 || field == null) {
            throw new IllegalArgumentException(
                "Period does not support field '" + field + "'");
        }
    } else {
        values[index] = FieldUtils.safeAdd(values[index], value);
    }
}
 
Example 15
Source File: StringConverter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Gets the duration of the string using the standard type.
 * This matches the toString() method of ReadableDuration.
 * 
 * @param object  the String to convert, must not be null
 * @throws ClassCastException if the object is invalid
 */
public long getDurationMillis(Object object) {
    // parse here because duration could be bigger than the int supported
    // by the period parser
    String original = (String) object;
    String str = original;
    int len = str.length();
    if (len >= 4 &&
        (str.charAt(0) == 'P' || str.charAt(0) == 'p') &&
        (str.charAt(1) == 'T' || str.charAt(1) == 't') &&
        (str.charAt(len - 1) == 'S' || str.charAt(len - 1) == 's')) {
        // ok
    } else {
        throw new IllegalArgumentException("Invalid format: \"" + original + '"');
    }
    str = str.substring(2, len - 1);
    int dot = -1;
    boolean negative = false;
    for (int i = 0; i < str.length(); i++) {
        if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
            // ok
        } else if (i == 0 && str.charAt(0) == '-') {
        	// ok
        	negative = true;
        } else if (i > (negative ? 1 : 0) && str.charAt(i) == '.' && dot == -1) {
            // ok
            dot = i;
        } else {
            throw new IllegalArgumentException("Invalid format: \"" + original + '"');
        }
    }
    long millis = 0, seconds = 0;
    int firstDigit = negative ? 1 : 0;
    if (dot > 0) {
        seconds = Long.parseLong(str.substring(firstDigit, dot));
        str = str.substring(dot + 1);
        if (str.length() != 3) {
            str = (str + "000").substring(0, 3);
        }
        millis = Integer.parseInt(str);
    } else if (negative) {
    	seconds = Long.parseLong(str.substring(firstDigit, str.length()));
    } else {
        seconds = Long.parseLong(str);
    }
    if (negative) {
        return FieldUtils.safeAdd(FieldUtils.safeMultiply(-seconds, 1000), -millis);
    } else {
        return FieldUtils.safeAdd(FieldUtils.safeMultiply(seconds, 1000), millis);
    }
}
 
Example 16
Source File: StringConverter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Gets the duration of the string using the standard type.
 * This matches the toString() method of ReadableDuration.
 * 
 * @param object  the String to convert, must not be null
 * @throws ClassCastException if the object is invalid
 */
public long getDurationMillis(Object object) {
    // parse here because duration could be bigger than the int supported
    // by the period parser
    String original = (String) object;
    String str = original;
    int len = str.length();
    if (len >= 4 &&
        (str.charAt(0) == 'P' || str.charAt(0) == 'p') &&
        (str.charAt(1) == 'T' || str.charAt(1) == 't') &&
        (str.charAt(len - 1) == 'S' || str.charAt(len - 1) == 's')) {
        // ok
    } else {
        throw new IllegalArgumentException("Invalid format: \"" + original + '"');
    }
    str = str.substring(2, len - 1);
    int dot = -1;
    boolean negative = false;
    for (int i = 0; i < str.length(); i++) {
        if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
            // ok
        } else if (i == 0 && str.charAt(0) == '-') {
        	// ok
        	negative = true;
        } else if (i > (negative ? 1 : 0) && str.charAt(i) == '.' && dot == -1) {
            // ok
            dot = i;
        } else {
            throw new IllegalArgumentException("Invalid format: \"" + original + '"');
        }
    }
    long millis = 0, seconds = 0;
    int firstDigit = negative ? 1 : 0;
    if (dot > 0) {
        seconds = Long.parseLong(str.substring(firstDigit, dot));
        str = str.substring(dot + 1);
        if (str.length() != 3) {
            str = (str + "000").substring(0, 3);
        }
        millis = Integer.parseInt(str);
    } else if (negative) {
    	seconds = Long.parseLong(str.substring(firstDigit, str.length()));
    } else {
        seconds = Long.parseLong(str);
    }
    if (negative) {
        return FieldUtils.safeAdd(FieldUtils.safeMultiply(-seconds, 1000), -millis);
    } else {
        return FieldUtils.safeAdd(FieldUtils.safeMultiply(seconds, 1000), millis);
    }
}
 
Example 17
Source File: Duration.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns a new duration with this length plus that specified multiplied by the scalar.
 * This instance is immutable and is not altered.
 * <p>
 * If the addition is zero, this instance is returned.
 * 
 * @param durationToAdd  the duration to add to this one
 * @param scalar  the amount of times to add, such as -1 to subtract once
 * @return the new duration instance
 */
public Duration withDurationAdded(long durationToAdd, int scalar) {
    if (durationToAdd == 0 || scalar == 0) {
        return this;
    }
    long add = FieldUtils.safeMultiply(durationToAdd, scalar);
    long duration = FieldUtils.safeAdd(getMillis(), add);
    return new Duration(duration);
}
 
Example 18
Source File: Time_5_Period_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * Converts this period to a period in seconds assuming a
 * 7 day week, 24 hour day, 60 minute hour and 60 second minute.
 * <p>
 * This method allows you to convert between different types of period.
 * However to achieve this it makes the assumption that all
 * weeks are 7 days, all days are 24 hours, all hours are 60 minutes and
 * all minutes are 60 seconds. This is not true when daylight savings time
 * is considered, and may also not be true for some unusual chronologies.
 * However, it is included as it is a useful operation for many
 * applications and business rules.
 * <p>
 * If the period contains years or months, an exception will be thrown.
 * 
 * @return a period representing the number of standard seconds in this period
 * @throws UnsupportedOperationException if the period contains years or months
 * @throws ArithmeticException if the number of seconds is too large to be represented
 * @since 1.5
 */
public Seconds toStandardSeconds() {
    checkYearsAndMonths("Seconds");
    long seconds = getMillis() / DateTimeConstants.MILLIS_PER_SECOND;
    seconds = FieldUtils.safeAdd(seconds, getSeconds());
    seconds = FieldUtils.safeAdd(seconds, ((long) getMinutes()) * ((long) DateTimeConstants.SECONDS_PER_MINUTE));
    seconds = FieldUtils.safeAdd(seconds, ((long) getHours()) * ((long) DateTimeConstants.SECONDS_PER_HOUR));
    seconds = FieldUtils.safeAdd(seconds, ((long) getDays()) * ((long) DateTimeConstants.SECONDS_PER_DAY));
    seconds = FieldUtils.safeAdd(seconds, ((long) getWeeks()) * ((long) DateTimeConstants.SECONDS_PER_WEEK));
    return Seconds.seconds(FieldUtils.safeToInt(seconds));
}
 
Example 19
Source File: Period.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Converts this period to a period in days assuming a
 * 7 day week, 24 hour day, 60 minute hour and 60 second minute.
 * <p>
 * This method allows you to convert between different types of period.
 * However to achieve this it makes the assumption that all
 * weeks are 7 days, all days are 24 hours, all hours are 60 minutes and
 * all minutes are 60 seconds. This is not true when daylight savings time
 * is considered, and may also not be true for some unusual chronologies.
 * However, it is included as it is a useful operation for many
 * applications and business rules.
 * <p>
 * If the period contains years or months, an exception will be thrown.
 * 
 * @return a period representing the number of standard days in this period
 * @throws UnsupportedOperationException if the period contains years or months
 * @throws ArithmeticException if the number of days is too large to be represented
 * @since 1.5
 */
public Days toStandardDays() {
    checkYearsAndMonths("Days");
    long millis = getMillis();  // assign to a long
    millis += ((long) getSeconds()) * DateTimeConstants.MILLIS_PER_SECOND;
    millis += ((long) getMinutes()) * DateTimeConstants.MILLIS_PER_MINUTE;
    millis += ((long) getHours()) * DateTimeConstants.MILLIS_PER_HOUR;
    long days = millis / DateTimeConstants.MILLIS_PER_DAY;
    days = FieldUtils.safeAdd(days, getDays());
    days = FieldUtils.safeAdd(days, ((long) getWeeks()) * ((long) DateTimeConstants.DAYS_PER_WEEK));
    return Days.days(FieldUtils.safeToInt(days));
}
 
Example 20
Source File: Period.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Converts this period to a period in minutes assuming a
 * 7 day week, 24 hour day, 60 minute hour and 60 second minute.
 * <p>
 * This method allows you to convert between different types of period.
 * However to achieve this it makes the assumption that all
 * weeks are 7 days, all days are 24 hours, all hours are 60 minutes and
 * all minutes are 60 seconds. This is not true when daylight savings time
 * is considered, and may also not be true for some unusual chronologies.
 * However, it is included as it is a useful operation for many
 * applications and business rules.
 * <p>
 * If the period contains years or months, an exception will be thrown.
 * 
 * @return a period representing the number of standard minutes in this period
 * @throws UnsupportedOperationException if the period contains years or months
 * @throws ArithmeticException if the number of minutes is too large to be represented
 * @since 1.5
 */
public Minutes toStandardMinutes() {
    checkYearsAndMonths("Minutes");
    long millis = getMillis();  // assign to a long
    millis += ((long) getSeconds()) * DateTimeConstants.MILLIS_PER_SECOND;
    long minutes = millis / DateTimeConstants.MILLIS_PER_MINUTE;
    minutes = FieldUtils.safeAdd(minutes, getMinutes());
    minutes = FieldUtils.safeAdd(minutes, ((long) getHours()) * ((long) DateTimeConstants.MINUTES_PER_HOUR));
    minutes = FieldUtils.safeAdd(minutes, ((long) getDays()) * ((long) DateTimeConstants.MINUTES_PER_DAY));
    minutes = FieldUtils.safeAdd(minutes, ((long) getWeeks()) * ((long) DateTimeConstants.MINUTES_PER_WEEK));
    return Minutes.minutes(FieldUtils.safeToInt(minutes));
}