com.deltax.util
Class TimeInterval

java.lang.Object
  extended bycom.deltax.util.TimeInterval
All Implemented Interfaces:
java.io.Serializable

public class TimeInterval
extends java.lang.Object
implements java.io.Serializable

A class implementing a time interval, which parses back and forth human-readable specifications.

The minimum time unit is the minute: when converting from milliseconds, any part of minute is discarded, so for example a TimeInterval of 65.000ms equals a TimeInterval of 60.000ms.

The maximum time unit is the week.

The format for a string denoting a time interval is [w week[s]][+|,][ d day[s]][+|,][ h hour[s]][+|,][ m minutes[s]], where w,d ,h,m are integer values, and the following abbreviations may be used:

 
NormalCompactSingle charFixed length
weekwkwww
day-ddd
hourhrhhh
minuteminmmm

For example, 1week+2days+3hrs+30min or 3hrs,30min are valid descriptors.

Version:
1.4
Author:
C. Sadun
See Also:
Serialized Form

Field Summary
static long DAY
          One day, in milliseconds
static java.lang.String[] DAY_STR
          Descriptions and abbreviations for day
 long days
          Days portion of the time interval
static long HOUR
          One hour, in milliseconds
static java.lang.String[] HOUR_STR
          Descriptions and abbreviations for hour
 long hours
          Hours portion of the time interval
static long MILLISECOND
          One millisecond
static java.lang.String[] MILLISECOND_STR
          Descriptions and abbreviations for millisecond
 long milliseconds
          Milliseconds portion of the time interval
static long MINUTE
          One minute, in milliseconds
static java.lang.String[] MINUTE_STR
          Descriptions and abbreviations for minute
 long minutes
          Minute portion of the time interval
static long SECOND
          One second
static java.lang.String[] SECOND_STR
          Descriptions and abbreviations for second
 long seconds
          Seconds portion of the time interval
static long WEEK
          One week, in milliseconds
static java.lang.String[] WEEK_STR
          Descriptions and abbreviations for week
 long weeks
          Week portion of the time interval
 
Constructor Summary
TimeInterval()
          Create a time interval of zero length
TimeInterval(long l)
          Create a time interval of the given length, in milliseconds
TimeInterval(long weeks, long days, long hours, long minutes, long seconds, long milliseconds)
          Create a time interval, with the specified week/day/hour/minute values
TimeInterval(java.lang.String s)
          Creates a TimeInterval parsing a formatted specification.
 
Method Summary
static java.lang.String describe(long interval)
          Return the canonic description of the given interval (in milliseconds).
 boolean equals(java.lang.Object obj)
          Return true only for a TimeInterval encapsulating the same amount of time, false otherwise.
 long getTime()
          Return the length of this timeinterval, in milliseconds
 int hashCode()
          Return the hash code for this time interval
 boolean longer(TimeInterval timeinterval)
          Return true if the given time interval is longer than this
static void main(java.lang.String[] args)
          Attempts to parse the first argument and prints out its value in milliseconds.
static TimeInterval parse(java.lang.String s)
          Parses a time interval formatted description and returns the corresponding TimeInterval object See class comment for information on the format.
 boolean shorter(TimeInterval timeinterval)
          Return true if the given time interval is shorter than this
 java.lang.String toString()
          Create a formatted description of the timeinterval
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MILLISECOND

public static final long MILLISECOND
One millisecond


SECOND

public static final long SECOND
One second


MINUTE

public static final long MINUTE
One minute, in milliseconds


HOUR

public static final long HOUR
One hour, in milliseconds


DAY

public static final long DAY
One day, in milliseconds


WEEK

public static final long WEEK
One week, in milliseconds


MILLISECOND_STR

public static final java.lang.String[] MILLISECOND_STR
Descriptions and abbreviations for millisecond


SECOND_STR

public static final java.lang.String[] SECOND_STR
Descriptions and abbreviations for second


MINUTE_STR

public static final java.lang.String[] MINUTE_STR
Descriptions and abbreviations for minute


HOUR_STR

public static final java.lang.String[] HOUR_STR
Descriptions and abbreviations for hour


DAY_STR

public static final java.lang.String[] DAY_STR
Descriptions and abbreviations for day


WEEK_STR

public static final java.lang.String[] WEEK_STR
Descriptions and abbreviations for week


milliseconds

public long milliseconds
Milliseconds portion of the time interval


seconds

public long seconds
Seconds portion of the time interval


weeks

public long weeks
Week portion of the time interval


days

public long days
Days portion of the time interval


hours

public long hours
Hours portion of the time interval


minutes

public long minutes
Minute portion of the time interval

Constructor Detail

TimeInterval

public TimeInterval()
Create a time interval of zero length


TimeInterval

public TimeInterval(long l)
Create a time interval of the given length, in milliseconds

Parameters:
l - the length of the interval, in milliseconds

TimeInterval

public TimeInterval(long weeks,
                    long days,
                    long hours,
                    long minutes,
                    long seconds,
                    long milliseconds)
Create a time interval, with the specified week/day/hour/minute values

Parameters:
weeks - the week portion of the time interval
days - the day portion of the time interval
hours - the hour portion of the time interval
minutes - the minute portion of the time interval

TimeInterval

public TimeInterval(java.lang.String s)
             throws TimeIntervalFormatException
Creates a TimeInterval parsing a formatted specification. See class comment for information on the format.

Parameters:
s - the formatted string denoting the time interval
Method Detail

getTime

public long getTime()
Return the length of this timeinterval, in milliseconds

Returns:
the length of this timeinterval, in milliseconds

parse

public static TimeInterval parse(java.lang.String s)
                          throws TimeIntervalFormatException
Parses a time interval formatted description and returns the corresponding TimeInterval object See class comment for information on the format.

Parameters:
s - the formatted string denoting the time interval
Throws:
TimeIntervalFormatException

describe

public static java.lang.String describe(long interval)
Return the canonic description of the given interval (in milliseconds).

Parameters:
interval - the interval to describe, in milliseconds
Returns:
the canonic description of the interval.

longer

public boolean longer(TimeInterval timeinterval)
Return true if the given time interval is longer than this

Returns:
the length of this timeinterval, in milliseconds

shorter

public boolean shorter(TimeInterval timeinterval)
Return true if the given time interval is shorter than this

Returns:
the length of this timeinterval, in milliseconds

toString

public java.lang.String toString()
Create a formatted description of the timeinterval

Returns:
a formatted description of the timeinterval

equals

public boolean equals(java.lang.Object obj)
Return true only for a TimeInterval encapsulating the same amount of time, false otherwise.

Returns:
true only for a TimeInterval encapsulating the same amount of time, false otherwise

hashCode

public int hashCode()
Return the hash code for this time interval

Returns:
the hash code for this time interval

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Attempts to parse the first argument and prints out its value in milliseconds.

Throws:
java.lang.Exception