KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > datetime > DateTimeStamp


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.datetime;
4
5 import jodd.util.ObjectUtil;
6 import jodd.util.HashCodeUtil;
7
8 /**
9  * Generic date time stamp just stores and holds date and time information.
10  * This class does not contain any date/time logic, neither guarantees
11  * that date is valid.
12  *
13  * @see JDateTime
14  * @see JulianDateStamp
15  */

16 public class DateTimeStamp implements Comparable JavaDoc, java.io.Serializable JavaDoc, Cloneable JavaDoc {
17
18     /**
19      * Default empty constructor.
20      */

21     public DateTimeStamp() {
22     }
23
24     /**
25      * Constructor that sets date and time.
26      */

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     /**
37      * Constructor that sets just date. Time is set to zeros.
38      */

39     public DateTimeStamp(int year, int month, int day) {
40         this(year, month, day, 0, 0, 0);
41     }
42
43     /**
44      * Year.
45      */

46     public int year;
47
48     /**
49      * Month, range: [1 - 12]
50      */

51     public int month = 1;
52
53     /**
54      * Day, range: [1 - 31]
55      */

56     public int day = 1;
57
58     /**
59      * Hour, range: [0 - 23]
60      */

61     public int hour;
62
63     /**
64      * Minute, range [0 - 59]
65      */

66     public int minute;
67
68     /**
69      * Second, range: [0.000 - 59.999]
70      */

71     public double second;
72
73
74     // ---------------------------------------------------------------- get/set
75

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     // ---------------------------------------------------------------- compare
125

126     /**
127      * Compares this object with the specified object for order. Returns a
128      * negative integer, zero, or a positive integer as this object is less
129      * than, equal to, or greater than the specified object.
130      *
131      * @param o the Object to be compared.
132      * @return a negative integer, zero, or a positive integer as this object
133      * is less than, equal to, or greater than the specified object.
134      *
135      * @throws ClassCastException if the specified object's type prevents it
136      * from being compared to this Object.
137      */

138     public int compareTo(Object JavaDoc 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     // ---------------------------------------------------------------- toString
166

167     /**
168      * Simple to string conversion.
169      *
170      * @return date/time string in 'y-m-d h:m:m.s' format
171      */

172     public String JavaDoc toString() {
173         double sec = (int)(second * 1000) / 1000.0;
174         return year + "-" + month + '-' + day + ' ' + hour + ':' + minute + ':' + sec;
175     }
176
177     // ---------------------------------------------------------------- equals & hashCode
178

179     public boolean equals(Object JavaDoc 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     // ---------------------------------------------------------------- clone
208

209
210     protected Object JavaDoc 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