com.marringtons.time
Class Time

java.lang.Object
  extended bycom.marringtons.time.Time
All Implemented Interfaces:
Comparable

public class Time
extends Object
implements Comparable

An object for working with the time of day. String formats for time are hours, minutes, seconds and milliseconds all separated my any non-numeric digit. Alternatively they can be fixed at 2, 2, 2 and 3 digits. 10:00:00.000, 10, 10:00, 1000, 100000, 1000000000 and 10:00am all create the equivalent time objects.

 Time time = new Time();		// time now
 time = new Time( "10:00");	// or at a string format of time.
 

Author:
Paul Marrington

Field Summary
 Calendar calendar
          Calendar structure for the current time
 int days
          Days for times above 24 hours
 String defaultFormat
          Set the default format for writing the time as a string.
 int hour
          Hour of day (0-24)
 boolean isInterval
          True if time is an interval rather than a time of day
 int millisecond
          Millisecond of second (0-999)
 int milliseconds
          Once a time is set this is the number of milliseconds since the last midnight.
 int minute
          Minute of hour (0-59)
 int second
          Second of minute (0-59)
 
Constructor Summary
Time()
          Default constructor sets the time for now - using the local computer rtc.
Time(int days, int hour, int minute, int second, int millisecond)
          Set the time object given a time in components.
Time(long milliseconds)
          Set the time given a time in milliseconds.
Time(String time)
          Set the time given some user entered time information - flexible format.
 
Method Summary
 int compareTo(Object otherTime)
           
 boolean equals(Object time)
          Simple equality given the Epoch time.
 int hashCode()
          The Epoch time chopped to an integer should make a good hash code.
static Time now()
          A convenience method for the default constructor to make code more readable.
 Time plus(Time addition)
          Calculate the time created by adding 2 times.
 String toString()
          Convert time to a string using the default format (hh:mm:ss.mmmpp).
 String toString(String format)
          Convert time to a string using the supplied format (hh:mm:ss.mmm/am/pm/).
 Time until(Time to)
          Calculate the time between 2 other times over a period of 24 hours or less.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

milliseconds

public int milliseconds
Once a time is set this is the number of milliseconds since the last midnight.


days

public int days
Days for times above 24 hours


hour

public int hour
Hour of day (0-24)


minute

public int minute
Minute of hour (0-59)


second

public int second
Second of minute (0-59)


millisecond

public int millisecond
Millisecond of second (0-999)


isInterval

public boolean isInterval
True if time is an interval rather than a time of day


calendar

public Calendar calendar
Calendar structure for the current time


defaultFormat

public String defaultFormat
Set the default format for writing the time as a string. Default is "hh:mm:ss.mmmpp". Details are in toString( String)

See Also:
toString(String)
Constructor Detail

Time

public Time()
Default constructor sets the time for now - using the local computer rtc.


Time

public Time(long milliseconds)
Set the time given a time in milliseconds.

Parameters:
milliseconds - - in day or epoch.

Time

public Time(int days,
            int hour,
            int minute,
            int second,
            int millisecond)
Set the time object given a time in components.

Parameters:
days - Can be used for a time a specific number of days away.
hour - Hours since midnight.
minute - Minutes past the last hour.
second - Seconds into the last minute.
millisecond - Thousandths of a second past the past second.

Time

public Time(String time)
Set the time given some user entered time information - flexible format. If it starts with a +, the isInterval flag is set to treat time as an interval. If you want days a more fixed format is required - dd hh:mm:ss.mm - where seconds and milliseconds are optional.

Parameters:
time - String representing the time of day - 12 or 24 hour format.
Method Detail

now

public static Time now()
A convenience method for the default constructor to make code more readable.

Returns:
Time structure for now

toString

public String toString()
Convert time to a string using the default format (hh:mm:ss.mmmpp).

Returns:
Time in the required format.
See Also:
Object.toString()

toString

public String toString(String format)
Convert time to a string using the supplied format (hh:mm:ss.mmm/am/pm/). Anything in between these patterns are copied into the resulting string. All items are optional.

Parameters:
format - Format string
Returns:
Time in the required format.
See Also:
Object.toString()

until

public Time until(Time to)
Calculate the time between 2 other times over a period of 24 hours or less.
 Time target = new Time( endTimeString);
 Time toWait = Time.now.until( target);
 

Parameters:
to - End time
Returns:
time between

plus

public Time plus(Time addition)
Calculate the time created by adding 2 times.
 Time endTime = Time.now.add( new Time( timeFromNowString));
 

Parameters:
addition - Time to add to the time stored in this object.
Returns:
end time (may have days).

equals

public boolean equals(Object time)
Simple equality given the Epoch time.

Returns:
true if equal time.
See Also:
Object.equals(java.lang.Object)

hashCode

public int hashCode()
The Epoch time chopped to an integer should make a good hash code.

Returns:
a fairly unique hash code.
See Also:
Object.hashCode()

compareTo

public int compareTo(Object otherTime)
Specified by:
compareTo in interface Comparable
See Also:
Comparable.compareTo(java.lang.Object)


Copyright © 2005 Paul Marrington http://library.marringtons.com