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.base;
 
 
BaseDuration is an abstract implementation of ReadableDuration that stores data in a long duration milliseconds field.

This class should generally not be used directly by API users. The org.joda.time.ReadableDuration interface should be used when different kinds of duration objects are to be referenced.

BaseDuration subclasses may be mutable and not thread-safe.

Author(s):
Brian S O'Neill
Stephen Colebourne
Since:
1.0
 
 public abstract class BaseDuration
         extends AbstractDuration
         implements ReadableDurationSerializable {

    
Serialization version
 
     private static final long serialVersionUID = 2581698638990L;

    
The duration length
 
     private long iMillis;

    
Creates a duration from the given millisecond duration.

Parameters:
duration the duration, in milliseconds
 
     protected BaseDuration(long duration) {
         super();
          = duration;
     }

    
Creates a duration from the given interval endpoints.

Parameters:
startInstant interval start, in milliseconds
endInstant interval end, in milliseconds
Throws:
java.lang.ArithmeticException if the duration exceeds a 64 bit long
 
     protected BaseDuration(long startInstantlong endInstant) {
         super();
          = FieldUtils.safeAdd(endInstant, -startInstant);
     }

    
Creates a duration from the given interval endpoints.

Parameters:
start interval start, null means now
end interval end, null means now
Throws:
java.lang.ArithmeticException if the duration exceeds a 64 bit long
 
     protected BaseDuration(ReadableInstant startReadableInstant end) {
         super();
         if (start == end) {
              = 0L;
         } else {
             long startMillis = DateTimeUtils.getInstantMillis(start);
             long endMillis = DateTimeUtils.getInstantMillis(end);
              = FieldUtils.safeAdd(endMillis, -startMillis);
         }
     }

    
Creates a duration from the specified object using the ConverterManager.

Parameters:
duration duration to convert
Throws:
java.lang.IllegalArgumentException if duration is invalid
    protected BaseDuration(Object duration) {
        super();
        DurationConverter converter = ConverterManager.getInstance().getDurationConverter(duration);
         = converter.getDurationMillis(duration);
    }
    //-----------------------------------------------------------------------
    
Gets the length of this duration in milliseconds.

Returns:
the length of the duration in milliseconds.
    public long getMillis() {
        return ;
    }
    //-----------------------------------------------------------------------
    
Sets the length of this duration in milliseconds.

Parameters:
duration the new length of the duration
    protected void setMillis(long duration) {
         = duration;
    }
    //-----------------------------------------------------------------------
    
Converts this duration to a Period instance using the specified period type and the ISO chronology.

Only precise fields in the period type will be used. At most these are hours, minutes, seconds and millis - the period type may restrict the selection further.

For more control over the conversion process, you must pair the duration with an instant, see toPeriodFrom(org.joda.time.ReadableInstant,org.joda.time.PeriodType).

Parameters:
type the period type to use, null means standard
Returns:
a Period created using the millisecond duration from this instance
    public Period toPeriod(PeriodType type) {
        return new Period(getMillis(), type);
    }

    
Converts this duration to a Period instance using the standard period type and the specified chronology.

Only precise fields in the period type will be used. Exactly which fields are precise depends on the chronology. Only the time fields are precise for ISO chronology with a time zone. However, ISO UTC also has precise days and weeks.

For more control over the conversion process, you must pair the duration with an instant, see toPeriodFrom(org.joda.time.ReadableInstant) and toPeriodTo(org.joda.time.ReadableInstant)

Parameters:
chrono the chronology to use, null means ISO default
Returns:
a Period created using the millisecond duration from this instance
    public Period toPeriod(Chronology chrono) {
        return new Period(getMillis(), chrono);
    }

    
Converts this duration to a Period instance using the specified period type and chronology.

Only precise fields in the period type will be used. Exactly which fields are precise depends on the chronology. Only the time fields are precise for ISO chronology with a time zone. However, ISO UTC also has precise days and weeks.

For more control over the conversion process, you must pair the duration with an instant, see toPeriodFrom(org.joda.time.ReadableInstant,org.joda.time.PeriodType) and toPeriodTo(org.joda.time.ReadableInstant,org.joda.time.PeriodType)

Parameters:
type the period type to use, null means standard
chrono the chronology to use, null means ISO default
Returns:
a Period created using the millisecond duration from this instance
    public Period toPeriod(PeriodType typeChronology chrono) {
        return new Period(getMillis(), typechrono);
    }

    
Converts this duration to a Period instance by adding the duration to a start instant to obtain an interval using the standard period type.

This conversion will determine the fields of a period accurately. The results are based on the instant millis, the chronology of the instant, the standard period type and the length of this duration.

Parameters:
startInstant the instant to calculate the period from, null means now
Returns:
a Period created using the millisecond duration from this instance
    public Period toPeriodFrom(ReadableInstant startInstant) {
        return new Period(startInstantthis);
    }

    
Converts this duration to a Period instance by adding the duration to a start instant to obtain an interval.

This conversion will determine the fields of a period accurately. The results are based on the instant millis, the chronology of the instant, the period type and the length of this duration.

Parameters:
startInstant the instant to calculate the period from, null means now
type the period type determining how to split the duration into fields, null means All type
Returns:
a Period created using the millisecond duration from this instance
    public Period toPeriodFrom(ReadableInstant startInstantPeriodType type) {
        return new Period(startInstantthistype);
    }

    
Converts this duration to a Period instance by subtracting the duration from an end instant to obtain an interval using the standard period type.

This conversion will determine the fields of a period accurately. The results are based on the instant millis, the chronology of the instant, the standard period type and the length of this duration.

Parameters:
endInstant the instant to calculate the period to, null means now
Returns:
a Period created using the millisecond duration from this instance
    public Period toPeriodTo(ReadableInstant endInstant) {
        return new Period(thisendInstant);
    }

    
Converts this duration to a Period instance by subtracting the duration from an end instant to obtain an interval using the standard period type.

This conversion will determine the fields of a period accurately. The results are based on the instant millis, the chronology of the instant, the period type and the length of this duration.

Parameters:
endInstant the instant to calculate the period to, null means now
type the period type determining how to split the duration into fields, null means All type
Returns:
a Period created using the millisecond duration from this instance
    public Period toPeriodTo(ReadableInstant endInstantPeriodType type) {
        return new Period(thisendInstanttype);
    }

    
Converts this duration to an Interval starting at the specified instant.

Parameters:
startInstant the instant to start the interval at, null means now
Returns:
an Interval starting at the specified instant
    public Interval toIntervalFrom(ReadableInstant startInstant) {
        return new Interval(startInstantthis);
    }

    
Converts this duration to an Interval ending at the specified instant.

Parameters:
endInstant the instant to end the interval at, null means now
Returns:
an Interval ending at the specified instant
    public Interval toIntervalTo(ReadableInstant endInstant) {
        return new Interval(thisendInstant);
    }
New to GrepCode? Check out our FAQ X