Constructors Summary |
---|
public Duration()Default no-arg constructor
|
public Duration(boolean negative, int aYears, int aMonths, int aDays, int aHours, int aMinutes, double 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.
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.
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 boolean | equals(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.
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.Calendar | getAsCalendar()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 getAsCalendar(Calendar.getInstance());
|
public java.util.Calendar | getAsCalendar(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.
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 int | getDays()
return days;
|
public int | getHours()
return hours;
|
public int | getMinutes()
return minutes;
|
public int | getMonths()
return months;
|
public double | getSeconds()
return seconds;
|
public int | getYears()
return years;
|
public int | hashCode()
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 boolean | isNegative()
return isNegative;
|
public void | parseDate(java.lang.String date)This method parses the date portion of a String that represents
xsd:duration - nYnMnD.
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 void | parseTime(java.lang.String time)This method parses the time portion of a String that represents
xsd:duration - nHnMnS.
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 void | setDays(int days)
this.days = days;
|
public void | setHours(int hours)
this.hours = hours;
|
public void | setMinutes(int minutes)
this.minutes = minutes;
|
public void | setMonths(int months)
this.months = months;
|
public void | setNegative(boolean negative)
isNegative = negative;
|
public void | setSeconds(int seconds)
this.seconds = seconds;
|
public void | setSeconds(double seconds)Sets the seconds. NOTE: The fractional value of seconds is rounded up to
milliseconds.
this.seconds = ((double) (Math.round(seconds * 100))) / 100;
|
public void | setYears(int years)
this.years = years;
|
public java.lang.String | toString()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();
|