/*
* (C) 2006 - Geotechnical Software Services
*
* This code is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
package no.geosoft.cc.util;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.io.Serializable;
/**
* A class representing a moment in time. Extends Day which represents
* the day of the moment, and defines the time within the day to
* millisecond accuracy.
*
* @author Jacob Dreyer (<a href="mailto:jacob.dreyer@geosoft.no">jacob.dreyer@geosoft.no</a>)
*/
public class Time extends Day
{
/**
* Instantiate a Time object.
* The time is lenient meaning that illegal day parameters can be
* specified and results in a recomputed day with legal month/day
* values.
*
* @param year Year of this time
* @param month Month of this time
* @param dayOfMonth Day of month of this time.
* @param hours Hours of this time [0-23]
* @param minutes Minutes of this time [0-23]
* @param seconds Seconds of this time [0-23]
*/
public Time (int year, int month, int dayOfMonth,
int hours, int minutes, int seconds)
{
super (year, month, dayOfMonth);
setHours (hours);
setMinutes (minutes);
setSeconds (seconds);
}
public Time (Day day, int hours, int minutes, int seconds)
{
this (day.getYear(), day.getMonth(), day.getDayOfMonth(),
hours, minutes, seconds);
}
public Time (int hours, int minutes, int seconds)
{
this (new Day(), hours, minutes, seconds);
}
public Time()
{
calendar_ = new GregorianCalendar(); // Now
}
public void setDay (Day day)
{
setYear (day.getYear());
setMonth (day.getMonth());
setDayOfMonth (day.getDayOfMonth());
}
public void setHours (int hours)
{
calendar_.set (Calendar.HOUR_OF_DAY, hours);
}
public int getHours ()
{
return calendar_.get (Calendar.HOUR_OF_DAY);
}
public void setMinutes (int minutes)
{
calendar_.set (Calendar.MINUTE, minutes);
}
public int getMinutes()
{
return calendar_.get (Calendar.MINUTE);
}
public void setSeconds (int seconds)
{
calendar_.set (Calendar.SECOND, seconds);
}
public int getSeconds()
{
return calendar_.get (Calendar.SECOND);
}
public void setMilliSeconds (int milliSeconds)
{
calendar_.set (Calendar.MILLISECOND, milliSeconds);
}
public int getMilliSeconds()
{
return calendar_.get (Calendar.MILLISECOND);
}
public boolean isAfter (Time time)
{
return calendar_.after (time.calendar_);
}
public boolean isBefore (Time time)
{
return calendar_.before (time.calendar_);
}
public boolean equals (Time time)
{
return calendar_.equals (time.calendar_);
}
public void addHours (int nHours)
{
calendar_.add (Calendar.HOUR_OF_DAY, nHours);
}
public void addMinutes (int nMinutes)
{
calendar_.add (Calendar.MINUTE, nMinutes);
}
public void addSeconds (int nSeconds)
{
calendar_.add (Calendar.SECOND, nSeconds);
}
public void addMilliSeconds (int nMilliSeconds)
{
calendar_.add (Calendar.MILLISECOND, nMilliSeconds);
}
public long milliSecondsBetween (Time time)
{
long millisBetween = calendar_.getTime().getTime() -
time.calendar_.getTime().getTime();
return millisBetween;
}
public double secondsBetween (Time time)
{
long millisBetween = calendar_.getTime().getTime() -
time.calendar_.getTime().getTime();
return millisBetween / 1000;
}
public double minutesBetween (Time time)
{
long millisBetween = calendar_.getTime().getTime() -
time.calendar_.getTime().getTime();
return millisBetween / (1000 * 60);
}
public double hoursBetween (Time time)
{
long millisBetween = calendar_.getTime().getTime() -
time.calendar_.getTime().getTime();
return millisBetween / (1000 * 60 * 60);
}
public String toString()
{
StringBuffer string = new StringBuffer();
string.append (super.toString());
string.append (' ');
if (getHours() < 10) string.append ('0');
string.append (getHours());
string.append (':');
if (getMinutes() < 10) string.append ('0');
string.append (getMinutes());
string.append (':');
if (getSeconds() < 10) string.append ('0');
string.append (getSeconds());
string.append (',');
string.append (getMilliSeconds());
return string.toString();
}
public static void main (String args[])
{
Time time = new Time (12,00,00);
System.out.println (time);
}
}