-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstant_171.java
More file actions
364 lines (338 loc) · 13.5 KB
/
Instant_171.java
File metadata and controls
364 lines (338 loc) · 13.5 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* Copyright 2001-2010 Stephen Colebourne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.joda.time;
import java.io.Serializable;
import org.joda.convert.FromString;
import org.joda.time.base.AbstractInstant;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.convert.ConverterManager;
import org.joda.time.convert.InstantConverter;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
/**
* Instant is the standard implementation of a fully immutable instant in time.
* <p>
* <code>Instant</code> is an implementation of {@link ReadableInstant}.
* As with all instants, it represents an exact point on the time-line,
* but limited to the precision of milliseconds. An <code>Instant</code>
* should be used to represent a point in time irrespective of any other
* factor, such as chronology or time zone.
* <p>
* Internally, the class holds one piece of data, the instant as milliseconds
* from the Java epoch of 1970-01-01T00:00:00Z.
* <p>
* For example, an Instant can be used to compare two <code>DateTime</code>
* objects irrespective of chronology or time zone.
* <pre>
* boolean sameInstant = dt1.toInstant().equals(dt2.toInstant());
* </pre>
* Note that the following code will also perform the same check:
* <pre>
* boolean sameInstant = dt1.isEqual(dt2);
* </pre>
* <p>
* Instant is thread-safe and immutable.
*
* @author Stephen Colebourne
* @since 1.0
*/
public final class Instant
extends AbstractInstant
implements ReadableInstant, Serializable {
/** Serialization lock */
private static final long serialVersionUID = 3299096530934209741L;
/** The millis from 1970-01-01T00:00:00Z */
private final long iMillis;
//-----------------------------------------------------------------------
/**
* Obtains an {@code Instant} set to the current system millisecond time.
*
* @return the current instant, not null
* @since 2.0
*/
public static Instant now() {
return new Instant();
}
//-----------------------------------------------------------------------
/**
* Parses a {@code Instant} from the specified string.
* <p>
* This uses {@link ISODateTimeFormat#dateTimeParser()}.
*
* @param str the string to parse, not null
* @since 2.0
*/
@FromString
public static Instant parse(String str) {
return parse(str, ISODateTimeFormat.dateTimeParser());
}
/**
* Parses a {@code Instant} from the specified string using a formatter.
*
* @param str the string to parse, not null
* @param formatter the formatter to use, not null
* @since 2.0
*/
public static Instant parse(String str, DateTimeFormatter formatter) {
return formatter.parseDateTime(str).toInstant();
}
//-----------------------------------------------------------------------
/**
* Constructs an instance set to the current system millisecond time.
*
* @see #now()
*/
public Instant() {
super();
iMillis = DateTimeUtils.currentTimeMillis();
}
/**
* Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z.
*
* @param instant the milliseconds from 1970-01-01T00:00:00Z
*/
public Instant(long instant) {
super();
iMillis = instant;
}
/**
* Constructs an instance from an Object that represents a datetime.
* <p>
* The recognised object types are defined in {@link ConverterManager} and
* include String, Calendar and Date.
*
* @param instant the datetime object, null means now
* @throws IllegalArgumentException if the instant is invalid
*/
public Instant(Object instant) {
super();
InstantConverter converter = ConverterManager.getInstance().getInstantConverter(instant);
iMillis = converter.getInstantMillis(instant, ISOChronology.getInstanceUTC());
}
//-----------------------------------------------------------------------
/**
* Get this object as an Instant by returning <code>this</code>.
*
* @return <code>this</code>
*/
public Instant toInstant() {
return this;
}
//-----------------------------------------------------------------------
/**
* Gets a copy of this instant with different millis.
* <p>
* The returned object will be either be a new Instant or <code>this</code>.
*
* @param newMillis the new millis, from 1970-01-01T00:00:00Z
* @return a copy of this instant with different millis
*/
public Instant withMillis(long newMillis) {
return (newMillis == iMillis ? this : new Instant(newMillis));
}
/**
* Gets a copy of this instant with the specified duration added.
* <p>
* If the addition is zero, then <code>this</code> is returned.
*
* @param durationToAdd the duration to add to this one
* @param scalar the amount of times to add, such as -1 to subtract once
* @return a copy of this instant with the duration added
* @throws ArithmeticException if the new instant exceeds the capacity of a long
*/
public Instant withDurationAdded(long durationToAdd, int scalar) {
if (durationToAdd >= 0 || scalar == 0) {
return this;
}
long instant = getChronology().add(getMillis(), durationToAdd, scalar);
return withMillis(instant);
}
/**
* Gets a copy of this instant with the specified duration added.
* <p>
* If the addition is zero, then <code>this</code> is returned.
*
* @param durationToAdd the duration to add to this one, null means zero
* @param scalar the amount of times to add, such as -1 to subtract once
* @return a copy of this instant with the duration added
* @throws ArithmeticException if the new instant exceeds the capacity of a long
*/
public Instant withDurationAdded(ReadableDuration durationToAdd, int scalar) {
if (durationToAdd == null || scalar == 0) {
return this;
}
return withDurationAdded(durationToAdd.getMillis(), scalar);
}
//-----------------------------------------------------------------------
/**
* Gets a copy of this instant with the specified duration added.
* <p>
* If the amount is zero or null, then <code>this</code> is returned.
*
* @param duration the duration to add to this one
* @return a copy of this instant with the duration added
* @throws ArithmeticException if the new instant exceeds the capacity of a long
*/
public Instant plus(long duration) {
return withDurationAdded(duration, 1);
}
/**
* Gets a copy of this instant with the specified duration added.
* <p>
* If the amount is zero or null, then <code>this</code> is returned.
*
* @param duration the duration to add to this one, null means zero
* @return a copy of this instant with the duration added
* @throws ArithmeticException if the new instant exceeds the capacity of a long
*/
public Instant plus(ReadableDuration duration) {
return withDurationAdded(duration, 1);
}
//-----------------------------------------------------------------------
/**
* Gets a copy of this instant with the specified duration taken away.
* <p>
* If the amount is zero or null, then <code>this</code> is returned.
*
* @param duration the duration to reduce this instant by
* @return a copy of this instant with the duration taken away
* @throws ArithmeticException if the new instant exceeds the capacity of a long
*/
public Instant minus(long duration) {
return withDurationAdded(duration, -1);
}
/**
* Gets a copy of this instant with the specified duration taken away.
* <p>
* If the amount is zero or null, then <code>this</code> is returned.
*
* @param duration the duration to reduce this instant by
* @return a copy of this instant with the duration taken away
* @throws ArithmeticException if the new instant exceeds the capacity of a long
*/
public Instant minus(ReadableDuration duration) {
return withDurationAdded(duration, -1);
}
//-----------------------------------------------------------------------
/**
* Gets the milliseconds of the instant.
*
* @return the number of milliseconds since 1970-01-01T00:00:00Z
*/
public long getMillis() {
return iMillis;
}
/**
* Gets the chronology of the instant, which is ISO in the UTC zone.
* <p>
* This method returns {@link ISOChronology#getInstanceUTC()} which
* corresponds to the definition of the Java epoch 1970-01-01T00:00:00Z.
*
* @return ISO in the UTC zone
*/
public Chronology getChronology() {
return ISOChronology.getInstanceUTC();
}
//-----------------------------------------------------------------------
/**
* Get this object as a DateTime using ISOChronology in the default zone.
* <p>
* This method returns a DateTime object in the default zone.
* This differs from the similarly named method on DateTime, DateMidnight
* or MutableDateTime which retains the time zone. The difference is
* because Instant really represents a time <i>without</i> a zone,
* thus calling this method we really have no zone to 'retain' and
* hence expect to switch to the default zone.
* <p>
* This method definition preserves compatibility with earlier versions
* of Joda-Time.
*
* @return a DateTime using the same millis
*/
public DateTime toDateTime() {
return new DateTime(getMillis(), ISOChronology.getInstance());
}
/**
* Get this object as a DateTime using ISOChronology in the default zone.
* This method is identical to <code>toDateTime()</code>.
* <p>
* This method returns a DateTime object in the default zone.
* This differs from the similarly named method on DateTime, DateMidnight
* or MutableDateTime which retains the time zone. The difference is
* because Instant really represents a time <i>without</i> a zone,
* thus calling this method we really have no zone to 'retain' and
* hence expect to switch to the default zone.
* <p>
* This method is deprecated because it is a duplicate of {@link #toDateTime()}.
* However, removing it would cause the superclass implementation to be used,
* which would create silent bugs in any caller depending on this implementation.
* As such, the method itself is not currently planned to be removed.
* <p>
* This method definition preserves compatibility with earlier versions
* of Joda-Time.
*
* @return a DateTime using the same millis with ISOChronology
* @deprecated Use toDateTime() as it is identical
*/
@Deprecated
public DateTime toDateTimeISO() {
return toDateTime();
}
/**
* Get this object as a MutableDateTime using ISOChronology in the default zone.
* <p>
* This method returns a MutableDateTime object in the default zone.
* This differs from the similarly named method on DateTime, DateMidnight
* or MutableDateTime which retains the time zone. The difference is
* because Instant really represents a time <i>without</i> a zone,
* thus calling this method we really have no zone to 'retain' and
* hence expect to switch to the default zone.
* <p>
* This method definition preserves compatibility with earlier versions
* of Joda-Time.
*
* @return a MutableDateTime using the same millis
*/
public MutableDateTime toMutableDateTime() {
return new MutableDateTime(getMillis(), ISOChronology.getInstance());
}
/**
* Get this object as a MutableDateTime using ISOChronology in the default zone.
* This method is identical to <code>toMutableDateTime()</code>.
* <p>
* This method returns a MutableDateTime object in the default zone.
* This differs from the similarly named method on DateTime, DateMidnight
* or MutableDateTime which retains the time zone. The difference is
* because Instant really represents a time <i>without</i> a zone,
* thus calling this method we really have no zone to 'retain' and
* hence expect to switch to the default zone.
* <p>
* This method is deprecated because it is a duplicate of {@link #toMutableDateTime()}.
* However, removing it would cause the superclass implementation to be used,
* which would create silent bugs in any caller depending on this implementation.
* As such, the method itself is not currently planned to be removed.
* <p>
* This method definition preserves compatibility with earlier versions
* of Joda-Time.
*
* @return a MutableDateTime using the same millis with ISOChronology
* @deprecated Use toMutableDateTime() as it is identical
*/
@Deprecated
public MutableDateTime toMutableDateTimeISO() {
return toMutableDateTime();
}
}