Class Duration

  • All Implemented Interfaces:
    Serializable, Comparable

    @AlfrescoPublicApi
    public class Duration
    extends Object
    implements Comparable, Serializable
    This data type represents duration/interval/period as defined by the XMLSchema type duration. The lexical representation of duration is PnYnMnDTnHnMnS. P is a literal value that starts the expression nY is an integer number of years followed by the literal Y nM is an integer number of months followed by the literal M nD is an integer number of days followed by the literal D T is the literal that separates the date and time nH is an integer number of hours followed by a literal H nM is an integer number of minutes followed by a literal M nS is a decimal number of seconds followed by a literal S Any numbers and designator may be absent if the value is zero. A minus sign may appear before the literal P to indicate a negative duration. If no time items are present the literal T must not appear. This implementation is immutable and thread safe. There are two forms of duration common on database types. The code contains warnings wheer these are relevant.
    Author:
    andyh
    See Also:
    Serialized Form
    • Constructor Detail

      • Duration

        public Duration()
        Constructor for Duration - a zero value duration
      • Duration

        public Duration​(String duration)
        Construct a Duration from the XMLSchema definition
      • Duration

        public Duration​(Date date)
        Create a duration given a date. The duration is between the two dates provided. Sadly, it works out the duration by incrementing the lower calendar until it matches the higher.
      • Duration

        public Duration​(String start,
                        String end)
        Create a duration betweeen two dates expressed as strings. Uses the standard XML date form.
        Parameters:
        start - - the date at the start of the period
        end - - the date at the end of the period
      • Duration

        public Duration​(Date start_in,
                        Date end_in)
        Construct a preiod between the two given dates
        Parameters:
        start_in - Date
        end_in - Date
      • Duration

        public Duration​(boolean positive_in,
                        long months_in,
                        long seconds_in,
                        long nanos_in)
        Construct a duration from months seconds and nanos Checks sign and fixes up seconds and nano. Treats year-month abd day-sec as separate chunks
    • Method Detail

      • add

        public static Date add​(Date date,
                               Duration duration)
        Add a duration to a date and return the date plus the specified increment.
        Parameters:
        date - - the initial date
        duration - - the duration to add on to the date (the duration may be negative)
        Returns:
        the adjusted date.
      • subtract

        public static Date subtract​(Date date,
                                    Duration duration)
        Subtract a period for a given date
        Parameters:
        date - - the intial date
        duration - - the diration to subtract
        Returns:
        the adjusted date.
      • subtract

        public Duration subtract​(Duration sub)
        Subtract one duration from another
      • unaryMinus

        public Duration unaryMinus()
        Negate the duration
      • divide

        public Duration divide​(int d)
        Divide the duration - if year-month drops the day-second part of the duration
      • isYearToMonth

        public boolean isYearToMonth()
        Check if is year-month
      • isDayToSec

        public boolean isDayToSec()
        Check if is day-sec
      • hasTime

        public boolean hasTime()
        Check if it includes time
      • getYearToMonth

        public Duration getYearToMonth()
        Extract the year to month part
      • getDayToYear

        public Duration getDayToYear()
        Extract the day to sec part.
      • largestComponentformattedString

        public String largestComponentformattedString()
      • formattedString

        public String formattedString()
        Format in human readable form TODO: I18n
      • main

        public static void main​(String[] args)
        TODO: Tests that should be moved into a unit test
        Parameters:
        args - String[]