]> WPIA git - gigi.git/blob - src/club/wpia/gigi/util/DayDate.java
upd: rename package name and all references to it
[gigi.git] / src / club / wpia / gigi / util / DayDate.java
1 package club.wpia.gigi.util;
2
3 import java.sql.Date;
4
5 /**
6  * This Class consists of a millisecond timestamp that is only interesting up to
7  * day-precision.
8  */
9 public class DayDate {
10
11     public static final long MILLI_DAY = 24 * 60 * 60 * 1000;
12
13     private long time;
14
15     /**
16      * Creates a new {@link DayDate} from the SQL Day-exact variant {@link Date}
17      * .
18      * 
19      * @see #toSQLDate()
20      */
21     public DayDate(Date date) {
22         this(date.getTime());
23     }
24
25     /**
26      * Creates a new {@link DayDate} based on the given millisecond timestamp.
27      * 
28      * @param millis
29      *            the timestamp to create the Date from.
30      * @throws IllegalArgumentException
31      *             if the parameter contains more precision than needed.
32      */
33     public DayDate(long millis) {
34         this.time = millis;
35         if (millis % MILLI_DAY != 0) {
36             throw new IllegalArgumentException();
37         }
38     }
39
40     /**
41      * Gets the enclosed timestamp.
42      * 
43      * @return the enclosed timestamp.
44      */
45     public long getTime() {
46         return time;
47     }
48
49     /**
50      * Converts this DayDate to an {@link Date}.
51      * 
52      * @return the corresponding {@link Date}
53      * @see #DayDate(Date)
54      */
55     public Date toSQLDate() {
56         return new Date(time);
57     }
58
59     @Override
60     public boolean equals(Object obj) {
61         if (null == obj) {
62             return false;
63         }
64         if ( !(obj instanceof DayDate)) {
65             throw new Error("You may not compare this date somthing other than a DayDate");
66         }
67         return ((DayDate) obj).time == time;
68     }
69
70     @Override
71     public int hashCode() {
72         return Long.hashCode(time);
73     }
74
75     public java.util.Date toDate() {
76         return new java.util.Date(time);
77     }
78 }