1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.foundation;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
/**
* A pure java implementation of NSDate that extends
* java.util.Date for greater java compatibility.
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 892 $
*/
public class NSDate extends Date
{
// NSComparisonResult compatibility
public static final int NSOrderedAscending = -1;
public static final int NSOrderedSame = 0;
public static final int NSOrderedDescending = 1;
// public static final double TimeIntervalSince1970;
// public static final NSDate DateFor1970;
/**
* Default constructor represents the current date.
*/
public NSDate ()
{
super();
}
/**
* Represents the specified number of seconds from the current date.
*/
public NSDate (double seconds)
{
super( (long) new NSDate().getTime() +
timeIntervalToMilliseconds(seconds) );
}
/**
* Represents the specified number of seconds from the specified date.
*/
public NSDate (double seconds, Date sinceDate)
{
super( (long) sinceDate.getTime() +
timeIntervalToMilliseconds(seconds) );
}
/**
* Returns the interval between this date and 1 January 2001 GMT.
*/
public double timeIntervalSinceReferenceDate ()
{
GregorianCalendar referenceDate =
new GregorianCalendar( TimeZone.getTimeZone( "GMT" ) );
referenceDate.set( 2001, 0, 0, 0, 0, 0 );
return timeIntervalSinceDate( referenceDate.getTime() );
}
/**
* Returns the interval between this date and the specified date
* in seconds.
*/
public double timeIntervalSinceDate (Date aDate)
{
return millisecondsToTimeInterval(
this.getTime() - aDate.getTime() );
}
/**
* Returns the interval between this date and the current date
* in seconds.
*/
public double timeIntervalSinceNow ()
{
return timeIntervalSinceDate( new NSDate() );
}
/**
* Compares this date to the specified date and returns the
* earlier date. Unspecified which is returned if both are equal.
*/
public NSDate earlierDate (NSDate aDate)
{
if ( aDate == null ) return this;
if ( after( aDate ) ) return aDate;
return this;
}
/**
* Compares this date to the specified date and returns the
* later date. Unspecified which is returned if both are equal.
*/
public NSDate laterDate (NSDate aDate)
{
if ( aDate == null ) return this;
if ( before( aDate ) ) return aDate;
return this;
}
/**
* Returns a negative value if the specified date is later than
* this date, a positive value if the specified date is earlier
* than this date, or zero if the dates are equal. The return
* values are compatible with type NSComparisonResult.
*/
public int compare (Date aDate)
{
if ( before( aDate ) ) return NSOrderedAscending;
if ( after( aDate ) ) return NSOrderedDescending;
return NSOrderedSame;
}
/**
* Returns whether the this date is equal to the specified date,
* per the result of equals().
*/
public boolean isEqualToDate (Date aDate)
{
return equals( aDate );
}
/**
* Returns a date that differs from this date by the specified
* number of seconds.
*/
public NSDate dateByAddingTimeInterval (double seconds)
{
return new NSDate( seconds, this );
}
/**
* Returns the number of seconds between now and the reference date.
*/
public static double currentTimeIntervalSinceReferenceDate ()
{
return new NSDate().timeIntervalSinceReferenceDate();
}
/**
* Converts seconds to milliseconds. Included for compatibility.
*/
public static long timeIntervalToMilliseconds (double seconds)
{
return (long) seconds*1000;
}
/**
* Converts milliseconds to seconds. Included for compatibility.
*/
public static double millisecondsToTimeInterval (long millis)
{
return millis/1000.0;
}
/**
* Returns a date that is greater than all representable dates.
*/
public static NSDate distantFuture ()
{
NSDate result = new NSDate();
result.setTime( Long.MAX_VALUE );
return result;
}
/**
* Returns a date that is less than all representable dates.
*/
public static NSDate distantPast ()
{
NSDate result = new NSDate();
result.setTime( Long.MIN_VALUE );
return result;
}
// inherited from java.util.Date
// public java.lang.String toString ();
// public boolean equals (java.lang.Object);
// public int hashCode ();
}
/*
* $Log$
* Revision 1.1 2006/02/16 12:47:16 cgruber
* Check in all sources in eclipse-friendly maven-enabled packages.
*
* Revision 1.1.1.1 2000/12/21 15:47:28 mpowers
* Contributing wotonomy.
*
* Revision 1.3 2000/12/20 16:25:38 michael
* Added log to all files.
*
*
*/
|