forked from LupusNobilis/java-org.hwo
126 lines
2.6 KiB
Java
126 lines
2.6 KiB
Java
package org.hwo.datetime;
|
|
|
|
import java.sql.Time;
|
|
import java.sql.Timestamp;
|
|
import java.util.Calendar;
|
|
|
|
public class DateTime {
|
|
|
|
private Date date;
|
|
private TimeOfDay
|
|
time;
|
|
|
|
public DateTime()
|
|
{
|
|
this.setTime(new TimeOfDay());
|
|
this.setDate(new Date());
|
|
}
|
|
|
|
public DateTime(Date date)
|
|
{
|
|
this.setTime(new TimeOfDay());
|
|
this.setDate(new Date(date));
|
|
}
|
|
|
|
public DateTime(Date date,TimeOfDay time)
|
|
{
|
|
this.setTime(new TimeOfDay(time));
|
|
this.setDate(new Date(date));
|
|
}
|
|
|
|
public DateTime(Timestamp timestamp){
|
|
this.setTime(new TimeOfDay());
|
|
this.setDate(new Date());
|
|
setTimeStamp(timestamp);
|
|
}
|
|
|
|
public DateTime(DateTime source){
|
|
this.date = source.getDate().Clone();
|
|
this.time = new TimeOfDay(source.getTime());
|
|
}
|
|
|
|
static public DateTime NOW(){
|
|
Calendar calendar = Calendar.getInstance();
|
|
Timestamp ts = new Timestamp(calendar.getTimeInMillis());
|
|
|
|
return new DateTime(ts);
|
|
}
|
|
|
|
public int compare(DateTime comp){
|
|
int dc = this.date.compare(comp.getDate());
|
|
if (dc != 0)
|
|
return dc;
|
|
return this.time.compare(comp.getTime());
|
|
}
|
|
|
|
public boolean isBefore(DateTime comp){
|
|
return (compare(comp)<0);
|
|
}
|
|
public boolean isAfter(DateTime comp){
|
|
return (compare(comp)>0);
|
|
}
|
|
public boolean isEqual(DateTime comp){
|
|
return (compare(comp)==0);
|
|
}
|
|
|
|
|
|
public Date getDate() {
|
|
return date;
|
|
}
|
|
|
|
public void setDate(Date date) {
|
|
this.date = date;
|
|
}
|
|
|
|
public TimeOfDay getTime() {
|
|
return time;
|
|
}
|
|
|
|
public void setTime(TimeOfDay time) {
|
|
this.time = time;
|
|
}
|
|
|
|
public String getSQLDateTime(){
|
|
return String.format("%s %s",this.date.getSQLDate(),this.time.getSQLTime());
|
|
}
|
|
|
|
public Timestamp getTimeStamp(){
|
|
return new Timestamp(date.year -1900, date.month - 1, date.day, time.getHours(), time.getMinutes(), time.getSeconds(), 0);
|
|
}
|
|
|
|
public void setTimeStamp(Timestamp timestamp){
|
|
this.date.setYear(timestamp.getYear()+1900);
|
|
this.date.setMonth(timestamp.getMonth()+1);
|
|
this.date.setDay(timestamp.getDay());
|
|
this.time.setHours(timestamp.getHours());
|
|
this.time.setMinutes(timestamp.getMinutes());
|
|
this.time.setSeconds(timestamp.getSeconds());
|
|
}
|
|
|
|
public int secondsBefore(DateTime comp){
|
|
int result = date.daysBefore(comp.getDate()) * TimeOfDay.SECONDSPERDAY;
|
|
result += time.secondsBefore(comp.getTime());
|
|
return result;
|
|
}
|
|
|
|
public void addSeconds(int seconds){
|
|
TimeOfDay newTime = new TimeOfDay(time);
|
|
int days = seconds / TimeOfDay.SECONDSPERDAY;
|
|
seconds %= TimeOfDay.SECONDSPERDAY;
|
|
|
|
date.addDays(days);
|
|
newTime.setSeconds(newTime.getSeconds() + seconds);
|
|
|
|
if (newTime.isEarlierThan(time))
|
|
date.addDays(1);
|
|
|
|
time = newTime;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return getSQLDateTime();
|
|
}
|
|
|
|
}
|