本文整理汇总了Java中org.joda.time.field.FieldUtils.safeAdd方法的典型用法代码示例。如果您正苦于以下问题:Java FieldUtils.safeAdd方法的具体用法?Java FieldUtils.safeAdd怎么用?Java FieldUtils.safeAdd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.joda.time.field.FieldUtils
的用法示例。
在下文中一共展示了FieldUtils.safeAdd方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: forOffsetHoursMinutes
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* 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);
}
示例2: standardPeriodIn
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* 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);
}
示例3: addPeriodInto
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* 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;
}
示例4: BaseInterval
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* Constructs an interval from a start instant and a duration.
*
* @param start start of this interval, null means now
* @param duration the duration of this interval, null means zero length
* @throws IllegalArgumentException if the end is before the start
* @throws ArithmeticException if the end instant exceeds the capacity of a long
*/
protected BaseInterval(ReadableInstant start, ReadableDuration duration) {
super();
iChronology = DateTimeUtils.getInstantChronology(start);
iStartMillis = DateTimeUtils.getInstantMillis(start);
long durationMillis = DateTimeUtils.getDurationMillis(duration);
iEndMillis = FieldUtils.safeAdd(iStartMillis, durationMillis);
checkInterval(iStartMillis, iEndMillis);
}
示例5: BasePeriod
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* 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);
}
示例6: addFieldInto
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* 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);
}
}
示例7: BaseDuration
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* Creates a duration from the given interval endpoints.
*
* @param start interval start, null means now
* @param end interval end, null means now
* @throws ArithmeticException if the duration exceeds a 64 bit long
*/
protected BaseDuration(ReadableInstant start, ReadableInstant end) {
super();
if (start == end) {
iMillis = 0L;
} else {
long startMillis = DateTimeUtils.getInstantMillis(start);
long endMillis = DateTimeUtils.getInstantMillis(end);
iMillis = FieldUtils.safeAdd(endMillis, -startMillis);
}
}
示例8: addIndexedField
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* Adds to the indexed field part of the period.
*
* @param period the period to query
* @param index the index to use
* @param values the array to populate
* @param valueToAdd the value to add
* @return true if the array is updated
* @throws UnsupportedOperationException if not supported
*/
boolean addIndexedField(ReadablePeriod period, int index, int[] values, int valueToAdd) {
if (valueToAdd == 0) {
return false;
}
int realIndex = iIndices[index];
if (realIndex == -1) {
throw new UnsupportedOperationException("Field is not supported");
}
values[realIndex] = FieldUtils.safeAdd(values[realIndex], valueToAdd);
return true;
}
示例9: add
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
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);
}
示例10: normalizedStandard
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* Normalizes this period using standard rules, assuming a 12 month year,
* 7 day week, 24 hour day, 60 minute hour and 60 second minute,
* providing control over how the result is split into fields.
* <p>
* This method allows you to normalize a period.
* However to achieve this it makes the assumption that all years are
* 12 months, 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 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, then the months will be
* normalized to be between 0 and 11. The days field and below will be
* normalized as necessary, however this will not overflow into the months
* field. Thus a period of 1 year 15 months will normalize to 2 years 3 months.
* But a period of 1 month 40 days will remain as 1 month 40 days.
* <p>
* The PeriodType parameter controls how the result is created. It allows
* you to omit certain fields from the result if desired. For example,
* you may not want the result to include weeks, in which case you pass
* in <code>PeriodType.yearMonthDayTime()</code>.
*
* @param type the period type of the new period, null means standard type
* @return a normalized period equivalent to this period
* @throws ArithmeticException if any field is too large to be represented
* @throws UnsupportedOperationException if this period contains non-zero
* years or months but the specified period type does not support them
* @since 1.5
*/
public Period normalizedStandard(PeriodType type) {
long millis = getMillis(); // no overflow can happen, even with Integer.MAX_VALUEs
millis += (((long) getSeconds()) * ((long) DateTimeConstants.MILLIS_PER_SECOND));
millis += (((long) getMinutes()) * ((long) DateTimeConstants.MILLIS_PER_MINUTE));
millis += (((long) getHours()) * ((long) DateTimeConstants.MILLIS_PER_HOUR));
millis += (((long) getDays()) * ((long) DateTimeConstants.MILLIS_PER_DAY));
millis += (((long) getWeeks()) * ((long) DateTimeConstants.MILLIS_PER_WEEK));
Period result = new Period(millis, DateTimeUtils.getPeriodType(type), ISOChronology.getInstanceUTC());
int years = getYears();
int months = getMonths();
if (years != 0 || months != 0) {
years = FieldUtils.safeAdd(years, months / 12);
months = months % 12;
if (years != 0) {
result = result.withYears(years);
}
if (months != 0) {
result = result.withMonths(months);
}
}
return result;
}
示例11: withDurationAdded
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* 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);
}
示例12: getDurationMillis
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* 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;
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) >= '0' && str.charAt(i) <= '9') ||
(i == 0 && str.charAt(0) == '-')) {
// ok
} else if (i > 0 && str.charAt(i) == '.' && dot == -1) {
// ok
dot = i;
} else {
throw new IllegalArgumentException("Invalid format: \"" + original + '"');
}
}
long millis = 0, seconds = 0;
if (dot > 0) {
seconds = Long.parseLong(str.substring(0, dot));
str = str.substring(dot + 1);
if (str.length() != 3) {
str = (str + "000").substring(0, 3);
}
millis = Integer.parseInt(str);
} else {
seconds = Long.parseLong(str);
}
if (seconds < 0) {
return FieldUtils.safeAdd(FieldUtils.safeMultiply(seconds, 1000), -millis);
} else {
return FieldUtils.safeAdd(FieldUtils.safeMultiply(seconds, 1000), millis);
}
}
示例13: toStandardDays
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* 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));
}
示例14: toStandardHours
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* Converts this period to a period in hours 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 hours in this period
* @throws UnsupportedOperationException if the period contains years or months
* @throws ArithmeticException if the number of hours is too large to be represented
* @since 1.5
*/
public Hours toStandardHours() {
checkYearsAndMonths("Hours");
long millis = getMillis(); // assign to a long
millis += ((long) getSeconds()) * DateTimeConstants.MILLIS_PER_SECOND;
millis += ((long) getMinutes()) * DateTimeConstants.MILLIS_PER_MINUTE;
long hours = millis / DateTimeConstants.MILLIS_PER_HOUR;
hours = FieldUtils.safeAdd(hours, getHours());
hours = FieldUtils.safeAdd(hours, ((long) getDays()) * ((long) DateTimeConstants.HOURS_PER_DAY));
hours = FieldUtils.safeAdd(hours, ((long) getWeeks()) * ((long) DateTimeConstants.HOURS_PER_WEEK));
return Hours.hours(FieldUtils.safeToInt(hours));
}
示例15: toStandardMinutes
import org.joda.time.field.FieldUtils; //导入方法依赖的package包/类
/**
* 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));
}