java-org.hwo/src/org/hwo/datetime/TimeOfDay.java

104 lines
2.0 KiB
Java

package org.hwo.datetime;
import java.sql.Time;
import org.hwo.datetime.TimeOfDay;
public class TimeOfDay {
public static int SECONDSPERDAY = (24 * 60 * 60);
private int hours,
minutes,
seconds;
public static TimeOfDay beforeMidnight(){
TimeOfDay t = new TimeOfDay();
t.setHours(23);
t.setMinutes(59);
t.setSeconds(59);
return t;
}
public TimeOfDay() {
setHours(0);
setMinutes(0);
setSeconds(0);
}
public TimeOfDay(int seconds){
setSeconds(seconds);
}
public TimeOfDay(TimeOfDay time){
setSeconds(time.getSecondsOfTheDay());
}
public TimeOfDay(int hours,int minutes,int seconds){
this.setHours(hours);
this.setMinutes(minutes);
this.setSeconds(seconds);
}
public int compare(TimeOfDay comp){
if (getSecondsOfTheDay() < comp.getSecondsOfTheDay())
return -1;
if (getSecondsOfTheDay() > comp.getSecondsOfTheDay())
return 1;
return 0;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours % 24;
}
public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes = minutes % 60;
this.setHours(this.hours + (minutes/60));
}
public int getSeconds() {
return seconds;
}
public void setSeconds(int seconds) {
this.seconds = seconds % 60;
this.setMinutes(this.minutes + (seconds/60));
}
public int getSecondsOfTheDay(){
return (hours * 3600) + (minutes * 60) + seconds;
}
public boolean isEarlierThan(TimeOfDay time){
return getSecondsOfTheDay() < time.getSecondsOfTheDay();
}
public boolean isLaterThan(TimeOfDay time){
return getSecondsOfTheDay() > time.getSecondsOfTheDay();
}
public String getSQLTime(){
return String.format("%02d:%02d:%02d", hours,minutes,seconds);
}
public void setTime(Time time){
setHours(time.getHours());
setMinutes(time.getMinutes());
setSeconds(time.getSeconds());
}
public int secondsBefore(TimeOfDay comp){
return comp.getSecondsOfTheDay() - getSecondsOfTheDay();
}
}