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;
|
package org.hwo.datetime;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.sql.Timestamp;
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
public class DateTime {
|
public class DateTime {
|
||||||
|
|
||||||
|
private Date date;
|
||||||
|
private TimeOfDay
|
||||||
|
time;
|
||||||
|
|
||||||
public static String getDateCode()
|
public DateTime()
|
||||||
{
|
{
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
this.setTime(new TimeOfDay());
|
||||||
return sdf.format(new Date());
|
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());
|
spHours.setValue(timeOfDay.getHours());
|
||||||
spMinutes.setValue(timeOfDay.getMinutes());
|
spMinutes.setValue(timeOfDay.getMinutes());
|
||||||
spSeconds.setValue(timeOfDay.getSeonds());
|
spSeconds.setValue(timeOfDay.getSeconds());
|
||||||
}
|
}
|
||||||
|
|
||||||
public TimeOfDay getTimeOfDay()
|
public TimeOfDay getTimeOfDay()
|
||||||
|
|
|
@ -1,79 +1,82 @@
|
||||||
package org.hwo.datetime;
|
package org.hwo.datetime;
|
||||||
|
|
||||||
|
import java.sql.Time;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.hwo.datetime.TimeOfDay;
|
||||||
|
|
||||||
public class TimeOfDay {
|
public class TimeOfDay {
|
||||||
|
|
||||||
public static int getHours(int secondsOfDay)
|
private int hours,
|
||||||
{
|
minutes,
|
||||||
return secondsOfDay / 3600;
|
seconds;
|
||||||
}
|
|
||||||
public static int getMinutes(int secondsOfDay)
|
public TimeOfDay() {
|
||||||
{
|
setHours(0);
|
||||||
return (secondsOfDay % 3600) / 60;
|
setMinutes(0);
|
||||||
}
|
setSeconds(0);
|
||||||
public static int getSeconds(int secondsOfDay)
|
|
||||||
{
|
|
||||||
return (secondsOfDay % 60);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int secondsOfDay;
|
public TimeOfDay(int seconds){
|
||||||
|
setSeconds(getSecondsOfTheDay());
|
||||||
public TimeOfDay()
|
|
||||||
{
|
|
||||||
secondsOfDay = 0;
|
|
||||||
}
|
|
||||||
public TimeOfDay(int sod)
|
|
||||||
{
|
|
||||||
secondsOfDay = sod;
|
|
||||||
}
|
|
||||||
public TimeOfDay(int hour,int minute,int second)
|
|
||||||
{
|
|
||||||
initialize(hour, minute, second);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initialize(int hour,int minute,int second)
|
public TimeOfDay(TimeOfDay time){
|
||||||
{
|
setSeconds(time.getSecondsOfTheDay());
|
||||||
secondsOfDay = (hour * 3600) + (minute * 60) + second;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSeondsOfDay()
|
public TimeOfDay(int hours,int minutes,int seconds){
|
||||||
{
|
this.setHours(hours);
|
||||||
return secondsOfDay;
|
this.setMinutes(minutes);
|
||||||
|
this.setSeconds(seconds);
|
||||||
}
|
}
|
||||||
public void setSecondsOfDay(int secondsOfDay)
|
|
||||||
{
|
public int getHours() {
|
||||||
this.secondsOfDay = secondsOfDay;
|
return hours;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getHours()
|
public void setHours(int hours) {
|
||||||
{
|
this.hours = hours % 24;
|
||||||
return getHours(secondsOfDay);
|
}
|
||||||
|
|
||||||
|
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()
|
public int getSecondsOfTheDay(){
|
||||||
{
|
return (hours * 3600) + (minutes * 60) + seconds;
|
||||||
return getMinutes(secondsOfDay);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSeonds()
|
public boolean isEarlierThan(TimeOfDay time){
|
||||||
{
|
return getSecondsOfTheDay() < time.getSecondsOfTheDay();
|
||||||
return getSeconds(secondsOfDay);
|
}
|
||||||
|
|
||||||
|
public boolean isLaterThan(TimeOfDay time){
|
||||||
|
return getSecondsOfTheDay() > time.getSecondsOfTheDay();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHours(int hours)
|
public String getSQLTime(){
|
||||||
{
|
return String.format("%02d:%02d:%02d", hours,minutes,seconds);
|
||||||
initialize(hours, getMinutes(), getSeonds());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMinutes(int minutes)
|
public void setTime(Time time){
|
||||||
{
|
setHours(time.getHours());
|
||||||
initialize(getHours(), minutes, getSeonds());
|
setMinutes(time.getMinutes());
|
||||||
|
setSeconds(time.getSeconds());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSeconds(int seconds)
|
|
||||||
{
|
|
||||||
initialize(getHours(), getMinutes(), seconds);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue