Java源码示例:org.joda.time.field.FieldUtils
示例1
/**
* 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
/**
* 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);
}
示例3
/**
* 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);
}
示例4
/**
* 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);
}
示例5
public long getDateTimeMillis(
int year, int monthOfYear, int dayOfMonth,
int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond)
throws IllegalArgumentException {
Chronology base;
if ((base = getBase()) != null) {
return base.getDateTimeMillis(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
}
FieldUtils.verifyValueBounds(DateTimeFieldType.hourOfDay(), hourOfDay, 0, 23);
FieldUtils.verifyValueBounds(DateTimeFieldType.minuteOfHour(), minuteOfHour, 0, 59);
FieldUtils.verifyValueBounds(DateTimeFieldType.secondOfMinute(), secondOfMinute, 0, 59);
FieldUtils.verifyValueBounds(DateTimeFieldType.millisOfSecond(), millisOfSecond, 0, 999);
return getDateMidnightMillis(year, monthOfYear, dayOfMonth)
+ hourOfDay * DateTimeConstants.MILLIS_PER_HOUR
+ minuteOfHour * DateTimeConstants.MILLIS_PER_MINUTE
+ secondOfMinute * DateTimeConstants.MILLIS_PER_SECOND
+ millisOfSecond;
}
示例6
/**
* Gets a copy of this Partial with the specified period added.
* <p>
* If the addition is zero, then <code>this</code> is returned.
* Fields in the period that aren't present in the partial are ignored.
* <p>
* This method is typically used to add multiple copies of complex
* period instances. Adding one field is best achieved using the method
* {@link #withFieldAdded(DurationFieldType, int)}.
*
* @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 instance with the period added
* @throws ArithmeticException if the new datetime exceeds the capacity
*/
public Partial withPeriodAdded(ReadablePeriod period, int scalar) {
if (period == null || scalar == 0) {
return this;
}
int[] newValues = getValues();
for (int i = 0; i < period.size(); i++) {
DurationFieldType fieldType = period.getFieldType(i);
int index = indexOf(fieldType);
if (index >= 0) {
newValues = getField(index).add(this, index, newValues,
FieldUtils.safeMultiply(period.getValue(i), scalar));
}
}
return new Partial(this, newValues);
}
示例7
/**
* Set the Month component of the specified time instant.<p>
* If the new month has less total days than the specified
* day of the month, this value is coerced to the nearest
* sane value. e.g.<p>
* 07-31 to month 6 = 06-30<p>
* 03-31 to month 2 = 02-28 or 02-29 depending<p>
*
* @param instant the time instant in millis to update.
* @param month the month (1,12) to update the time to.
* @return the updated time instant.
* @throws IllegalArgumentException if month is invalid
*/
public long set(long instant, int month) {
FieldUtils.verifyValueBounds(this, month, MIN, iMax);
//
int thisYear = iChronology.getYear(instant);
//
int thisDom = iChronology.getDayOfMonth(instant, thisYear);
int maxDom = iChronology.getDaysInYearMonth(thisYear, month);
if (thisDom > maxDom) {
// Quietly force DOM to nearest sane value.
thisDom = maxDom;
}
// Return newly calculated millis value
return iChronology.getYearMonthDayMillis(thisYear, month, thisDom) +
iChronology.getMillisOfDay(instant);
}
示例8
/**
* 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);
}
示例9
/**
* 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);
}
示例10
/**
* Gets a copy of this Partial with the specified period added.
* <p>
* If the addition is zero, then <code>this</code> is returned.
* Fields in the period that aren't present in the partial are ignored.
* <p>
* This method is typically used to add multiple copies of complex
* period instances. Adding one field is best achieved using the method
* {@link #withFieldAdded(DurationFieldType, int)}.
*
* @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 instance with the period added
* @throws ArithmeticException if the new datetime exceeds the capacity
*/
public Partial withPeriodAdded(ReadablePeriod period, int scalar) {
if (period == null || scalar == 0) {
return this;
}
int[] newValues = getValues();
for (int i = 0; i < period.size(); i++) {
DurationFieldType fieldType = period.getFieldType(i);
int index = indexOf(fieldType);
if (index >= 0) {
newValues = getField(index).add(this, index, newValues,
FieldUtils.safeMultiply(period.getValue(i), scalar));
}
}
return new Partial(this, newValues);
}
示例11
public long getDateTimeMillis(
int year, int monthOfYear, int dayOfMonth,
int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond)
throws IllegalArgumentException {
Chronology base;
if ((base = getBase()) != null) {
return base.getDateTimeMillis(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
}
FieldUtils.verifyValueBounds(DateTimeFieldType.hourOfDay(), hourOfDay, 0, 23);
FieldUtils.verifyValueBounds(DateTimeFieldType.minuteOfHour(), minuteOfHour, 0, 59);
FieldUtils.verifyValueBounds(DateTimeFieldType.secondOfMinute(), secondOfMinute, 0, 59);
FieldUtils.verifyValueBounds(DateTimeFieldType.millisOfSecond(), millisOfSecond, 0, 999);
return getDateMidnightMillis(year, monthOfYear, dayOfMonth)
+ hourOfDay * DateTimeConstants.MILLIS_PER_HOUR
+ minuteOfHour * DateTimeConstants.MILLIS_PER_MINUTE
+ secondOfMinute * DateTimeConstants.MILLIS_PER_SECOND
+ millisOfSecond;
}
示例12
/**
* Gets a copy of this Partial with the specified period added.
* <p>
* If the addition is zero, then <code>this</code> is returned.
* Fields in the period that aren't present in the partial are ignored.
* <p>
* This method is typically used to add multiple copies of complex
* period instances. Adding one field is best achieved using the method
* {@link #withFieldAdded(DurationFieldType, int)}.
*
* @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 instance with the period added
* @throws ArithmeticException if the new datetime exceeds the capacity
*/
public Partial withPeriodAdded(ReadablePeriod period, int scalar) {
if (period == null || scalar == 0) {
return this;
}
int[] newValues = getValues();
for (int i = 0; i < period.size(); i++) {
DurationFieldType fieldType = period.getFieldType(i);
int index = indexOf(fieldType);
if (index >= 0) {
newValues = getField(index).add(this, index, newValues,
FieldUtils.safeMultiply(period.getValue(i), scalar));
}
}
return new Partial(this, newValues);
}
示例13
/**
* 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;
}
示例14
/**
* 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);
}
示例15
public long addWrapField(long instant, int years) {
if (years == 0) {
return instant;
}
// Return newly calculated millis value
int thisYear = iChronology.getYear(instant);
int wrappedYear = FieldUtils.getWrappedValue
(thisYear, years, iChronology.getMinYear(), iChronology.getMaxYear());
return set(instant, wrappedYear);
}
示例16
/**
* Constructs an interval from a millisecond duration and an end instant.
*
* @param duration the duration of this interval, null means zero length
* @param end end of this interval, null means now
* @throws IllegalArgumentException if the end is before the start
* @throws ArithmeticException if the start instant exceeds the capacity of a long
*/
protected BaseInterval(ReadableDuration duration, ReadableInstant end) {
super();
iChronology = DateTimeUtils.getInstantChronology(end);
iEndMillis = DateTimeUtils.getInstantMillis(end);
long durationMillis = DateTimeUtils.getDurationMillis(duration);
iStartMillis = FieldUtils.safeAdd(iEndMillis, -durationMillis);
checkInterval(iStartMillis, iEndMillis);
}
示例17
/**
* 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());
}
示例18
/**
* A limit chronology is only equal to a limit chronology with the
* same base chronology and limits.
*
* @param obj the object to compare to
* @return true if equal
* @since 1.4
*/
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof LimitChronology == false) {
return false;
}
LimitChronology chrono = (LimitChronology) obj;
return
getBase().equals(chrono.getBase()) &&
FieldUtils.equals(getLowerLimit(), chrono.getLowerLimit()) &&
FieldUtils.equals(getUpperLimit(), chrono.getUpperLimit());
}
示例19
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);
}
示例20
/**
* Compares this object with the specified object for equality based
* on start and end millis plus the chronology.
* All ReadableInterval instances are accepted.
* <p>
* To compare the duration of two time intervals, use {@link #toDuration()}
* to get the durations and compare those.
*
* @param readableInterval a readable interval to check against
* @return true if the intervals are equal comparing the start millis,
* end millis and chronology
*/
public boolean equals(Object readableInterval) {
if (this == readableInterval) {
return true;
}
if (readableInterval instanceof ReadableInterval == false) {
return false;
}
ReadableInterval other = (ReadableInterval) readableInterval;
return
getStartMillis() == other.getStartMillis() &&
getEndMillis() == other.getEndMillis() &&
FieldUtils.equals(getChronology(), other.getChronology());
}
示例21
/**
* A limit chronology is only equal to a limit chronology with the
* same base chronology and limits.
*
* @param obj the object to compare to
* @return true if equal
* @since 1.4
*/
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof LimitChronology == false) {
return false;
}
LimitChronology chrono = (LimitChronology) obj;
return
getBase().equals(chrono.getBase()) &&
FieldUtils.equals(getLowerLimit(), chrono.getLowerLimit()) &&
FieldUtils.equals(getUpperLimit(), chrono.getUpperLimit());
}
示例22
/**
* Compares this object with the specified object for equality based
* on start and end millis plus the chronology.
* All ReadableInterval instances are accepted.
* <p>
* To compare the duration of two time intervals, use {@link #toDuration()}
* to get the durations and compare those.
*
* @param readableInterval a readable interval to check against
* @return true if the start and end millis are equal
*/
public boolean equals(Object readableInterval) {
if (this == readableInterval) {
return true;
}
if (readableInterval instanceof ReadableInterval == false) {
return false;
}
ReadableInterval other = (ReadableInterval) readableInterval;
return
getStartMillis() == other.getStartMillis() &&
getEndMillis() == other.getEndMillis() &&
FieldUtils.equals(getChronology(), other.getChronology());
}
示例23
/**
* 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);
}
示例24
/**
* Creates a period from the given duration and end point.
*
* @param duration the duration of the interval, null means zero-length
* @param endInstant the interval end, null means now
* @param type which set of fields this period supports, null means standard
*/
protected BasePeriod(ReadableDuration duration, ReadableInstant endInstant, PeriodType type) {
super();
type = checkPeriodType(type);
long durationMillis = DateTimeUtils.getDurationMillis(duration);
long endMillis = DateTimeUtils.getInstantMillis(endInstant);
long startMillis = FieldUtils.safeSubtract(endMillis, durationMillis);
Chronology chrono = DateTimeUtils.getInstantChronology(endInstant);
iType = type;
iValues = chrono.get(this, startMillis, endMillis);
}
示例25
public long addWrapField(long instant, int years) {
if (years == 0) {
return instant;
}
// Return newly calculated millis value
int thisYear = iChronology.getYear(instant);
int wrappedYear = FieldUtils.getWrappedValue
(thisYear, years, iChronology.getMinYear(), iChronology.getMaxYear());
return set(instant, wrappedYear);
}
示例26
/**
* 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());
}
示例27
@Override
public long getMillis(long value, long instant) {
return getMillis(FieldUtils.safeToInt(value), instant);
}
示例28
/**
* Returns a copy of this month-day with the specified period added.
* <p>
* If the addition is zero, then <code>this</code> is returned.
* Fields in the period that aren't present in the partial are ignored.
* <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 #plusMonths(int)}.
*
* @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 instance with the period added, never null
* @throws ArithmeticException if the new date-time exceeds the capacity
*/
public MonthDay withPeriodAdded(ReadablePeriod period, int scalar) {
if (period == null || scalar == 0) {
return this;
}
int[] newValues = getValues();
for (int i = 0; i < period.size(); i++) {
DurationFieldType fieldType = period.getFieldType(i);
int index = indexOf(fieldType);
if (index >= 0) {
newValues = getField(index).add(this, index, newValues,
FieldUtils.safeMultiply(period.getValue(i), scalar));
}
}
return new MonthDay(this, newValues);
}
示例29
public long set(long instant, int year) {
FieldUtils.verifyValueBounds
(this, year, iChronology.getMinYear(), iChronology.getMaxYear());
return iChronology.setYear(instant, year);
}
示例30
/**
* 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);
}
}