DateTime Tool Classes update
parent
33562be436
commit
6440418319
|
@ -0,0 +1,105 @@
|
|||
package org.hwo.datetime;
|
||||
|
||||
public class Date {
|
||||
|
||||
int year,
|
||||
month,
|
||||
day;
|
||||
|
||||
public Date(){
|
||||
java.util.Date d = new java.util.Date();
|
||||
year = d.getYear() + 1900;
|
||||
month = d.getMonth() + 1;
|
||||
day = d.getDate();
|
||||
}
|
||||
|
||||
public Date(Date date){
|
||||
year = date.year;
|
||||
month = date.month;
|
||||
day = date.day;
|
||||
}
|
||||
|
||||
public Date Clone(){
|
||||
Date d = new Date();
|
||||
d.year = year;
|
||||
d.month = month;
|
||||
d.day = day;
|
||||
return d;
|
||||
}
|
||||
|
||||
public int getYear() {
|
||||
return year;
|
||||
}
|
||||
public void setYear(int year) {
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public int getMonth() {
|
||||
return month;
|
||||
}
|
||||
public void setMonth(int month) {
|
||||
this.month = month;
|
||||
}
|
||||
|
||||
public int getDay() {
|
||||
return day;
|
||||
}
|
||||
public void setDay(int day) {
|
||||
|
||||
this.day = day;
|
||||
}
|
||||
|
||||
public void addMonth(int months){
|
||||
this.month += months;
|
||||
while (this.month > 12){
|
||||
this.year += 1;
|
||||
this.month -= 12;
|
||||
}
|
||||
while (this.month < 1){
|
||||
this.year -= 1;
|
||||
this.month += 12;
|
||||
}
|
||||
}
|
||||
|
||||
public void addDays(int days){
|
||||
this.day += days;
|
||||
while (this.day > getDaysInMonth()){
|
||||
this.day -= getDaysInMonth();
|
||||
addMonth(1);
|
||||
}
|
||||
while (this.day < 1){
|
||||
addMonth(-1);
|
||||
this.day += getDaysInMonth();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getSQLDate(){
|
||||
return String.format("%04d-%02d-%02d",year,month,day);
|
||||
}
|
||||
|
||||
public void fromSQLDate(String sqlDate){
|
||||
setYear(Integer.parseInt(sqlDate.substring(0,4)));
|
||||
setMonth(Integer.parseInt(sqlDate.substring(5, 7)));
|
||||
setDay(Integer.parseInt(sqlDate.substring(8, 10)));
|
||||
}
|
||||
|
||||
|
||||
public boolean isLeapYear(){
|
||||
if (year % 400 == 0)
|
||||
return true;
|
||||
if ( (year % 100 != 0) && (year % 4 == 0))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static int[] daysInMonth = { 31,28,31,30,31,30,31,31,30,31,30,31 };
|
||||
public int getDaysInMonth(){
|
||||
int d = daysInMonth[ month ];
|
||||
if (month == 2)
|
||||
d += 1;
|
||||
return d;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,15 +1,67 @@
|
|||
package org.hwo.datetime;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
public class DateTime {
|
||||
|
||||
private Date date;
|
||||
private TimeOfDay
|
||||
time;
|
||||
|
||||
public static String getDateCode()
|
||||
public DateTime()
|
||||
{
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
||||
return sdf.format(new Date());
|
||||
this.setTime(new TimeOfDay());
|
||||
this.setDate(new 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 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());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getSQLDateTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -144,7 +144,7 @@ public class JTimeOfDay extends JPanel {
|
|||
{
|
||||
spHours.setValue(timeOfDay.getHours());
|
||||
spMinutes.setValue(timeOfDay.getMinutes());
|
||||
spSeconds.setValue(timeOfDay.getSeonds());
|
||||
spSeconds.setValue(timeOfDay.getSeconds());
|
||||
}
|
||||
|
||||
public TimeOfDay getTimeOfDay()
|
||||
|
|
|
@ -1,79 +1,82 @@
|
|||
package org.hwo.datetime;
|
||||
|
||||
import java.sql.Time;
|
||||
import java.util.Date;
|
||||
|
||||
import org.hwo.datetime.TimeOfDay;
|
||||
|
||||
public class TimeOfDay {
|
||||
|
||||
public static int getHours(int secondsOfDay)
|
||||
{
|
||||
return secondsOfDay / 3600;
|
||||
}
|
||||
public static int getMinutes(int secondsOfDay)
|
||||
{
|
||||
return (secondsOfDay % 3600) / 60;
|
||||
}
|
||||
public static int getSeconds(int secondsOfDay)
|
||||
{
|
||||
return (secondsOfDay % 60);
|
||||
private int hours,
|
||||
minutes,
|
||||
seconds;
|
||||
|
||||
public TimeOfDay() {
|
||||
setHours(0);
|
||||
setMinutes(0);
|
||||
setSeconds(0);
|
||||
}
|
||||
|
||||
private int secondsOfDay;
|
||||
|
||||
public TimeOfDay()
|
||||
{
|
||||
secondsOfDay = 0;
|
||||
}
|
||||
public TimeOfDay(int sod)
|
||||
{
|
||||
secondsOfDay = sod;
|
||||
}
|
||||
public TimeOfDay(int hour,int minute,int second)
|
||||
{
|
||||
initialize(hour, minute, second);
|
||||
public TimeOfDay(int seconds){
|
||||
setSeconds(getSecondsOfTheDay());
|
||||
}
|
||||
|
||||
private void initialize(int hour,int minute,int second)
|
||||
{
|
||||
secondsOfDay = (hour * 3600) + (minute * 60) + second;
|
||||
public TimeOfDay(TimeOfDay time){
|
||||
setSeconds(time.getSecondsOfTheDay());
|
||||
}
|
||||
|
||||
public int getSeondsOfDay()
|
||||
{
|
||||
return secondsOfDay;
|
||||
public TimeOfDay(int hours,int minutes,int seconds){
|
||||
this.setHours(hours);
|
||||
this.setMinutes(minutes);
|
||||
this.setSeconds(seconds);
|
||||
}
|
||||
public void setSecondsOfDay(int secondsOfDay)
|
||||
{
|
||||
this.secondsOfDay = secondsOfDay;
|
||||
|
||||
public int getHours() {
|
||||
return hours;
|
||||
}
|
||||
|
||||
public int getHours()
|
||||
{
|
||||
return getHours(secondsOfDay);
|
||||
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 getMinutes()
|
||||
{
|
||||
return getMinutes(secondsOfDay);
|
||||
public int getSecondsOfTheDay(){
|
||||
return (hours * 3600) + (minutes * 60) + seconds;
|
||||
}
|
||||
|
||||
public int getSeonds()
|
||||
{
|
||||
return getSeconds(secondsOfDay);
|
||||
public boolean isEarlierThan(TimeOfDay time){
|
||||
return getSecondsOfTheDay() < time.getSecondsOfTheDay();
|
||||
}
|
||||
|
||||
public boolean isLaterThan(TimeOfDay time){
|
||||
return getSecondsOfTheDay() > time.getSecondsOfTheDay();
|
||||
}
|
||||
|
||||
public void setHours(int hours)
|
||||
{
|
||||
initialize(hours, getMinutes(), getSeonds());
|
||||
public String getSQLTime(){
|
||||
return String.format("%02d:%02d:%02d", hours,minutes,seconds);
|
||||
}
|
||||
|
||||
public void setMinutes(int minutes)
|
||||
{
|
||||
initialize(getHours(), minutes, getSeonds());
|
||||
public void setTime(Time time){
|
||||
setHours(time.getHours());
|
||||
setMinutes(time.getMinutes());
|
||||
setSeconds(time.getSeconds());
|
||||
}
|
||||
|
||||
public void setSeconds(int seconds)
|
||||
{
|
||||
initialize(getHours(), getMinutes(), seconds);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue