FileDocCategorySizeDatePackage
Duration.javaAPI DocApache Axis 1.414230Sat Apr 22 18:57:26 BST 2006org.apache.axis.types

Duration

public class Duration extends Object implements Serializable
Implementation of the XML Schema type duration. Duration supports a minimum fractional second precision of milliseconds.
author
Wes Moulder
author
Dominik Kacprzak (dominik@opentoolbox.com)
see
XML Schema 3.2.6

Fields Summary
boolean
isNegative
int
years
int
months
int
days
int
hours
int
minutes
double
seconds
Constructors Summary
public Duration()
Default no-arg constructor


            
      
    
public Duration(boolean negative, int aYears, int aMonths, int aDays, int aHours, int aMinutes, double aSeconds)

param
negative
param
aYears
param
aMonths
param
aDays
param
aHours
param
aMinutes
param
aSeconds

        isNegative = negative;
        years = aYears;
        months = aMonths;
        days = aDays;
        hours = aHours;
        minutes = aMinutes;
        setSeconds(aSeconds);
    
public Duration(String duration)
Constructs Duration from a String in an xsd:duration format - PnYnMnDTnHnMnS.

param
duration String
throws
SchemaException if the string doesn't parse correctly.

        int position = 1;
        int timePosition = duration.indexOf("T");

        // P is required but P by itself is invalid
        if (duration.indexOf("P") == -1 || duration.equals("P")) {
            throw new IllegalArgumentException(
                    Messages.getMessage("badDuration"));
        }

        // if present, time cannot be empty
        if (duration.lastIndexOf("T") == duration.length() - 1) {
            throw new IllegalArgumentException(
                    Messages.getMessage("badDuration"));
        }

        // check the sign
        if (duration.startsWith("-")) {
            isNegative = true;
            position++;
        }

        // parse time part
        if (timePosition != -1) {
            parseTime(duration.substring(timePosition + 1));
        } else {
            timePosition = duration.length();
        }

        // parse date part
        if (position != timePosition) {
            parseDate(duration.substring(position, timePosition));
        }
    
public Duration(boolean negative, Calendar calendar)
Constructs Duration from a Calendar.

param
calendar Calendar
throws
IllegalArgumentException if the calendar object does not represent any date nor time.

        this.isNegative = negative;
        this.years = calendar.get(Calendar.YEAR);
        this.months = calendar.get(Calendar.MONTH);
        this.days = calendar.get(Calendar.DATE);
        this.hours = calendar.get(Calendar.HOUR);
        this.minutes = calendar.get(Calendar.MINUTE);
        this.seconds = calendar.get(Calendar.SECOND);
        this.seconds += ((double) calendar.get(Calendar.MILLISECOND)) / 100;
        if (years == 0 && months == 0 && days == 0 && hours == 0 &&
            minutes == 0 && seconds == 0) {
            throw new IllegalArgumentException(Messages.getMessage(
                    "badCalendarForDuration"));
        }
    
Methods Summary
public booleanequals(java.lang.Object object)
The equals method compares the time represented by duration object, not its string representation. Hence, a duration object representing 65 minutes is considered equal to a duration object representing 1 hour and 5 minutes.

param
object

        if (!(object instanceof Duration)) {
            return false;
        }

        Calendar thisCalendar = this.getAsCalendar();
        Duration duration = (Duration) object;

        return this.isNegative == duration.isNegative &&
                this.getAsCalendar().equals(duration.getAsCalendar());
    
public java.util.CalendargetAsCalendar()
Returns duration as a calendar. Due to the way a Calendar class works, the values for particular fields may not be the same as obtained through getter methods. For example, if a duration's object getMonths returns 20, a similar call on a calendar object will return 1 year and 8 months.

return
Calendar

        return getAsCalendar(Calendar.getInstance());
    
public java.util.CalendargetAsCalendar(java.util.Calendar startTime)
Returns duration as a calendar. Due to the way a Calendar class works, the values for particular fields may not be the same as obtained through getter methods. For example, if a Duration's object getMonths returns 20, a similar call on a Calendar object will return 1 year and 8 months.

param
startTime Calendar
return
Calendar

        Calendar ret = (Calendar) startTime.clone();
        ret.set(Calendar.YEAR, years);
        ret.set(Calendar.MONTH, months);
        ret.set(Calendar.DATE, days);
        ret.set(Calendar.HOUR, hours);
        ret.set(Calendar.MINUTE, minutes);
        ret.set(Calendar.SECOND, (int) seconds);
        ret.set(Calendar.MILLISECOND,
                (int) (seconds * 100 - Math.round(seconds) * 100));
        return ret;
    
public intgetDays()

        return days;
    
public intgetHours()

        return hours;
    
public intgetMinutes()

        return minutes;
    
public intgetMonths()

        return months;
    
public doublegetSeconds()

        return seconds;
    
public intgetYears()

        return years;
    
public inthashCode()

        int hashCode = 0;

        if (isNegative) {
            hashCode++;
        }
        hashCode += years;
        hashCode += months;
        hashCode += days;
        hashCode += hours;
        hashCode += minutes;
        hashCode += seconds;
        // milliseconds
        hashCode += (seconds * 100) % 100;

        return hashCode;
    
public booleanisNegative()

        return isNegative;
    
