biweekly.util
Class Duration

java.lang.Object
  extended by biweekly.util.Duration

public final class Duration
extends Object

Represents a period of time (for example, "2 hours and 30 minutes").

This class is immutable. Use the builder() method to construct a new instance, or the parse(java.lang.String) method to parse a duration string.

Examples:

 Duration duration = Duration.builder().hours(2).minutes(30).build();
 Duration duration = Duration.parse("PT2H30M");
 
 //add a duration value to a Date
 Date start = ...
 Date end = duration.add(start);
 

Author:
Michael Angstadt

Nested Class Summary
static class Duration.Builder
          Builds Duration objects.
 
Method Summary
 Date add(Date date)
          Adds this duration value to a Date object.
static Duration.Builder builder()
          Creates a builder object for constructing new instances of this class.
static Duration diff(Date start, Date end)
          Builds a duration based on the difference between two dates.
 boolean equals(Object obj)
           
static Duration fromMillis(long milliseconds)
          Builds a duration from a number of milliseconds.
 Integer getDays()
          Gets the number of days.
 Integer getHours()
          Gets the number of hours.
 Integer getMinutes()
          Gets the number of minutes.
 Integer getSeconds()
          Gets the number of seconds.
 Integer getWeeks()
          Gets the number of weeks.
 int hashCode()
           
 boolean hasTime()
          Determines if any time components are present.
 boolean isPrior()
          Gets whether the duration is negative.
static Duration parse(String value)
          Parses a duration string.
 long toMillis()
          Converts the duration value to milliseconds.
 String toString()
          Converts the duration to its string representation.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

parse

public static Duration parse(String value)
Parses a duration string.

Parameters:
value - the duration string (e.g. "P30DT10H")
Returns:
the parsed duration
Throws:
IllegalArgumentException - if the duration string is invalid

diff

public static Duration diff(Date start,
                            Date end)
Builds a duration based on the difference between two dates.

Parameters:
start - the start date
end - the end date
Returns:
the duration

fromMillis

public static Duration fromMillis(long milliseconds)
Builds a duration from a number of milliseconds.

Parameters:
milliseconds - the number of milliseconds
Returns:
the duration

builder

public static Duration.Builder builder()
Creates a builder object for constructing new instances of this class.

Returns:
the builder object

isPrior

public boolean isPrior()
Gets whether the duration is negative.

Returns:
true if it's negative, false if not

getWeeks

public Integer getWeeks()
Gets the number of weeks.

Returns:
the number of weeks or null if not set

getDays

public Integer getDays()
Gets the number of days.

Returns:
the number of days or null if not set

getHours

public Integer getHours()
Gets the number of hours.

Returns:
the number of hours or null if not set

getMinutes

public Integer getMinutes()
Gets the number of minutes.

Returns:
the number of minutes or null if not set

getSeconds

public Integer getSeconds()
Gets the number of seconds.

Returns:
the number of seconds or null if not set

add

public Date add(Date date)
Adds this duration value to a Date object.

Parameters:
date - the date to add to
Returns:
the new date value

toMillis

public long toMillis()
Converts the duration value to milliseconds.

Returns:
the duration value in milliseconds (will be negative if isPrior() is true)

hasTime

public boolean hasTime()
Determines if any time components are present.

Returns:
true if the duration has at least one time component, false if not

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

toString

public String toString()
Converts the duration to its string representation.

Overrides:
toString in class Object
Returns:
the string representation (e.g. "P4DT1H" for "4 days and 1 hour")


Copyright © 2013 Michael Angstadt. All Rights Reserved.