Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2010 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;
 
 
 import  org.joda.convert.FromString;
An immutable duration specifying a length of time in milliseconds.

A duration is defined by a fixed number of milliseconds. There is no concept of fields, such as days or seconds, as these fields can vary in length. A duration may be converted to a Period to obtain field values. This conversion will typically cause a loss of precision however.

Duration is thread-safe and immutable.

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

    
Constant representing zero millisecond duration
 
     public static final Duration ZERO = new Duration(0L);

    
Serialization version
 
     private static final long serialVersionUID = 2471658376918L;
 
     //-----------------------------------------------------------------------
     
Parses a Duration from the specified string.

This parses the format PTa.bS, as per toString().

Parameters:
str the string to parse, not null
Since:
2.0
 
     @FromString
     public static Duration parse(String str) {
         return new Duration(str);
     }
 
     //-----------------------------------------------------------------------
     
Create a duration with the specified number of days assuming that there are the standard number of milliseconds in a day.

This method assumes that there are 24 hours in a day, 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in a second. This will be true for most days, however days with Daylight Savings changes will not have 24 hours, so use this method with care.

A Duration is a representation of an amount of time. If you want to express the concepts of 'days' you should consider using the Days class.

Parameters:
days the number of standard days in this duration
Returns:
the duration, never null
Throws:
ArithmeticException if the days value is too large
Since:
1.6
 
     public static Duration standardDays(long days) {
         if (days == 0) {
             return ;
         }
         return new Duration(FieldUtils.safeMultiply(days.));
     }

    
Create a duration with the specified number of hours assuming that there are the standard number of milliseconds in an hour.

This method assumes that there are 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in a second. All currently supplied chronologies use this definition.

A Duration is a representation of an amount of time. If you want to express the concepts of 'hours' you should consider using the Hours class.

Parameters:
hours the number of standard hours in this duration
Returns:
the duration, never null
Throws:
ArithmeticException if the hours value is too large
Since:
1.6
    public static Duration standardHours(long hours) {
        if (hours == 0) {
            return ;
        }
        return new Duration(FieldUtils.safeMultiply(hours.));
    }

    
Create a duration with the specified number of minutes assuming that there are the standard number of milliseconds in a minute.

This method assumes that there are 60 seconds in a minute and 1000 milliseconds in a second. All currently supplied chronologies use this definition.

A Duration is a representation of an amount of time. If you want to express the concepts of 'minutes' you should consider using the Minutes class.

Parameters:
minutes the number of standard minutes in this duration
Returns:
the duration, never null
Throws:
ArithmeticException if the minutes value is too large
Since:
1.6
    public static Duration standardMinutes(long minutes) {
        if (minutes == 0) {
            return ;
        }
        return new Duration(FieldUtils.safeMultiply(minutes.));
    }

    
Create a duration with the specified number of seconds assuming that there are the standard number of milliseconds in a second.

This method assumes that there are 1000 milliseconds in a second. All currently supplied chronologies use this definition.

A Duration is a representation of an amount of time. If you want to express the concepts of 'seconds' you should consider using the Seconds class.

Parameters:
seconds the number of standard seconds in this duration
Returns:
the duration, never null
Throws:
ArithmeticException if the seconds value is too large
Since:
1.6
    public static Duration standardSeconds(long seconds) {
        if (seconds == 0) {
            return ;
        }
        return new Duration(FieldUtils.safeMultiply(seconds.));
    }

    
Create a duration with the specified number of milliseconds.

Parameters:
millis the number of standard milliseconds in this duration
Returns:
the duration, never null
Since:
2.0
    public static Duration millis(long millis) {
        if (millis == 0) {
            return ;
        }
        return new Duration(millis);
    }
    //-----------------------------------------------------------------------
    
Creates a duration from the given millisecond duration.

Parameters:
duration the duration, in milliseconds
    public Duration(long duration) {
        super(duration);
    }

    
Creates a duration from the given interval endpoints.

Parameters:
startInstant interval start, in milliseconds
endInstant interval end, in milliseconds
Throws:
ArithmeticException if the duration exceeds a 64 bit long
    public Duration(long startInstantlong endInstant) {
        super(startInstantendInstant);
    }

    
Creates a duration from the given interval endpoints.

Parameters:
start interval start, null means now
end interval end, null means now
Throws:
ArithmeticException if the duration exceeds a 64 bit long
    public Duration(ReadableInstant startReadableInstant end) {
        super(startend);
    }

    
Creates a duration from the specified object using the ConverterManager.

Parameters:
duration duration to convert
Throws:
IllegalArgumentException if duration is invalid
    public Duration(Object duration) {
        super(duration);
    }
    //-----------------------------------------------------------------------
    
Gets the length of this duration in days assuming that there are the standard number of milliseconds in a day.

This method assumes that there are 24 hours in a day, 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in a second. This will be true for most days, however days with Daylight Savings changes will not have 24 hours, so use this method with care.

This returns getMillis() / MILLIS_PER_DAY. The result is an integer division, thus excess milliseconds are truncated.

Returns:
the length of the duration in standard seconds
Since:
2.0
    public long getStandardDays() {
        return getMillis() / .;
    }

    
Gets the length of this duration in hours assuming that there are the standard number of milliseconds in an hour.

This method assumes that there are 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in a second. All currently supplied chronologies use this definition.

This returns getMillis() / MILLIS_PER_HOUR. The result is an integer division, thus excess milliseconds are truncated.

Returns:
the length of the duration in standard seconds
Since:
2.0
    public long getStandardHours() {
        return getMillis() / .;
    }

    
Gets the length of this duration in minutes assuming that there are the standard number of milliseconds in a minute.

