1 3 package jodd.datetime; 4 5 import jodd.util.ObjectUtil; 6 import jodd.util.HashCodeUtil; 7 8 16 public class DateTimeStamp implements Comparable , java.io.Serializable , Cloneable { 17 18 21 public DateTimeStamp() { 22 } 23 24 27 public DateTimeStamp(int year, int month, int day, int hour, int minute, double second) { 28 this.year = year; 29 this.month = month; 30 this.day = day; 31 this.hour = hour; 32 this.minute = minute; 33 this.second = second; 34 } 35 36 39 public DateTimeStamp(int year, int month, int day) { 40 this(year, month, day, 0, 0, 0); 41 } 42 43 46 public int year; 47 48 51 public int month = 1; 52 53 56 public int day = 1; 57 58 61 public int hour; 62 63 66 public int minute; 67 68 71 public double second; 72 73 74 76 public int getYear() { 77 return year; 78 } 79 80 public void setYear(int year) { 81 this.year = year; 82 } 83 84 public int getMonth() { 85 return month; 86 } 87 88 public void setMonth(int month) { 89 this.month = month; 90 } 91 92 public int getDay() { 93 return day; 94 } 95 96 public void setDay(int day) { 97 this.day = day; 98 } 99 100 public int getHour() { 101 return hour; 102 } 103 104 public void setHour(int hour) { 105 this.hour = hour; 106 } 107 108 public int getMinute() { 109 return minute; 110 } 111 112 public void setMinute(int minute) { 113 this.minute = minute; 114 } 115 116 public double getSecond() { 117 return second; 118 } 119 120 public void setSecond(double second) { 121 this.second = second; 122 } 123 124 126 138 public int compareTo(Object o) { 139 DateTimeStamp gts = (DateTimeStamp) o; 140 141 int date1 = year * 10000 + month * 100 + day; 142 int date2 = gts.year * 10000 + gts.month * 100 + gts.day; 143 144 if (date1 < date2) { 145 return -1; 146 } 147 if (date1 > date2) { 148 return 1; 149 } 150 151 date1 = hour * 10000000 + minute * 100000 + (int) (second * 1000); 152 date2 = gts.hour * 10000000 + gts.minute * 100000 + (int) (gts.second * 1000); 153 154 if (date1 < date2) { 155 return -1; 156 } 157 if (date1 > date2) { 158 return 1; 159 } 160 161 return 0; 162 } 163 164 165 167 172 public String toString() { 173 double sec = (int)(second * 1000) / 1000.0; 174 return year + "-" + month + '-' + day + ' ' + hour + ':' + minute + ':' + sec; 175 } 176 177 179 public boolean equals(Object object) { 180 if (this == object) { 181 return true; 182 } 183 if (ObjectUtil.equalsType(object, this) == false) { 184 return false; 185 } 186 final DateTimeStamp stamp = (DateTimeStamp) object; 187 188 return (stamp.year == this.year) && 189 (stamp.month == this.minute) && 190 (stamp.day == this.day) && 191 (stamp.hour == this.hour) && 192 (stamp.minute == this.minute) && 193 (Double.doubleToLongBits(stamp.second) == Double.doubleToLongBits(this.second)); 194 } 195 196 public int hashCode() { 197 int result = HashCodeUtil.SEED; 198 result = HashCodeUtil.hash(result, this.year); 199 result = HashCodeUtil.hash(result, this.minute); 200 result = HashCodeUtil.hash(result, this.day); 201 result = HashCodeUtil.hash(result, this.hour); 202 result = HashCodeUtil.hash(result, this.minute); 203 result = HashCodeUtil.hash(result, this.second); 204 return result; 205 } 206 207 209 210 protected Object clone() { 211 DateTimeStamp dts = new DateTimeStamp(); 212 dts.year = this.year; 213 dts.month = this.month; 214 dts.day = this.day; 215 dts.hour = this.hour; 216 dts.minute = this.minute; 217 dts.second = this.second; 218 return dts; 219 } 220 } 221 | Popular Tags |