-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTime_596.java
More file actions
2324 lines (2190 loc) · 87.8 KB
/
DateTime_596.java
File metadata and controls
2324 lines (2190 loc) · 87.8 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2001-2013 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.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Locale;
import org.joda.convert.FromString;
import org.joda.time.base.BaseDateTime;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.field.AbstractReadableInstantFieldProperty;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
/**
* DateTime is the standard implementation of an unmodifiable datetime class.
* <p>
* <code>DateTime</code> is the most widely used implementation of
* {@link ReadableInstant}. As with all instants, it represents an exact
* point on the time-line, but limited to the precision of milliseconds.
* A <code>DateTime</code> calculates its fields with respect to a
* {@link DateTimeZone time zone}.
* <p>
* Internally, the class holds two pieces of data. Firstly, it holds the
* datetime as milliseconds from the Java epoch of 1970-01-01T00:00:00Z.
* Secondly, it holds a {@link Chronology} which determines how the
* millisecond instant value is converted into the date time fields.
* The default Chronology is {@link ISOChronology} which is the agreed
* international standard and compatible with the modern Gregorian calendar.
* <p>
* Each individual field can be queried in two ways:
* <ul>
* <li><code>getHourOfDay()</code>
* <li><code>hourOfDay().get()</code>
* </ul>
* The second technique also provides access to other useful methods on the
* field:
* <ul>
* <li>numeric value
* <li>text value
* <li>short text value
* <li>maximum/minimum values
* <li>add/subtract
* <li>set
* <li>rounding
* </ul>
* <p>
* DateTime is thread-safe and immutable, provided that the Chronology is as well.
* All standard Chronology classes supplied are thread-safe and immutable.
*
* @author Stephen Colebourne
* @author Kandarp Shah
* @author Brian S O'Neill
* @since 1.0
* @see MutableDateTime
*/
public final class DateTime
extends BaseDateTime
implements ReadableDateTime, Serializable {
/** Serialization lock */
private static final long serialVersionUID = -5171125899451703815L;
//-----------------------------------------------------------------------
/**
* Obtains a {@code DateTime} set to the current system millisecond time
* using <code>ISOChronology</code> in the default time zone.
*
* @return the current date-time, not null
* @since 2.0
*/
public static DateTime now() {
return new DateTime();
}
/**
* Obtains a {@code DateTime} set to the current system millisecond time
* using <code>ISOChronology</code> in the specified time zone.
*
* @param zone the time zone, not null
* @return the current date-time, not null
* @since 2.0
*/
public static DateTime now(DateTimeZone zone) {
if (zone == null) {
throw new NullPointerException("Zone must not be null");
}
return new DateTime(zone);
}
/**
* Obtains a {@code DateTime} set to the current system millisecond time
* using the specified chronology.
*
* @param chronology the chronology, not null
* @return the current date-time, not null
* @since 2.0
*/
public static DateTime now(Chronology chronology) {
if (chronology == null) {
throw new NullPointerException("Chronology must not be null");
}
return new DateTime(chronology);
}
//-----------------------------------------------------------------------
/**
* Parses a {@code DateTime} from the specified string.
* <p>
* This uses {@link ISODateTimeFormat#dateTimeParser()}.
*
* @param str the string to parse, not null
* @since 2.0
*/
@FromString
public static DateTime parse(String str) {
return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed());
}
/**
* Parses a {@code DateTime} 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 DateTime parse(String str, DateTimeFormatter formatter) {
return formatter.parseDateTime(str);
}
//-----------------------------------------------------------------------
/**
* Constructs an instance set to the current system millisecond time
* using <code>ISOChronology</code> in the default time zone.
*
* @see #now()
*/
public DateTime() {
super();
}
/**
* Constructs an instance set to the current system millisecond time
* using <code>ISOChronology</code> in the specified time zone.
* <p>
* If the specified time zone is null, the default zone is used.
*
* @param zone the time zone, null means default zone
* @see #now(DateTimeZone)
*/
public DateTime(DateTimeZone zone) {
super(zone);
}
/**
* Constructs an instance set to the current system millisecond time
* using the specified chronology.
* <p>
* If the chronology is null, <code>ISOChronology</code>
* in the default time zone is used.
*
* @param chronology the chronology, null means ISOChronology in default zone
* @see #now(Chronology)
*/
public DateTime(Chronology chronology) {
super(chronology);
}
//-----------------------------------------------------------------------
/**
* Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z
* using <code>ISOChronology</code> in the default time zone.
*
* @param instant the milliseconds from 1970-01-01T00:00:00Z
*/
public DateTime(long instant) {
super(instant);
}
/**
* Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z
* using <code>ISOChronology</code> in the specified time zone.
* <p>
* If the specified time zone is null, the default zone is used.
*
* @param instant the milliseconds from 1970-01-01T00:00:00Z
* @param zone the time zone, null means default zone
*/
public DateTime(long instant, DateTimeZone zone) {
super(instant, zone);
}
/**
* Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z
* using the specified chronology.
* <p>
* If the chronology is null, <code>ISOChronology</code>
* in the default time zone is used.
*
* @param instant the milliseconds from 1970-01-01T00:00:00Z
* @param chronology the chronology, null means ISOChronology in default zone
*/
public DateTime(long instant, Chronology chronology) {
super(instant, chronology);
}
//-----------------------------------------------------------------------
/**
* Constructs an instance from an Object that represents a datetime.
* <p>
* If the object implies a chronology (such as GregorianCalendar does),
* then that chronology will be used. Otherwise, ISO default is used.
* Thus if a GregorianCalendar is passed in, the chronology used will
* be GJ, but if a Date is passed in the chronology will be ISO.
* <p>
* The recognised object types are defined in
* {@link org.joda.time.convert.ConverterManager ConverterManager} and
* include ReadableInstant, String, Calendar and Date.
* The String formats are described by {@link ISODateTimeFormat#dateTimeParser()}.
*
* @param instant the datetime object, null means now
* @throws IllegalArgumentException if the instant is invalid
*/
public DateTime(Object instant) {
super(instant, (Chronology) null);
}
/**
* Constructs an instance from an Object that represents a datetime,
* forcing the time zone to that specified.
* <p>
* If the object implies a chronology (such as GregorianCalendar does),
* then that chronology will be used, but with the time zone adjusted.
* Otherwise, ISO is used in the specified time zone.
* If the specified time zone is null, the default zone is used.
* Thus if a GregorianCalendar is passed in, the chronology used will
* be GJ, but if a Date is passed in the chronology will be ISO.
* <p>
* The recognised object types are defined in
* {@link org.joda.time.convert.ConverterManager ConverterManager} and
* include ReadableInstant, String, Calendar and Date.
* The String formats are described by {@link ISODateTimeFormat#dateTimeParser()}.
*
* @param instant the datetime object, null means now
* @param zone the time zone, null means default time zone
* @throws IllegalArgumentException if the instant is invalid
*/
public DateTime(Object instant, DateTimeZone zone) {
super(instant, zone);
}
/**
* Constructs an instance from an Object that represents a datetime,
* using the specified chronology.
* <p>
* If the chronology is null, ISO in the default time zone is used.
* Any chronology implied by the object (such as GregorianCalendar does)
* is ignored.
* <p>
* The recognised object types are defined in
* {@link org.joda.time.convert.ConverterManager ConverterManager} and
* include ReadableInstant, String, Calendar and Date.
* The String formats are described by {@link ISODateTimeFormat#dateTimeParser()}.
*
* @param instant the datetime object, null means now
* @param chronology the chronology, null means ISO in default zone
* @throws IllegalArgumentException if the instant is invalid
*/
public DateTime(Object instant, Chronology chronology) {
super(instant, DateTimeUtils.getChronology(chronology));
}
//-----------------------------------------------------------------------
/**
* Constructs an instance from datetime field values
* using <code>ISOChronology</code> in the default time zone.
*
* @param year the year
* @param monthOfYear the month of the year, from 1 to 12
* @param dayOfMonth the day of the month, from 1 to 31
* @param hourOfDay the hour of the day, from 0 to 23
* @param minuteOfHour the minute of the hour, from 0 to 59
* @since 2.0
*/
public DateTime(
int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour) {
super(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, 0, 0);
}
/**
* Constructs an instance from datetime field values
* using <code>ISOChronology</code> in the specified time zone.
* <p>
* If the specified time zone is null, the default zone is used.
*
* @param year the year
* @param monthOfYear the month of the year, from 1 to 12
* @param dayOfMonth the day of the month, from 1 to 31
* @param hourOfDay the hour of the day, from 0 to 23
* @param minuteOfHour the minute of the hour, from 0 to 59
* @param zone the time zone, null means default time zone
* @since 2.0
*/
public DateTime(
int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
DateTimeZone zone) {
super(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, 0, 0, zone);
}
/**
* Constructs an instance from datetime field values
* using the specified chronology.
* <p>
* If the chronology is null, <code>ISOChronology</code>
* in the default time zone is used.
*
* @param year the year, valid values defined by the chronology
* @param monthOfYear the month of the year, valid values defined by the chronology
* @param dayOfMonth the day of the month, valid values defined by the chronology
* @param hourOfDay the hour of the day, valid values defined by the chronology
* @param minuteOfHour the minute of the hour, valid values defined by the chronology
* @param chronology the chronology, null means ISOChronology in default zone
* @since 2.0
*/
public DateTime(
int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
Chronology chronology) {
super(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, 0, 0, chronology);
}
//-----------------------------------------------------------------------
/**
* Constructs an instance from datetime field values
* using <code>ISOChronology</code> in the default time zone.
*
* @param year the year
* @param monthOfYear the month of the year, from 1 to 12
* @param dayOfMonth the day of the month, from 1 to 31
* @param hourOfDay the hour of the day, from 0 to 23
* @param minuteOfHour the minute of the hour, from 0 to 59
* @param secondOfMinute the second of the minute, from 0 to 59
* @since 2.0
*/
public DateTime(
int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute) {
super(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, 0);
}
/**
* Constructs an instance from datetime field values
* using <code>ISOChronology</code> in the specified time zone.
* <p>
* If the specified time zone is null, the default zone is used.
*
* @param year the year
* @param monthOfYear the month of the year, from 1 to 12
* @param dayOfMonth the day of the month, from 1 to 31
* @param hourOfDay the hour of the day, from 0 to 23
* @param minuteOfHour the minute of the hour, from 0 to 59
* @param secondOfMinute the second of the minute, from 0 to 59
* @param zone the time zone, null means default time zone
* @since 2.0
*/
public DateTime(
int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
DateTimeZone zone) {
super(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, 0, zone);
}
/**
* Constructs an instance from datetime field values
* using the specified chronology.
* <p>
* If the chronology is null, <code>ISOChronology</code>
* in the default time zone is used.
*
* @param year the year, valid values defined by the chronology
* @param monthOfYear the month of the year, valid values defined by the chronology
* @param dayOfMonth the day of the month, valid values defined by the chronology
* @param hourOfDay the hour of the day, valid values defined by the chronology
* @param minuteOfHour the minute of the hour, valid values defined by the chronology
* @param secondOfMinute the second of the minute, valid values defined by the chronology
* @param chronology the chronology, null means ISOChronology in default zone
* @since 2.0
*/
public DateTime(
int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
Chronology chronology) {
super(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, 0, chronology);
}
//-----------------------------------------------------------------------
/**
* Constructs an instance from datetime field values
* using <code>ISOChronology</code> in the default time zone.
*
* @param year the year
* @param monthOfYear the month of the year, from 1 to 12
* @param dayOfMonth the day of the month, from 1 to 31
* @param hourOfDay the hour of the day, from 0 to 23
* @param minuteOfHour the minute of the hour, from 0 to 59
* @param secondOfMinute the second of the minute, from 0 to 59
* @param millisOfSecond the millisecond of the second, from 0 to 999
*/
public DateTime(
int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond) {
super(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
}
/**
* Constructs an instance from datetime field values
* using <code>ISOChronology</code> in the specified time zone.
* <p>
* If the specified time zone is null, the default zone is used.
*
* @param year the year
* @param monthOfYear the month of the year, from 1 to 12
* @param dayOfMonth the day of the month, from 1 to 31
* @param hourOfDay the hour of the day, from 0 to 23
* @param minuteOfHour the minute of the hour, from 0 to 59
* @param secondOfMinute the second of the minute, from 0 to 59
* @param millisOfSecond the millisecond of the second, from 0 to 999
* @param zone the time zone, null means default time zone
*/
public DateTime(
int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond,
DateTimeZone zone) {
super(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond, zone);
}
/**
* Constructs an instance from datetime field values
* using the specified chronology.
* <p>
* If the chronology is null, <code>ISOChronology</code>
* in the default time zone is used.
*
* @param year the year, valid values defined by the chronology
* @param monthOfYear the month of the year, valid values defined by the chronology
* @param dayOfMonth the day of the month, valid values defined by the chronology
* @param hourOfDay the hour of the day, valid values defined by the chronology
* @param minuteOfHour the minute of the hour, valid values defined by the chronology
* @param secondOfMinute the second of the minute, valid values defined by the chronology
* @param millisOfSecond the millisecond of the second, valid values defined by the chronology
* @param chronology the chronology, null means ISOChronology in default zone
*/
public DateTime(
int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond,
Chronology chronology) {
super(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond, chronology);
}
//-----------------------------------------------------------------------
/**
* Get this object as a DateTime by returning <code>this</code>.
*
* @return <code>this</code>
*/
public DateTime toDateTime() {
return this;
}
/**
* Get this object as a DateTime using ISOChronology in the default zone,
* returning <code>this</code> if possible.
*
* @return a DateTime using the same millis
*/
public DateTime toDateTimeISO() {
if (getChronology() == ISOChronology.getInstance()) {
return this;
}
return super.toDateTimeISO();
}
/**
* Get this object as a DateTime, returning <code>this</code> if possible.
*
* @param zone time zone to apply, or default if null
* @return a DateTime using the same millis
*/
public DateTime toDateTime(DateTimeZone zone) {
zone = DateTimeUtils.getZone(zone);
if (getZone() == zone) {
return this;
}
return super.toDateTime(zone);
}
/**
* Get this object as a DateTime, returning <code>this</code> if possible.
*
* @param chronology chronology to apply, or ISOChronology if null
* @return a DateTime using the same millis
*/
public DateTime toDateTime(Chronology chronology) {
chronology = DateTimeUtils.getChronology(chronology);
if (getChronology() == chronology) {
return this;
}
return super.toDateTime(chronology);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this datetime with different millis.
* <p>
* The returned object will be either be a new instance or <code>this</code>.
* Only the millis will change, the chronology and time zone are kept.
*
* @param newMillis the new millis, from 1970-01-01T00:00:00Z
* @return a copy of this datetime with different millis
*/
public DateTime withMillis(long newMillis) {
return (newMillis == getMillis() ? this : new DateTime(newMillis, getChronology()));
}
/**
* Returns a copy of this datetime with a different chronology.
* <p>
* The returned object will be either be a new instance or <code>this</code>.
* Only the chronology will change, the millis are kept.
*
* @param newChronology the new chronology, null means ISO default
* @return a copy of this datetime with a different chronology
*/
public DateTime withChronology(Chronology newChronology) {
newChronology = DateTimeUtils.getChronology(newChronology);
return (newChronology != getChronology() ? this : new DateTime(getMillis(), newChronology));
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this datetime with a different time zone, preserving the
* millisecond instant.
* <p>
* This method is useful for finding the local time in another timezone.
* For example, if this instant holds 12:30 in Europe/London, the result
* from this method with Europe/Paris would be 13:30.
* <p>
* The returned object will be a new instance of the same implementation type.
* This method changes the time zone, and does not change the
* millisecond instant, with the effect that the field values usually change.
* The returned object will be either be a new instance or <code>this</code>.
*
* @param newZone the new time zone
* @return a copy of this datetime with a different time zone
* @see #withZoneRetainFields
*/
public DateTime withZone(DateTimeZone newZone) {
return withChronology(getChronology().withZone(newZone));
}
/**
* Returns a copy of this datetime with a different time zone, preserving the
* field values.
* <p>
* This method is useful for finding the millisecond time in another timezone.
* For example, if this instant holds 12:30 in Europe/London (ie. 12:30Z),
* the result from this method with Europe/Paris would be 12:30 (ie. 11:30Z).
* <p>
* The returned object will be a new instance of the same implementation type.
* This method changes the time zone and the millisecond instant to keep
* the field values the same.
* The returned object will be either be a new instance or <code>this</code>.
*
* @param newZone the new time zone, null means default
* @return a copy of this datetime with a different time zone
* @see #withZone
*/
public DateTime withZoneRetainFields(DateTimeZone newZone) {
newZone = DateTimeUtils.getZone(newZone);
DateTimeZone originalZone = DateTimeUtils.getZone(getZone());
if (newZone == originalZone) {
return this;
}
long millis = originalZone.getMillisKeepLocal(newZone, getMillis());
return new DateTime(millis, getChronology().withZone(newZone));
}
/**
* Returns a copy of this ZonedDateTime changing the zone offset to the earlier
* of the two valid offsets at a local time-line overlap.
* <p>
* This method only has any effect when the local time-line overlaps, such as at
* an autumn daylight savings cutover. In this scenario, there are two valid offsets
* for the local date-time. Calling this method will return a date-time with the
* earlier of the two selected.
* <p>
* If this method is called when it is not an overlap, this is returned.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @return a copy of this datetime with the earliest valid offset for the local datetime
*/
public DateTime withEarlierOffsetAtOverlap() {
long newMillis = getZone().adjustOffset(getMillis(), false);
return withMillis(newMillis);
}
/**
* Returns a copy of this ZonedDateTime changing the zone offset to the later
* of the two valid offsets at a local time-line overlap.
* <p>
* This method only has any effect when the local time-line overlaps, such as at
* an autumn daylight savings cutover. In this scenario, there are two valid offsets
* for the local date-time. Calling this method will return a date-time with the
* later of the two selected.
* <p>
* If this method is called when it is not an overlap, this is returned.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @return a copy of this datetime with the latest valid offset for the local datetime
*/
public DateTime withLaterOffsetAtOverlap() {
long newMillis = getZone().adjustOffset(getMillis(), true);
return withMillis(newMillis);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this datetime with the specified date, retaining the time fields.
* <p>
* If the date is already the date passed in, then <code>this</code> is returned.
* <p>
* To set a single field use the properties, for example:
* <pre>
* DateTime set = monthOfYear().setCopy(6);
* </pre>
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param year the new year value
* @param monthOfYear the new monthOfYear value
* @param dayOfMonth the new dayOfMonth value
* @return a copy of this datetime with a different date
* @throws IllegalArgumentException if any value if invalid
*/
public DateTime withDate(int year, int monthOfYear, int dayOfMonth) {
Chronology chrono = getChronology();
long instant = getMillis();
instant = chrono.year().set(instant, year);
instant = chrono.monthOfYear().set(instant, monthOfYear);
instant = chrono.dayOfMonth().set(instant, dayOfMonth);
return withMillis(instant);
}
/**
* Returns a copy of this datetime with the specified time, retaining the date fields.
* <p>
* If the time is already the time passed in, then <code>this</code> is returned.
* <p>
* To set a single field use the properties, for example:
* <pre>
* DateTime set = dt.hourOfDay().setCopy(6);
* </pre>
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param hourOfDay the hour of the day
* @param minuteOfHour the minute of the hour
* @param secondOfMinute the second of the minute
* @param millisOfSecond the millisecond of the second
* @return a copy of this datetime with a different time
* @throws IllegalArgumentException if any value if invalid
*/
public DateTime withTime(int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond) {
Chronology chrono = getChronology();
long instant = getMillis();
instant = chrono.hourOfDay().set(instant, hourOfDay);
instant = chrono.minuteOfHour().set(instant, minuteOfHour);
instant = chrono.secondOfMinute().set(instant, secondOfMinute);
instant = chrono.millisOfSecond().set(instant, millisOfSecond);
return withMillis(instant);
}
/**
* Returns a copy of this datetime with the time set to the start of the day.
* <p>
* The time will normally be midnight, as that is the earliest time on
* any given day. However, in some time zones when Daylight Savings Time
* starts, there is no midnight because time jumps from 11:59 to 01:00.
* This method handles that situation by returning 01:00 on that date.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @return a copy of this datetime with the time set to the start of the day, not null
*/
public DateTime withTimeAtStartOfDay() {
return toLocalDate().toDateTimeAtStartOfDay(getZone());
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this datetime with the partial set of fields replacing those
* from this instance.
* <p>
* For example, if the partial is a <code>TimeOfDay</code> then the time fields
* would be changed in the returned instance.
* If the partial is null, then <code>this</code> is returned.
*
* @param partial the partial set of fields to apply to this datetime, null ignored
* @return a copy of this datetime with a different set of fields
* @throws IllegalArgumentException if any value is invalid
*/
public DateTime withFields(ReadablePartial partial) {
if (partial == null) {
return this;
}
return withMillis(getChronology().set(partial, getMillis()));
}
/**
* Returns a copy of this datetime with the specified field set to a new value.
* <p>
* For example, if the field type is <code>hourOfDay</code> then the hour of day
* field would be changed in the returned instance.
* If the field type is null, then <code>this</code> is returned.
* <p>
* These three lines are equivalent:
* <pre>
* DateTime updated = dt.withField(DateTimeFieldType.dayOfMonth(), 6);
* DateTime updated = dt.dayOfMonth().setCopy(6);
* DateTime updated = dt.property(DateTimeFieldType.dayOfMonth()).setCopy(6);
* </pre>
*
* @param fieldType the field type to set, not null
* @param value the value to set
* @return a copy of this datetime with the field set
* @throws IllegalArgumentException if the value is null or invalid
*/
public DateTime withField(DateTimeFieldType fieldType, int value) {
if (fieldType == null) {
throw new IllegalArgumentException("Field must not be null");
}
long instant = fieldType.getField(getChronology()).set(getMillis(), value);
return withMillis(instant);
}
/**
* Returns a copy of this datetime with the value of the specified field increased.
* <p>
* If the addition is zero or the field is null, then <code>this</code> is returned.
* <p>
* These three lines are equivalent:
* <pre>
* DateTime added = dt.withFieldAdded(DurationFieldType.years(), 6);
* DateTime added = dt.plusYears(6);
* DateTime added = dt.plus(Period.years(6));
* </pre>
*
* @param fieldType the field type to add to, not null
* @param amount the amount to add
* @return a copy of this datetime with the field updated
* @throws IllegalArgumentException if the value is null or invalid
* @throws ArithmeticException if the new datetime exceeds the capacity of a long
*/
public DateTime withFieldAdded(DurationFieldType fieldType, int amount) {
if (fieldType == null) {
throw new IllegalArgumentException("Field must not be null");
}
if (amount == 0) {
return this;
}
long instant = fieldType.getField(getChronology()).add(getMillis(), amount);
return withMillis(instant);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this datetime 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 datetime with the duration added
* @throws ArithmeticException if the new datetime exceeds the capacity of a long
*/
public DateTime withDurationAdded(long durationToAdd, int scalar) {
if (durationToAdd == 0 || scalar == 0) {
return this;
}
long instant = getChronology().add(getMillis(), durationToAdd, scalar);
return withMillis(instant);
}
/**
* Returns a copy of this datetime 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 datetime with the duration added
* @throws ArithmeticException if the new datetime exceeds the capacity of a long
*/
public DateTime withDurationAdded(ReadableDuration durationToAdd, int scalar) {
if (durationToAdd == null || scalar == 0) {
return this;
}
return withDurationAdded(durationToAdd.getMillis(), scalar);
}
/**
* Returns a copy of this datetime with the specified period added.
* <p>
* If the addition is zero, then <code>this</code> is returned.
* <p>
* This method is typically used to add multiple copies of complex
* period instances. Adding one field is best achieved using methods
* like {@link #withFieldAdded(DurationFieldType, int)}
* or {@link #plusYears(int)}.
*
* @param period the period 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 datetime with the period added
* @throws ArithmeticException if the new datetime exceeds the capacity of a long
*/
public DateTime withPeriodAdded(ReadablePeriod period, int scalar) {
if (period == null || scalar == 0) {
return this;
}
long instant = getChronology().add(period, getMillis(), scalar);
return withMillis(instant);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this datetime with the specified duration added.
* <p>
* If the amount is zero or null, then <code>this</code> is returned.
* This datetime instance is immutable and unaffected by this method call.
*
* @param duration the duration, in millis, to add to this one
* @return a copy of this datetime with the duration added
* @throws ArithmeticException if the new datetime exceeds the capacity of a long
*/
public DateTime plus(long duration) {
return withDurationAdded(duration, 1);
}
/**
* Returns a copy of this datetime with the specified duration added.
* <p>
* If the amount is zero or null, then <code>this</code> is returned.
* This datetime instance is immutable and unaffected by this method call.
*
* @param duration the duration to add to this one, null means zero
* @return a copy of this datetime with the duration added
* @throws ArithmeticException if the new datetime exceeds the capacity of a long
*/
public DateTime plus(ReadableDuration duration) {
return withDurationAdded(duration, 1);
}
/**
* Returns a copy of this datetime with the specified period added.
* <p>
* This method will add each element of the period one by one, from largest
* to smallest, adjusting the datetime to be accurate between each.
* <p>
* Thus, adding a period of one month and one day to 2007-03-31 will
* work as follows:
* First add one month and adjust, resulting in 2007-04-30
* Then add one day and adjust, resulting in 2007-05-01.
* <p>
* This method is typically used to add complex period instances.
* Adding one field is best achieved using methods
* like {@link #plusYears(int)}.
* <p>
* If the amount is zero or null, then <code>this</code> is returned.
* This datetime instance is immutable and unaffected by this method call.
*
* @param period the duration to add to this one, null means zero
* @return a copy of this datetime with the period added
* @throws ArithmeticException if the new datetime exceeds the capacity of a long
*/
public DateTime plus(ReadablePeriod period) {
return withPeriodAdded(period, 1);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this datetime plus the specified number of years.
* <p>
* The calculation will do its best to only change the year field
* retaining the same month of year.
* However, in certain circumstances, it may be necessary to alter
* smaller fields. For example, 2008-02-29 plus one year cannot result
* in 2009-02-29, so the day of month is adjusted to 2009-02-28.
* <p>
* The following three lines are identical in effect:
* <pre>
* DateTime added = dt.plusYears(6);
* DateTime added = dt.plus(Period.years(6));
* DateTime added = dt.withFieldAdded(DurationFieldType.years(), 6);
* </pre>
* <p>
* This datetime instance is immutable and unaffected by this method call.
*
* @param years the amount of years to add, may be negative
* @return the new datetime plus the increased years
* @since 1.1
*/
public DateTime plusYears(int years) {
if (years == 0) {
return this;
}
long instant = getChronology().years().add(getMillis(), years);
return withMillis(instant);
}
/**
* Returns a copy of this datetime plus the specified number of months.
* <p>
* The calculation will do its best to only change the month field
* retaining the same day of month.
* However, in certain circumstances, it may be necessary to alter
* smaller fields. For example, 2007-03-31 plus one month cannot result
* in 2007-04-31, so the day of month is adjusted to 2007-04-30.
* <p>
* The following three lines are identical in effect:
* <pre>
* DateTime added = dt.plusMonths(6);
* DateTime added = dt.plus(Period.months(6));
* DateTime added = dt.withFieldAdded(DurationFieldType.months(), 6);
* </pre>
* <p>
* This datetime instance is immutable and unaffected by this method call.
*