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

The following examples show how to use org.joda.time.field.FieldUtils#safeMultiply() . 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_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 2
Source File: Time_23_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: Time_25_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 4
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 5
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 6
Source File: Cardumen_0070_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 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_0074_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, from -23 to +23
 * @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: Time_9_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, from -23 to +23
 * @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 (hoursOffset < -23 || hoursOffset > 23) {
        throw new IllegalArgumentException("Hours out of range: " + hoursOffset);
    }
    if (minutesOffset < 0 || minutesOffset > 59) {
        throw new IllegalArgumentException("Minutes out of range: " + minutesOffset);
    }
    int offset = 0;
    try {
        int hoursInMinutes = hoursOffset * 60;
        if (hoursInMinutes < 0) {
            minutesOffset = hoursInMinutes - minutesOffset;
        } else {
            minutesOffset = hoursInMinutes + minutesOffset;
        }
        offset = FieldUtils.safeMultiply(minutesOffset, DateTimeConstants.MILLIS_PER_MINUTE);
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException("Offset is too large");
    }
    return forOffsetMillis(offset);
}
 
Example 10
Source File: Time_5_Period_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns a new instance with each element in this period multiplied
 * by the specified scalar.
 *
 * @param scalar  the scalar to multiply by, not null
 * @return a {@code Period} based on this period with the amounts multiplied by the scalar, never null
 * @throws ArithmeticException if the capacity of any field is exceeded
 * @since 2.1
 */
public Period multipliedBy(int scalar) {
    if (this == ZERO || scalar == 1) {
        return this;
    }
    int[] values = getValues();  // cloned
    for (int i = 0; i < values.length; i++) {
        values[i] = FieldUtils.safeMultiply(values[i], scalar);
    }
    return new Period(values, getPeriodType());
}
 
