Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2005 Stephen Colebourne
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.joda.time.field;
 
Counterpart remainder datetime field to DividedDateTimeField. The field's unit duration is unchanged, but the range duration is scaled accordingly.

RemainderDateTimeField is thread-safe and immutable.

Author(s):
Brian S O'Neill
Since:
1.0
See also:
DividedDateTimeField
 
 public class RemainderDateTimeField extends DecoratedDateTimeField {
 
     private static final long serialVersionUID = 5708241235177666790L;
 
     // Shared with DividedDateTimeField.
     final int iDivisor;
     final DurationField iRangeField;

    
Constructor.

Parameters:
field the field to wrap, like "year()".
type the field type this field actually uses
divisor divisor, such as 100 years in a century
Throws:
java.lang.IllegalArgumentException if divisor is less than two
 
     public RemainderDateTimeField(DateTimeField field,
                                   DateTimeFieldType typeint divisor) {
         super(fieldtype);
 
         if (divisor < 2) {
             throw new IllegalArgumentException("The divisor must be at least 2");
         }
 
         DurationField rangeField = field.getDurationField();
         if (rangeField == null) {
              = null;
         } else {
              = new ScaledDurationField(
                 rangeFieldtype.getRangeDurationType(), divisor);
         }
 
          = divisor;
     }

    
Construct a RemainderDateTimeField that compliments the given DividedDateTimeField.

Parameters:
dividedField complimentary divided field, like "century()".
 
     public RemainderDateTimeField(DividedDateTimeField dividedField) {
         this(dividedFielddividedField.getType());
     }

    
Construct a RemainderDateTimeField that compliments the given DividedDateTimeField.

Parameters:
dividedField complimentary divided field, like "century()".
type the field type this field actually uses
 
     public RemainderDateTimeField(DividedDateTimeField dividedFieldDateTimeFieldType type) {
         super(dividedField.getWrappedField(), type);
          = dividedField.iDivisor;
          = dividedField.iDurationField;
     }
 
     //-----------------------------------------------------------------------
     
Get the remainder from the specified time instant.

Parameters:
instant the time instant in millis to query.
Returns:
the remainder extracted from the input.
 
     public int get(long instant) {
        int value = getWrappedField().get(instant);
        if (value >= 0) {
            return value % ;
        } else {
            return ( - 1) + ((value + 1) % );
        }
    }

    
Add the specified amount to the specified time instant, wrapping around within the remainder range if necessary. The amount added may be negative.

Parameters:
instant the time instant in millis to update.
amount the amount to add (can be negative).
Returns:
the updated time instant.
    public long addWrapField(long instantint amount) {
        return set(instant, FieldUtils.getWrappedValue(get(instant), amount, 0,  - 1));
    }

    
Set the specified amount of remainder units to the specified time instant.

Parameters:
instant the time instant in millis to update.
value value of remainder units to set.
Returns:
the updated time instant.
Throws:
java.lang.IllegalArgumentException if value is too large or too small.
    public long set(long instantint value) {
        FieldUtils.verifyValueBounds(thisvalue, 0,  - 1);
        int divided = getDivided(getWrappedField().get(instant));
        return getWrappedField().set(instantdivided *  + value);
    }

    
Returns a scaled version of the wrapped field's unit duration field.
        return ;
    }

    
Get the minimum value for the field, which is always zero.

Returns:
the minimum value of zero.
    public int getMinimumValue() {
        return 0;
    }

    
Get the maximum value for the field, which is always one less than the divisor.

Returns:
the maximum value
    public int getMaximumValue() {
        return  - 1;
    }
    public long roundFloor(long instant) {
        return getWrappedField().roundFloor(instant);
    }
    public long roundCeiling(long instant) {
        return getWrappedField().roundCeiling(instant);
    }
    public long roundHalfFloor(long instant) {
        return getWrappedField().roundHalfFloor(instant);
    }
    public long roundHalfCeiling(long instant) {
        return getWrappedField().roundHalfCeiling(instant);
    }
    public long roundHalfEven(long instant) {
        return getWrappedField().roundHalfEven(instant);
    }
    public long remainder(long instant) {
        return getWrappedField().remainder(instant);
    }

    
Returns the divisor applied, in the field's units.

Returns:
the divisor
    public int getDivisor() {
        return ;
    }
    private int getDivided(int value) {
        if (value >= 0) {
            return value / ;
        } else {
            return ((value + 1) / ) - 1;
        }
    }
New to GrepCode? Check out our FAQ X