This method assumes that there are 60 seconds in a minute and 1000 milliseconds in a second. All currently supplied chronologies use this definition.

This returns getMillis() / 60000. The result is an integer division, thus excess milliseconds are truncated.

Returns:
the length of the duration in standard seconds
Since:
2.0
    public long getStandardMinutes() {
        return getMillis() / .;
    }

    
Gets the length of this duration in seconds assuming that there are the standard number of milliseconds in a second.

This method assumes that there are 1000 milliseconds in a second. All currently supplied chronologies use this definition.

This returns getMillis() / 1000. The result is an integer division, so 2999 millis returns 2 seconds.

Returns:
the length of the duration in standard seconds
Since:
1.6
    public long getStandardSeconds() {
        return getMillis() / .;
    }
    //-----------------------------------------------------------------------
    
Get this duration as an immutable Duration object by returning this.

Returns:
this
    public Duration toDuration() {
        return this;
    }

    
Converts this duration to a period in days assuming that there are the standard number of milliseconds in a day.

This method assumes that there are 24 hours in a day, 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in a second. This will be true for most days, however days with Daylight Savings changes will not have 24 hours, so use this method with care.

Returns:
a period representing the number of standard days in this period, never null
Throws:
ArithmeticException if the number of days is too large to be represented
Since:
2.0
    public Days toStandardDays() {
        long days = getStandardDays();
        return Days.days(FieldUtils.safeToInt(days));
    }

    
Converts this duration to a period in hours assuming that there are the standard number of milliseconds in an hour.

This method assumes that there are 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in a second. All currently supplied chronologies use this definition.

Returns:
a period representing the number of standard hours in this period, never null
Throws:
ArithmeticException if the number of hours is too large to be represented
Since:
2.0
    public Hours toStandardHours() {
        long hours = getStandardHours();
        return Hours.hours(FieldUtils.safeToInt(hours));
    }

    
Converts this duration to a period in minutes assuming that there are the standard number of milliseconds in a minute.

This method assumes that there are 60 seconds in a minute and 1000 milliseconds in a second. All currently supplied chronologies use this definition.

Returns:
a period representing the number of standard minutes in this period, never null
Throws:
ArithmeticException if the number of minutes is too large to be represented
Since:
2.0
    public Minutes toStandardMinutes() {
        long minutes = getStandardMinutes();
        return Minutes.minutes(FieldUtils.safeToInt(minutes));
    }

    
Converts this duration to a period in seconds assuming that there are the standard number of milliseconds in a second.

This method assumes that there are 1000 milliseconds in a second. All currently supplied chronologies use this definition.

Returns:
a period representing the number of standard seconds in this period, never null
Throws:
ArithmeticException if the number of seconds is too large to be represented
Since:
1.6
    public Seconds toStandardSeconds() {
        long seconds = getStandardSeconds();
        return Seconds.seconds(FieldUtils.safeToInt(seconds));
    }
    //-----------------------------------------------------------------------
    
Creates a new Duration instance with a different milisecond length.

Parameters:
duration the new length of the duration
Returns:
the new duration instance
    public Duration withMillis(long duration) {
        if (duration == getMillis()) {
            return this;
        }
        return new Duration(duration);
    }

    
Returns a new duration with this length plus that specified multiplied by the scalar. This instance is immutable and is not altered.

If the addition is zero, this instance is returned.

Parameters:
durationToAdd the duration to add to this one
scalar the amount of times to add, such as -1 to subtract once
Returns:
the new duration instance
    public Duration withDurationAdded(long durationToAddint scalar) {
        if (durationToAdd == 0 || scalar == 0) {
            return this;
        }
        long add = FieldUtils.safeMultiply(durationToAddscalar);
        long duration = FieldUtils.safeAdd(getMillis(), add);
        return new Duration(duration);
    }

    
Returns a new duration with this length plus that specified multiplied by the scalar. This instance is immutable and is not altered.

If the addition is zero, this instance is returned.

Parameters:
durationToAdd the duration to add to this one, null means zero
scalar the amount of times to add, such as -1 to subtract once
Returns:
the new duration instance
    public Duration withDurationAdded(ReadableDuration durationToAddint scalar) {
        if (durationToAdd == null || scalar == 0) {
            return this;
        }
        return withDurationAdded(durationToAdd.getMillis(), scalar);
    }
    //-----------------------------------------------------------------------
    
Returns a new duration with this length plus that specified. This instance is immutable and is not altered.

If the addition is zero, this instance is returned.

Parameters:
amount the duration to add to this one
Returns:
the new duration instance
    public Duration plus(long amount) {
        return withDurationAdded(amount, 1);
    }

    
Returns a new duration with this length plus that specified. This instance is immutable and is not altered.

If the amount is zero, this instance is returned.

Parameters:
amount the duration to add to this one, null means zero
Returns:
the new duration instance
    public Duration plus(ReadableDuration amount) {
        if (amount == null) {
            return this;
        }
        return withDurationAdded(amount.getMillis(), 1);
    }

    
Returns a new duration with this length minus that specified. This instance is immutable and is not altered.

If the addition is zero, this instance is returned.

Parameters:
amount the duration to take away from this one
Returns:
the new duration instance
    public Duration minus(long amount) {
        return withDurationAdded(amount, -1);
    }

    
Returns a new duration with this length minus that specified. This instance is immutable and is not altered.

If the amount is zero, this instance is returned.

Parameters:
amount the duration to take away from this one, null means zero
Returns:
the new duration instance
    public Duration minus(ReadableDuration amount) {
        if (amount == null) {
            return this;
        }
        return withDurationAdded(amount.getMillis(), -1);
    }
New to GrepCode? Check out our FAQ X