1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.foundation;
20
21 import java.util.Date;
22 import java.util.GregorianCalendar;
23 import java.util.TimeZone;
24
25 /***
26 * A pure java implementation of NSDate that extends
27 * java.util.Date for greater java compatibility.
28 *
29 * @author michael@mpowers.net
30 * @author $Author: cgruber $
31 * @version $Revision: 892 $
32 */
33 public class NSDate extends Date
34 {
35
36
37 public static final int NSOrderedAscending = -1;
38 public static final int NSOrderedSame = 0;
39 public static final int NSOrderedDescending = 1;
40
41
42
43
44 /***
45 * Default constructor represents the current date.
46 */
47 public NSDate ()
48 {
49 super();
50 }
51
52 /***
53 * Represents the specified number of seconds from the current date.
54 */
55 public NSDate (double seconds)
56 {
57 super( (long) new NSDate().getTime() +
58 timeIntervalToMilliseconds(seconds) );
59 }
60
61 /***
62 * Represents the specified number of seconds from the specified date.
63 */
64 public NSDate (double seconds, Date sinceDate)
65 {
66 super( (long) sinceDate.getTime() +
67 timeIntervalToMilliseconds(seconds) );
68 }
69
70 /***
71 * Returns the interval between this date and 1 January 2001 GMT.
72 */
73 public double timeIntervalSinceReferenceDate ()
74 {
75 GregorianCalendar referenceDate =
76 new GregorianCalendar( TimeZone.getTimeZone( "GMT" ) );
77 referenceDate.set( 2001, 0, 0, 0, 0, 0 );
78 return timeIntervalSinceDate( referenceDate.getTime() );
79 }
80
81 /***
82 * Returns the interval between this date and the specified date
83 * in seconds.
84 */
85 public double timeIntervalSinceDate (Date aDate)
86 {
87 return millisecondsToTimeInterval(
88 this.getTime() - aDate.getTime() );
89 }
90
91 /***
92 * Returns the interval between this date and the current date
93 * in seconds.
94 */
95 public double timeIntervalSinceNow ()
96 {
97 return timeIntervalSinceDate( new NSDate() );
98 }
99
100 /***
101 * Compares this date to the specified date and returns the
102 * earlier date. Unspecified which is returned if both are equal.
103 */
104 public NSDate earlierDate (NSDate aDate)
105 {
106 if ( aDate == null ) return this;
107 if ( after( aDate ) ) return aDate;
108 return this;
109 }
110
111 /***
112 * Compares this date to the specified date and returns the
113 * later date. Unspecified which is returned if both are equal.
114 */
115 public NSDate laterDate (NSDate aDate)
116 {
117 if ( aDate == null ) return this;
118 if ( before( aDate ) ) return aDate;
119 return this;
120 }
121
122 /***
123 * Returns a negative value if the specified date is later than
124 * this date, a positive value if the specified date is earlier
125 * than this date, or zero if the dates are equal. The return
126 * values are compatible with type NSComparisonResult.
127 */
128 public int compare (Date aDate)
129 {
130 if ( before( aDate ) ) return NSOrderedAscending;
131 if ( after( aDate ) ) return NSOrderedDescending;
132 return NSOrderedSame;
133 }
134
135 /***
136 * Returns whether the this date is equal to the specified date,
137 * per the result of equals().
138 */
139 public boolean isEqualToDate (Date aDate)
140 {
141 return equals( aDate );
142 }
143
144 /***
145 * Returns a date that differs from this date by the specified
146 * number of seconds.
147 */
148 public NSDate dateByAddingTimeInterval (double seconds)
149 {
150 return new NSDate( seconds, this );
151 }
152
153 /***
154 * Returns the number of seconds between now and the reference date.
155 */
156 public static double currentTimeIntervalSinceReferenceDate ()
157 {
158 return new NSDate().timeIntervalSinceReferenceDate();
159 }
160
161 /***
162 * Converts seconds to milliseconds. Included for compatibility.
163 */
164 public static long timeIntervalToMilliseconds (double seconds)
165 {
166 return (long) seconds*1000;
167 }
168
169 /***
170 * Converts milliseconds to seconds. Included for compatibility.
171 */
172 public static double millisecondsToTimeInterval (long millis)
173 {
174 return millis/1000.0;
175 }
176
177 /***
178 * Returns a date that is greater than all representable dates.
179 */
180 public static NSDate distantFuture ()
181 {
182 NSDate result = new NSDate();
183 result.setTime( Long.MAX_VALUE );
184 return result;
185 }
186
187 /***
188 * Returns a date that is less than all representable dates.
189 */
190 public static NSDate distantPast ()
191 {
192 NSDate result = new NSDate();
193 result.setTime( Long.MIN_VALUE );
194 return result;
195 }
196
197
198
199
200
201
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217