001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.datetime;
004:
005: import jodd.util.ObjectUtil;
006: import jodd.util.HashCodeUtil;
007:
008: /**
009: * Generic date time stamp just stores and holds date and time information.
010: * This class does not contain any date/time logic, neither guarantees
011: * that date is valid.
012: *
013: * @see JDateTime
014: * @see JulianDateStamp
015: */
016: public class DateTimeStamp implements Comparable, java.io.Serializable,
017: Cloneable {
018:
019: /**
020: * Default empty constructor.
021: */
022: public DateTimeStamp() {
023: }
024:
025: /**
026: * Constructor that sets date and time.
027: */
028: public DateTimeStamp(int year, int month, int day, int hour,
029: int minute, double second) {
030: this .year = year;
031: this .month = month;
032: this .day = day;
033: this .hour = hour;
034: this .minute = minute;
035: this .second = second;
036: }
037:
038: /**
039: * Constructor that sets just date. Time is set to zeros.
040: */
041: public DateTimeStamp(int year, int month, int day) {
042: this (year, month, day, 0, 0, 0);
043: }
044:
045: /**
046: * Year.
047: */
048: public int year;
049:
050: /**
051: * Month, range: [1 - 12]
052: */
053: public int month = 1;
054:
055: /**
056: * Day, range: [1 - 31]
057: */
058: public int day = 1;
059:
060: /**
061: * Hour, range: [0 - 23]
062: */
063: public int hour;
064:
065: /**
066: * Minute, range [0 - 59]
067: */
068: public int minute;
069:
070: /**
071: * Second, range: [0.000 - 59.999]
072: */
073: public double second;
074:
075: // ---------------------------------------------------------------- get/set
076:
077: public int getYear() {
078: return year;
079: }
080:
081: public void setYear(int year) {
082: this .year = year;
083: }
084:
085: public int getMonth() {
086: return month;
087: }
088:
089: public void setMonth(int month) {
090: this .month = month;
091: }
092:
093: public int getDay() {
094: return day;
095: }
096:
097: public void setDay(int day) {
098: this .day = day;
099: }
100:
101: public int getHour() {
102: return hour;
103: }
104:
105: public void setHour(int hour) {
106: this .hour = hour;
107: }
108:
109: public int getMinute() {
110: return minute;
111: }
112:
113: public void setMinute(int minute) {
114: this .minute = minute;
115: }
116:
117: public double getSecond() {
118: return second;
119: }
120:
121: public void setSecond(double second) {
122: this .second = second;
123: }
124:
125: // ---------------------------------------------------------------- compare
126:
127: /**
128: * Compares this object with the specified object for order. Returns a
129: * negative integer, zero, or a positive integer as this object is less
130: * than, equal to, or greater than the specified object.
131: *
132: * @param o the Object to be compared.
133: * @return a negative integer, zero, or a positive integer as this object
134: * is less than, equal to, or greater than the specified object.
135: *
136: * @throws ClassCastException if the specified object's type prevents it
137: * from being compared to this Object.
138: */
139: public int compareTo(Object o) {
140: DateTimeStamp gts = (DateTimeStamp) o;
141:
142: int date1 = year * 10000 + month * 100 + day;
143: int date2 = gts.year * 10000 + gts.month * 100 + gts.day;
144:
145: if (date1 < date2) {
146: return -1;
147: }
148: if (date1 > date2) {
149: return 1;
150: }
151:
152: date1 = hour * 10000000 + minute * 100000
153: + (int) (second * 1000);
154: date2 = gts.hour * 10000000 + gts.minute * 100000
155: + (int) (gts.second * 1000);
156:
157: if (date1 < date2) {
158: return -1;
159: }
160: if (date1 > date2) {
161: return 1;
162: }
163:
164: return 0;
165: }
166:
167: // ---------------------------------------------------------------- toString
168:
169: /**
170: * Simple to string conversion.
171: *
172: * @return date/time string in 'y-m-d h:m:m.s' format
173: */
174: @Override
175: public String toString() {
176: double sec = (int) (second * 1000) / 1000.0;
177: return year + "-" + month + '-' + day + ' ' + hour + ':'
178: + minute + ':' + sec;
179: }
180:
181: // ---------------------------------------------------------------- equals & hashCode
182:
183: @Override
184: public boolean equals(Object object) {
185: if (this == object) {
186: return true;
187: }
188: if (ObjectUtil.equalsType(object, this ) == false) {
189: return false;
190: }
191: final DateTimeStamp stamp = (DateTimeStamp) object;
192:
193: return (stamp.year == this .year)
194: && (stamp.month == this .minute)
195: && (stamp.day == this .day)
196: && (stamp.hour == this .hour)
197: && (stamp.minute == this .minute)
198: && (Double.doubleToLongBits(stamp.second) == Double
199: .doubleToLongBits(this .second));
200: }
201:
202: @Override
203: public int hashCode() {
204: int result = HashCodeUtil.SEED;
205: result = HashCodeUtil.hash(result, this .year);
206: result = HashCodeUtil.hash(result, this .month);
207: result = HashCodeUtil.hash(result, this .day);
208: result = HashCodeUtil.hash(result, this .hour);
209: result = HashCodeUtil.hash(result, this .minute);
210: result = HashCodeUtil.hash(result, this .second);
211: return result;
212: }
213:
214: // ---------------------------------------------------------------- clone
215:
216: @Override
217: protected Object clone() {
218: DateTimeStamp dts = new DateTimeStamp();
219: dts.year = this.year;
220: dts.month = this.month;
221: dts.day = this.day;
222: dts.hour = this.hour;
223: dts.minute = this.minute;
224: dts.second = this.second;
225: return dts;
226: }
227: }
|