public voidparseDate(java.lang.String date)
This method parses the date portion of a String that represents xsd:duration - nYnMnD.

param
date
throws
IllegalArgumentException if date does not match pattern

        if (date.length() == 0 || date.indexOf("-") != -1) {
            throw new IllegalArgumentException(
                    Messages.getMessage("badDateDuration"));
        }

        // check if date string ends with either Y, M, or D
        if (!date.endsWith("Y") && !date.endsWith("M") && !date.endsWith("D")) {
            throw new IllegalArgumentException(
                    Messages.getMessage("badDateDuration"));
        }

        // catch any parsing exception
        try {
            // parse string and extract years, months, days
            int start = 0;
            int end = date.indexOf("Y");

            // if there is Y in a string but there is no value for years,
            // throw an exception
            if (start == end) {
                throw new IllegalArgumentException(
                        Messages.getMessage("badDateDuration"));
            }
            if (end != -1) {
                years = Integer.parseInt(date.substring(0, end));
                start = end + 1;
            }

            // months
            end = date.indexOf("M");
            // if there is M in a string but there is no value for months,
            // throw an exception
            if (start == end) {
                throw new IllegalArgumentException(
                        Messages.getMessage("badDateDuration"));
            }
            if (end != -1) {
                months = Integer.parseInt(date.substring(start, end));
                start = end + 1;
            }

            end = date.indexOf("D");
            // if there is D in a string but there is no value for days,
            // throw an exception
            if (start == end) {
                throw new IllegalArgumentException(
                        Messages.getMessage("badDateDuration"));
            }
            if (end != -1) {
                days = Integer.parseInt(date.substring(start, end));
            }
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException(
                    Messages.getMessage("badDateDuration"));
        }
    
public voidparseTime(java.lang.String time)
This method parses the time portion of a String that represents xsd:duration - nHnMnS.

param
time
throws
IllegalArgumentException if time does not match pattern

        if (time.length() == 0 || time.indexOf("-") != -1) {
            throw new IllegalArgumentException(
                    Messages.getMessage("badTimeDuration"));
        }

        // check if time ends with either H, M, or S
        if (!time.endsWith("H") && !time.endsWith("M") && !time.endsWith("S")) {
            throw new IllegalArgumentException(
                    Messages.getMessage("badTimeDuration"));
        }

        try {
            // parse string and extract hours, minutes, and seconds
            int start = 0;

            // Hours
            int end = time.indexOf("H");
            // if there is H in a string but there is no value for hours,
            // throw an exception
            if (start == end) {
                throw new IllegalArgumentException(
                        Messages.getMessage("badTimeDuration"));
            }
            if (end != -1) {
                hours = Integer.parseInt(time.substring(0, end));
                start = end + 1;
            }

            // Minutes
            end = time.indexOf("M");
            // if there is M in a string but there is no value for hours,
            // throw an exception
            if (start == end) {
                throw new IllegalArgumentException(
                        Messages.getMessage("badTimeDuration"));
            }

            if (end != -1) {
                minutes = Integer.parseInt(time.substring(start, end));
                start = end + 1;
            }

            // Seconds
            end = time.indexOf("S");
            // if there is S in a string but there is no value for hours,
            // throw an exception
            if (start == end) {
                throw new IllegalArgumentException(
                        Messages.getMessage("badTimeDuration"));
            }

            if (end != -1) {
                setSeconds(Double.parseDouble(time.substring(start, end)));
            }
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException(
                    Messages.getMessage("badTimeDuration"));
        }
    
public voidsetDays(int days)

param
days

        this.days = days;
    
public voidsetHours(int hours)

param
hours

        this.hours = hours;
    
public voidsetMinutes(int minutes)

param
minutes

        this.minutes = minutes;
    
public voidsetMonths(int months)

param
months

        this.months = months;
    
public voidsetNegative(boolean negative)

param
negative

        isNegative = negative;
    
public voidsetSeconds(int seconds)

param
seconds
deprecated
use {@link #setSeconds(double) setSeconds(double)} instead

        this.seconds = seconds;
    
public voidsetSeconds(double seconds)
Sets the seconds. NOTE: The fractional value of seconds is rounded up to milliseconds.

param
seconds double

        this.seconds = ((double) (Math.round(seconds * 100))) / 100;
    
public voidsetYears(int years)

param
years

        this.years = years;
    
public java.lang.StringtoString()
This returns the xml representation of an xsd:duration object.

        StringBuffer duration = new StringBuffer();

        duration.append("P");

        if (years != 0) {
            duration.append(years + "Y");
        }
        if (months != 0) {
            duration.append(months + "M");
        }
        if (days != 0) {
            duration.append(days + "D");
        }
        if (hours != 0 || minutes != 0 || seconds != 0.0) {
            duration.append("T");

            if (hours != 0) {
                duration.append(hours + "H");

            }
            if (minutes != 0) {
                duration.append(minutes + "M");

            }
            if (seconds != 0) {
                if (seconds == (int) seconds) {
                    duration.append((int) seconds + "S");
                } else {
                    duration.append(seconds + "S");
                }
            }
        }

        if (duration.length() == 1) {
            duration.append("T0S");
        }

        if (isNegative) {
            duration.insert(0, "-");
        }

        return duration.toString();