]> WPIA git - gigi.git/blob - src/club/wpia/gigi/util/DayDate.java
fix: correct validation of minimum and maximum ages
[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     private static final int MILLI_HOUR = 60 * 60 * 1000;
12
13     public static final long MILLI_DAY = 24 * MILLI_HOUR;
14
15     private long time;
16
17     /**
18      * Creates a new {@link DayDate} from the SQL Day-exact variant {@link Date}
19      * .
20      * 
21      * @see #toSQLDate()
22      */
23     public DayDate(Date date) {
24         this(date.getTime());
25     }
26
27     /**
28      * Creates a new {@link DayDate} based on the given millisecond timestamp.
29      * 
30      * @param millis
31      *            the timestamp to create the Date from.
32      * @throws IllegalArgumentException
33      *             if the parameter contains more precision than needed.
34      */
35     public DayDate(long millis) {
36         this.time = millis;
37         if (millis % MILLI_DAY != 0) {
38             throw new IllegalArgumentException();
39         }
40     }
41
42     /**
43      * Gets the enclosed timestamp.
44      * 
45      * @return the enclosed timestamp.
46      */
47     public long getTime() {
48         return time;
49     }
50
51     /**
52      * Converts this DayDate to an {@link Date}.
53      * 
54      * @return the corresponding {@link Date}
55      * @see #DayDate(Date)
56      */
57     public Date toSQLDate() {
58         return new Date(time);
59     }
60
61     @Override
62     public boolean equals(Object obj) {
63         if (null == obj) {
64             return false;
65         }
66         if ( !(obj instanceof DayDate)) {
67             throw new Error("You may not compare this date somthing other than a DayDate");
68         }
69         return ((DayDate) obj).time == time;
70     }
71
72     @Override
73     public int hashCode() {
74         return Long.hashCode(time);
75     }
76
77     public java.util.Date toDate() {
78         return new java.util.Date(time);
79     }
80
81     // Timezones currently reach from UTC-12 to UTC+14 that allows us to define
82     // the "earliest start" and "latest end" of a Date.
83     /**
84      * Gets this date's starting point.
85      * 
86      * @return The earliest point in time where this date was in any timezone
87      */
88     public java.util.Date start() {
89         return new java.util.Date(time - 12 * MILLI_HOUR);
90     }
91
92     /**
93      * Gets this date's ending point.
94      * 
95      * @return The latest point in time where this Date was any timezone
96      */
97     public java.util.Date end() {
98         return new java.util.Date(time + 14 * MILLI_HOUR + 24 * MILLI_HOUR);
99     }
100 }