Example 11
Source File: LocalDate.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this date with the specified period added.
 * <p>
 * If the addition is zero, then <code>this</code> is returned.
 * <p>
 * This method is typically used to add multiple copies of complex
 * period instances. Adding one field is best achieved using methods
 * like {@link #withFieldAdded(DurationFieldType, int)}
 * or {@link #plusYears(int)}.
 * <p>
 * Unsupported time fields are ignored, thus adding a period of 24 hours
 * will not have any effect.
 *
 * @param period  the period to add to this one, null means zero
 * @param scalar  the amount of times to add, such as -1 to subtract once
 * @return a copy of this date with the period added
 * @throws ArithmeticException if the result exceeds the internal capacity
 */
public LocalDate withPeriodAdded(ReadablePeriod period, int scalar) {
    if (period == null || scalar == 0) {
        return this;
    }
    long instant = getLocalMillis();
    Chronology chrono = getChronology();
    for (int i = 0; i < period.size(); i++) {
        long value = FieldUtils.safeMultiply(period.getValue(i), scalar);
        DurationFieldType type = period.getFieldType(i);
        if (isSupported(type)) {
            instant = type.getField(chrono).add(instant, value);
        }
    }
    return withLocalMillis(instant);
}
 
Example 12
Source File: LocalDate.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this date with the specified period added.
 * <p>
 * If the addition is zero, then <code>this</code> is returned.
 * <p>
 * This method is typically used to add multiple copies of complex
 * period instances. Adding one field is best achieved using methods
 * like {@link #withFieldAdded(DurationFieldType, int)}
 * or {@link #plusYears(int)}.
 * <p>
 * Unsupported time fields are ignored, thus adding a period of 24 hours
 * will not have any effect.
 *
 * @param period  the period to add to this one, null means zero
 * @param scalar  the amount of times to add, such as -1 to subtract once
 * @return a copy of this date with the period added
 * @throws ArithmeticException if the result exceeds the internal capacity
 */
public LocalDate withPeriodAdded(ReadablePeriod period, int scalar) {
    if (period == null || scalar == 0) {
        return this;
    }
    long instant = getLocalMillis();
    Chronology chrono = getChronology();
    for (int i = 0; i < period.size(); i++) {
        long value = FieldUtils.safeMultiply(period.getValue(i), scalar);
        DurationFieldType type = period.getFieldType(i);
        if (isSupported(type)) {
            instant = type.getField(chrono).add(instant, value);
        }
    }
    return withLocalMillis(instant);
}
 
Example 13
Source File: Time_8_DateTimeZone_s.java    From coming with MIT License 4 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 hours value must be in the range -23 to +23.
 * The minutes value must be in the range -59 to +59.
 * The following combinations of sign for the hour and minute are possible:
 * <pre>
 *  Hour    Minute    Example    Result
 * 
 *  +ve     +ve       (2, 15)    +02:15
 *  +ve     zero      (2, 0)     +02:00
 *  +ve     -ve       (2, -15)   IllegalArgumentException
 * 
 *  zero    +ve       (0, 15)    +00:15
 *  zero    zero      (0, 0)     +00:00
 *  zero    -ve       (0, -15)   -00:15
 * 
 *  -ve     +ve       (-2, 15)   -02:15
 *  -ve     zero      (-2, 0)    -02:00
 *  -ve     -ve       (-2, -15)  -02:15
 * </pre>
 * Note that in versions before 2.3, the minutes had to be zero or positive.
 * 
 * @param hoursOffset  the offset in hours from UTC, from -23 to +23
 * @param minutesOffset  the offset in minutes from UTC, from -59 to +59
 * @return the DateTimeZone object for the offset
 * @throws IllegalArgumentException if any value is out of range, the minutes are negative
 *  when the hours are positive, or the resulting offset exceeds +/- 23:59:59.000
 */
public static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) throws IllegalArgumentException {
    if (hoursOffset == 0 && minutesOffset == 0) {
        return DateTimeZone.UTC;
    }
    if (hoursOffset < -23 || hoursOffset > 23) {
        throw new IllegalArgumentException("Hours out of range: " + hoursOffset);
    }
    if (minutesOffset < 0 || minutesOffset > 59) {
        throw new IllegalArgumentException("Minutes out of range: " + minutesOffset);
    }
    int offset = 0;
    try {
        int hoursInMinutes = hoursOffset * 60;
        if (hoursInMinutes < 0) {
            minutesOffset = hoursInMinutes - minutesOffset;
        } else {
            minutesOffset = hoursInMinutes + minutesOffset;
        }
        offset = FieldUtils.safeMultiply(minutesOffset, DateTimeConstants.MILLIS_PER_MINUTE);
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException("Offset is too large");
    }
    return forOffsetMillis(offset);
}
 
Example 14
Source File: Duration.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a duration with the specified number of days assuming that
 * there are the standard number of milliseconds in a day.
 * <p>
 * This method assumes that there are 24 hours in a day,
 * 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in
 * a second. This will be true for most days, however days with Daylight
 * Savings changes will not have 24 hours, so use this method with care.
 * <p>
 * A Duration is a representation of an amount of time. If you want to express
 * the concepts of 'days' you should consider using the {@link Days} class.
 *
 * @param days  the number of standard days in this duration
 * @return the duration, never null
 * @throws ArithmeticException if the days value is too large
 * @since 1.6
 */
public static Duration standardDays(long days) {
    if (days == 0) {
        return ZERO;
    }
    return new Duration(FieldUtils.safeMultiply(days, DateTimeConstants.MILLIS_PER_DAY));
}
 
Example 15
Source File: Duration.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a duration with the specified number of minutes assuming that
 * there are the standard number of milliseconds in a minute.
 * <p>
 * This method assumes that there are 60 seconds in a minute and
 * 1000 milliseconds in a second.
 * All currently supplied chronologies use this definition.
 * <p>
 * A Duration is a representation of an amount of time. If you want to express
 * the concept of 'minutes' you should consider using the {@link Minutes} class.
 *
 * @param minutes  the number of standard minutes in this duration
 * @return the duration, never null
 * @throws ArithmeticException if the minutes value is too large
 * @since 1.6
 */
public static Duration standardMinutes(long minutes) {
    if (minutes == 0) {
        return ZERO;
    }
    return new Duration(FieldUtils.safeMultiply(minutes, DateTimeConstants.MILLIS_PER_MINUTE));
}
 
Example 16
Source File: Duration.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a duration with the specified number of days assuming that
 * there are the standard number of milliseconds in a day.
 * <p>
 * This method assumes that there are 24 hours in a day,
 * 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in
 * a second. This will be true for most days, however days with Daylight
 * Savings changes will not have 24 hours, so use this method with care.
 * <p>
 * A Duration is a representation of an amount of time. If you want to express
 * the concept of 'days' you should consider using the {@link Days} class.
 *
 * @param days  the number of standard days in this duration
 * @return the duration, never null
 * @throws ArithmeticException if the days value is too large
 * @since 1.6
 */
public static Duration standardDays(long days) {
    if (days == 0) {
        return ZERO;
    }
    return new Duration(FieldUtils.safeMultiply(days, DateTimeConstants.MILLIS_PER_DAY));
}
 
Example 17
Source File: DateTimeZone.java    From astor with GNU General Public License v2.0 3 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 hours value must be in the range -23 to +23.
 * The minutes value must be in the range -59 to +59.
 * The following combinations of sign for the hour and minute are possible:
 * <pre>
 *  Hour    Minute    Example    Result
 * 
 *  +ve     +ve       (2, 15)    +02:15
 *  +ve     zero      (2, 0)     +02:00
 *  +ve     -ve       (2, -15)   IllegalArgumentException
 * 
 *  zero    +ve       (0, 15)    +00:15
 *  zero    zero      (0, 0)     +00:00
 *  zero    -ve       (0, -15)   -00:15
 * 
 *  -ve     +ve       (-2, 15)   -02:15
 *  -ve     zero      (-2, 0)    -02:00
 *  -ve     -ve       (-2, -15)  -02:15
 * </pre>
 * Note that in versions before 2.3, the minutes had to be zero or positive.
 * 
 * @param hoursOffset  the offset in hours from UTC, from -23 to +23
 * @param minutesOffset  the offset in minutes from UTC, from -59 to +59
 * @return the DateTimeZone object for the offset
 * @throws IllegalArgumentException if any value is out of range, the minutes are negative
 *  when the hours are positive, or the resulting offset exceeds +/- 23:59:59.000
 */
public static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) throws IllegalArgumentException {
    if (hoursOffset == 0 && minutesOffset == 0) {
        return DateTimeZone.UTC;
    }
    if (hoursOffset < -23 || hoursOffset > 23) {
        throw new IllegalArgumentException("Hours out of range: " + hoursOffset);
    }
    if (minutesOffset < -59 || minutesOffset > 59) {
        throw new IllegalArgumentException("Minutes out of range: " + minutesOffset);
    }
    if (hoursOffset > 0 && minutesOffset < 0) {
        throw new IllegalArgumentException("Positive hours must not have negative minutes: " + minutesOffset);
    }
    int offset = 0;
    try {
        int hoursInMinutes = hoursOffset * 60;
        if (hoursInMinutes < 0) {
            minutesOffset = hoursInMinutes - Math.abs(minutesOffset);
        } else {
            minutesOffset = hoursInMinutes + minutesOffset;
        }
        offset = FieldUtils.safeMultiply(minutesOffset, DateTimeConstants.MILLIS_PER_MINUTE);
    } catch (ArithmeticException ex) {
        throw new IllegalArgumentException("Offset is too large");
    }
    return forOffsetMillis(offset);
}
 
Example 18
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 19
Source File: Duration.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a duration with the specified number of hours assuming that
 * there are the standard number of milliseconds in an hour.
 * <p>
 * This method assumes that there are 60 minutes in an hour,
 * 60 seconds in a minute and 1000 milliseconds in a second.
 * All currently supplied chronologies use this definition.
 * <p>
 * A Duration is a representation of an amount of time. If you want to express
 * the concepts of 'hours' you should consider using the {@link Hours} class.
 *
 * @param hours  the number of standard hours in this duration
 * @return the duration, never null
 * @throws ArithmeticException if the hours value is too large
 * @since 1.6
 */
public static Duration standardHours(long hours) {
    if (hours == 0) {
        return ZERO;
    }
    return new Duration(FieldUtils.safeMultiply(hours, DateTimeConstants.MILLIS_PER_HOUR));
}
 
Example 20
Source File: Duration.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a duration with the specified number of seconds assuming that
 * there are the standard number of milliseconds in a second.
 * <p>
 * This method assumes that there are 1000 milliseconds in a second.
 * All currently supplied chronologies use this definition.
 * <p>
 * A Duration is a representation of an amount of time. If you want to express
 * the concepts of 'seconds' you should consider using the {@link Seconds} class.
 *
 * @param seconds  the number of standard seconds in this duration
 * @return the duration, never null
 * @throws ArithmeticException if the seconds value is too large
 * @since 1.6
 */
public static Duration standardSeconds(long seconds) {
    if (seconds == 0) {
        return ZERO;
    }
    return new Duration(FieldUtils.safeMultiply(seconds, DateTimeConstants.MILLIS_PER_SECOND));
}