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;
 
Precise datetime field, composed of two precise duration fields.

This DateTimeField is useful for defining DateTimeFields that are composed of precise durations, like time of day fields. If either duration field is imprecise, then an ImpreciseDateTimeField may be used instead.

PreciseDateTimeField is thread-safe and immutable.

Author(s):
Brian S O'Neill
Stephen Colebourne
Since:
1.0
See also:
ImpreciseDateTimeField
 
 
     private static final long serialVersionUID = -5586801265774496376L;

    
The maximum range in the correct units
 
     private final int iRange;
 
     private final DurationField iRangeField;

    
Constructor.

Parameters:
type the field type this field uses
unit precise unit duration, like "seconds()".
range precise range duration, preferably a multiple of the unit, like "minutes()".
Throws:
java.lang.IllegalArgumentException if either duration field is imprecise
java.lang.IllegalArgumentException if unit milliseconds is less than one or effective value range is less than two.
 
     public PreciseDateTimeField(DateTimeFieldType type,
                                 DurationField unitDurationField range) {
         super(typeunit);
 
         if (!range.isPrecise()) {
             throw new IllegalArgumentException("Range duration field must be precise");
         }
 
         long rangeMillis = range.getUnitMillis();
          = (int)(rangeMillis / getUnitMillis());
         if ( < 2) {
             throw new IllegalArgumentException("The effective range must be at least 2");
         }
 
          = range;
     }

    
Get the amount of fractional units from the specified time instant.

Parameters:
instant the milliseconds from 1970-01-01T00:00:00Z to query
Returns:
the amount of fractional units extracted from the input.
 
     public int get(long instant) {
         if (instant >= 0) {
             return (int) ((instant / getUnitMillis()) % );
         } else {
             return  - 1 + (int) (((instant + 1) / getUnitMillis()) % );
         }
     }

    
Add to the component of the specified time instant, wrapping around within that component if necessary.

Parameters:
instant the milliseconds from 1970-01-01T00:00:00Z to add to
amount the amount of units to add (can be negative).
Returns:
the updated time instant.
 
     public long addWrapField(long instantint amount) {
         int thisValue = get(instant);
         int wrappedValue = FieldUtils.getWrappedValue
             (thisValueamountgetMinimumValue(), getMaximumValue());
         // copy code from set() to avoid repeat call to get()
         return instant + (wrappedValue - thisValue) * getUnitMillis();
    }

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

Parameters:
instant the milliseconds from 1970-01-01T00:00:00Z to set in
value value of 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(thisvaluegetMinimumValue(), getMaximumValue());
        return instant + (value - get(instant)) * ;
    }

    
Returns the range duration of this field. For example, if this field represents "minute of hour", then the range duration field is an hours.

Returns:
the range duration of this field, or null if field has no range
        return ;
    }

    
Get the maximum value for the field.

Returns:
the maximum value
    public int getMaximumValue() {
        return  - 1;
    }
    
    
Returns the range of the field in the field's units.

For example, 60 for seconds per minute. The field is allowed values from 0 to range - 1.

Returns:
unit range
    public int getRange() {
        return ;
    }
New to GrepCode? Check out our FAQ X