-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutableDateTime_639.java
More file actions
1486 lines (1372 loc) · 50.1 KB
/
MutableDateTime_639.java
File metadata and controls
1486 lines (1372 loc) · 50.1 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.convert.ToString;
import org.joda.time.base.BaseDateTime;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.field.AbstractReadableInstantFieldProperty;
import org.joda.time.field.FieldUtils;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
/**
* MutableDateTime is the standard implementation of a modifiable datetime class.
* It holds the datetime as milliseconds from the Java epoch of 1970-01-01T00:00:00Z.
* <p>
* This class uses a Chronology internally. The Chronology determines how the
* millisecond instant value is converted into the date time fields.
* The default Chronology is <code>ISOChronology</code> which is the agreed
* international standard and compatible with the modern Gregorian calendar.
* <p>
* Each individual field can be accessed 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>get numeric value
* <li>set numeric value
* <li>add to numeric value
* <li>add to numeric value wrapping with the field
* <li>get text value
* <li>get short text value
* <li>set text value
* <li>field maximum value
* <li>field minimum value
* </ul>
*
* <p>
* MutableDateTime is mutable and not thread-safe, unless concurrent threads
* are not invoking mutator methods.
*
* @author Guy Allard
* @author Brian S O'Neill
* @author Stephen Colebourne
* @author Mike Schrag
* @since 1.0
* @see DateTime
*/
public class MutableDateTime
extends BaseDateTime
implements ReadWritableDateTime, Cloneable, Serializable {
/** Serialization version */
private static final long serialVersionUID = 2852608688135209575L;
/** Rounding is disabled */
public static final int ROUND_NONE = 0;
/** Rounding mode as described by {@link DateTimeField#roundFloor} */
public static final int ROUND_FLOOR = 1;
/** Rounding mode as described by {@link DateTimeField#roundCeiling} */
public static final int ROUND_CEILING = 2;
/** Rounding mode as described by {@link DateTimeField#roundHalfFloor} */
public static final int ROUND_HALF_FLOOR = 3;
/** Rounding mode as described by {@link DateTimeField#roundHalfCeiling} */
public static final int ROUND_HALF_CEILING = 4;
/** Rounding mode as described by {@link DateTimeField#roundHalfEven} */
public static final int ROUND_HALF_EVEN = 5;
/** The field to round on */
private DateTimeField iRoundingField;
/** The mode of rounding */
private int iRoundingMode;
//-----------------------------------------------------------------------
/**
* Obtains a {@code MutableDateTime} 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 MutableDateTime now() {
return new MutableDateTime();
}
/**
* Obtains a {@code MutableDateTime} 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 MutableDateTime now(DateTimeZone zone) {
if (zone == null) {
throw new NullPointerException("Zone must not be null");
}
return new MutableDateTime(zone);
}
/**
* Obtains a {@code MutableDateTime} 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 MutableDateTime now(Chronology chronology) {
if (chronology == null) {
throw new NullPointerException("Chronology must not be null");
}
return new MutableDateTime(chronology);
}
//-----------------------------------------------------------------------
/**
* Parses a {@code MutableDateTime} from the specified string.
* <p>
* This uses {@link ISODateTimeFormat#dateTimeParser()}.
*
* @param str the string to parse, not null
* @since 2.0
*/
@FromString
public static MutableDateTime parse(String str) {
return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed());
}
/**
* Parses a {@code MutableDateTime} 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 MutableDateTime parse(String str, DateTimeFormatter formatter) {
return formatter.parseDateTime(str).toMutableDateTime();
}
//-----------------------------------------------------------------------
/**
* Constructs an instance set to the current system millisecond time
* using <code>ISOChronology</code> in the default time zone.
*
* @see #now()
*/
public MutableDateTime() {
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 MutableDateTime(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 MutableDateTime(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 MutableDateTime(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 MutableDateTime(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 MutableDateTime(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.
*
* @param instant the datetime object, null means now
* @throws IllegalArgumentException if the instant is invalid
*/
public MutableDateTime(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.
*
* @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 MutableDateTime(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.
*
* @param instant the datetime object, null means now
* @param chronology the chronology, null means ISOChronology in default zone
* @throws IllegalArgumentException if the instant is invalid
*/
public MutableDateTime(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
* @param dayOfMonth the day of the month
* @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
*/
public MutableDateTime(
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
* @param dayOfMonth the day of the month
* @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
* @param zone the time zone, null means default time zone
*/
public MutableDateTime(
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
* @param monthOfYear the month of the year
* @param dayOfMonth the day of the month
* @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
* @param chronology the chronology, null means ISOChronology in default zone
*/
public MutableDateTime(
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);
}
//-----------------------------------------------------------------------
/**
* Gets the field used for rounding this instant, returning null if rounding
* is not enabled.
*
* @return the rounding field
*/
public DateTimeField getRoundingField() {
return iRoundingField;
}
/**
* Gets the rounding mode for this instant, returning ROUND_NONE if rounding
* is not enabled.
*
* @return the rounding mode constant
*/
public int getRoundingMode() {
return iRoundingMode;
}
/**
* Sets the status of rounding to use the specified field and ROUND_FLOOR mode.
* A null field will disable rounding.
* Once set, the instant is then rounded using the new field and mode.
* <p>
* Enabling rounding will cause all subsequent calls to {@link #setMillis(long)}
* to be rounded. This can be used to control the precision of the instant,
* for example by setting a rounding field of minuteOfDay, the seconds and
* milliseconds will always be zero.
*
* @param field rounding field or null to disable
*/
public void setRounding(DateTimeField field) {
setRounding(field, MutableDateTime.ROUND_FLOOR);
}
/**
* Sets the status of rounding to use the specified field and mode.
* A null field or mode of ROUND_NONE will disable rounding.
* Once set, the instant is then rounded using the new field and mode.
* <p>
* Enabling rounding will cause all subsequent calls to {@link #setMillis(long)}
* to be rounded. This can be used to control the precision of the instant,
* for example by setting a rounding field of minuteOfDay, the seconds and
* milliseconds will always be zero.
*
* @param field rounding field or null to disable
* @param mode rounding mode or ROUND_NONE to disable
* @throws IllegalArgumentException if mode is unknown, no exception if field is null
*/
public void setRounding(DateTimeField field, int mode) {
if (field != null && (mode < ROUND_NONE || mode > ROUND_HALF_EVEN)) {
throw new IllegalArgumentException("Illegal rounding mode: " + mode);
}
iRoundingField = (mode == ROUND_NONE ? null : field);
iRoundingMode = (field == null ? ROUND_NONE : mode);
setMillis(getMillis());
}
//-----------------------------------------------------------------------
/**
* Set the milliseconds of the datetime.
* <p>
* All changes to the millisecond field occurs via this method.
*
* @param instant the milliseconds since 1970-01-01T00:00:00Z to set the
* datetime to
*/
public void setMillis(long instant) {
switch (iRoundingMode) {
case ROUND_NONE:
break;
case ROUND_FLOOR:
instant = iRoundingField.roundFloor(instant);
break;
case ROUND_CEILING:
instant = iRoundingField.roundCeiling(instant);
break;
case ROUND_HALF_FLOOR:
instant = iRoundingField.roundHalfFloor(instant);
break;
case ROUND_HALF_CEILING:
instant = iRoundingField.roundHalfCeiling(instant);
break;
case ROUND_HALF_EVEN:
instant = iRoundingField.roundHalfEven(instant);
break;
}
super.setMillis(instant);
}
/**
* Sets the millisecond instant of this instant from another.
* <p>
* This method does not change the chronology of this instant, just the
* millisecond instant.
*
* @param instant the instant to use, null means now
*/
public void setMillis(ReadableInstant instant) {
long instantMillis = DateTimeUtils.getInstantMillis(instant);
setMillis(instantMillis); // set via this class not super
}
//-----------------------------------------------------------------------
/**
* Add an amount of time to the datetime.
*
* @param duration the millis to add
* @throws ArithmeticException if the result exceeds the capacity of the instant
*/
public void add(long duration) {
setMillis(FieldUtils.safeAdd(getMillis(), duration)); // set via this class not super
}
/**
* Adds a duration to this instant.
* <p>
* This will typically change the value of most fields.
*
* @param duration the duration to add, null means add zero
* @throws ArithmeticException if the result exceeds the capacity of the instant
*/
public void add(ReadableDuration duration) {
add(duration, 1);
}
/**
* Adds a duration to this instant specifying how many times to add.
* <p>
* This will typically change the value of most fields.
*
* @param duration the duration to add, null means add zero
* @param scalar direction and amount to add, which may be negative
* @throws ArithmeticException if the result exceeds the capacity of the instant
*/
public void add(ReadableDuration duration, int scalar) {
if (duration != null) {
add(FieldUtils.safeMultiply(duration.getMillis(), scalar));
}
}
/**
* Adds a period to this instant.
* <p>
* This will typically change the value of most fields.
*
* @param period the period to add, null means add zero
* @throws ArithmeticException if the result exceeds the capacity of the instant
*/
public void add(ReadablePeriod period) {
add(period, 1);
}
/**
* Adds a period to this instant specifying how many times to add.
* <p>
* This will typically change the value of most fields.
*
* @param period the period to add, null means add zero
* @param scalar direction and amount to add, which may be negative
* @throws ArithmeticException if the result exceeds the capacity of the instant
*/
public void add(ReadablePeriod period, int scalar) {
if (period != null) {
setMillis(getChronology().add(period, getMillis(), scalar)); // set via this class not super
}
}
//-----------------------------------------------------------------------
/**
* Set the chronology of the datetime.
* <p>
* All changes to the chronology occur via this method.
*
* @param chronology the chronology to use, null means ISOChronology in default zone
*/
public void setChronology(Chronology chronology) {
super.setChronology(chronology);
}
//-----------------------------------------------------------------------
/**
* Sets the time zone of the datetime, changing the chronology and field values.
* <p>
* Changing the zone using this method retains the millisecond instant.
* The millisecond instant is adjusted in the new zone to compensate.
*
* chronology. Setting the time zone does not affect the millisecond value
* of this instant.
* <p>
* If the chronology already has this time zone, no change occurs.
*
* @param newZone the time zone to use, null means default zone
* @see #setZoneRetainFields
*/
public void setZone(DateTimeZone newZone) {
newZone = DateTimeUtils.getZone(newZone);
Chronology chrono = getChronology();
if (chrono.getZone() != newZone) {
setChronology(chrono.withZone(newZone)); // set via this class not super
}
}
/**
* Sets the time zone of the datetime, changing the chronology and millisecond.
* <p>
* Changing the zone using this method retains the field values.
* The millisecond instant is adjusted in the new zone to compensate.
* <p>
* If the chronology already has this time zone, no change occurs.
*
* @param newZone the time zone to use, null means default zone
* @see #setZone
*/
public void setZoneRetainFields(DateTimeZone newZone) {
newZone = DateTimeUtils.getZone(newZone);
DateTimeZone originalZone = DateTimeUtils.getZone(getZone());
if (newZone == originalZone) {
return;
}
long millis = originalZone.getMillisKeepLocal(newZone, getMillis());
setChronology(getChronology().withZone(newZone)); // set via this class not super
setMillis(millis);
}
//-----------------------------------------------------------------------
/**
* Sets the value of one of the fields of the instant, such as hourOfDay.
*
* @param type a field type, usually obtained from DateTimeFieldType, not null
* @param value the value to set the field to
* @throws IllegalArgumentException if the value is null or invalid
*/
public void set(DateTimeFieldType type, int value) {
if (type == null) {
throw new IllegalArgumentException("Field must not be null");
}
setMillis(type.getField(getChronology()).set(getMillis(), value));
}
/**
* Adds to the instant specifying the duration and multiple to add.
*
* @param type a field type, usually obtained from DateTimeFieldType, not null
* @param amount the amount to add of this duration
* @throws IllegalArgumentException if the value is null or invalid
* @throws ArithmeticException if the result exceeds the capacity of the instant
*/
public void add(DurationFieldType type, int amount) {
if (type == null) {
throw new IllegalArgumentException("Field must not be null");
}
if (amount >= 0) {
setMillis(type.getField(getChronology()).add(getMillis(), amount));
}
}
//-----------------------------------------------------------------------
/**
* Set the year to the specified value.
*
* @param year the year
* @throws IllegalArgumentException if the value is invalid
*/
public void setYear(final int year) {
setMillis(getChronology().year().set(getMillis(), year));
}
/**
* Add a number of years to the date.
*
* @param years the years to add
* @throws IllegalArgumentException if the value is invalid
*/
public void addYears(final int years) {
if (years != 0) {
setMillis(getChronology().years().add(getMillis(), years));
}
}
//-----------------------------------------------------------------------
/**
* Set the weekyear to the specified value.
*
* @param weekyear the weekyear
* @throws IllegalArgumentException if the value is invalid
*/
public void setWeekyear(final int weekyear) {
setMillis(getChronology().weekyear().set(getMillis(), weekyear));
}
/**
* Add a number of weekyears to the date.
*
* @param weekyears the weekyears to add
* @throws IllegalArgumentException if the value is invalid
*/
public void addWeekyears(final int weekyears) {
if (weekyears != 0) {
setMillis(getChronology().weekyears().add(getMillis(), weekyears));
}
}
//-----------------------------------------------------------------------
/**
* Set the month of the year to the specified value.
*
* @param monthOfYear the month of the year
* @throws IllegalArgumentException if the value is invalid
*/
public void setMonthOfYear(final int monthOfYear) {
setMillis(getChronology().monthOfYear().set(getMillis(), monthOfYear));
}
/**
* Add a number of months to the date.
*
* @param months the months to add
* @throws IllegalArgumentException if the value is invalid
*/
public void addMonths(final int months) {
if (months != 0) {
setMillis(getChronology().months().add(getMillis(), months));
}
}
//-----------------------------------------------------------------------
/**
* Set the week of weekyear to the specified value.
*
* @param weekOfWeekyear the week of the weekyear
* @throws IllegalArgumentException if the value is invalid
*/
public void setWeekOfWeekyear(final int weekOfWeekyear) {
setMillis(getChronology().weekOfWeekyear().set(getMillis(), weekOfWeekyear));
}
/**
* Add a number of weeks to the date.
*
* @param weeks the weeks to add
* @throws IllegalArgumentException if the value is invalid
*/
public void addWeeks(final int weeks) {
if (weeks != 0) {
setMillis(getChronology().weeks().add(getMillis(), weeks));
}
}
//-----------------------------------------------------------------------
/**
* Set the day of year to the specified value.
*
* @param dayOfYear the day of the year
* @throws IllegalArgumentException if the value is invalid
*/
public void setDayOfYear(final int dayOfYear) {
setMillis(getChronology().dayOfYear().set(getMillis(), dayOfYear));
}
/**
* Set the day of the month to the specified value.
*
* @param dayOfMonth the day of the month
* @throws IllegalArgumentException if the value is invalid
*/
public void setDayOfMonth(final int dayOfMonth) {
setMillis(getChronology().dayOfMonth().set(getMillis(), dayOfMonth));
}
/**
* Set the day of week to the specified value.
*
* @param dayOfWeek the day of the week
* @throws IllegalArgumentException if the value is invalid
*/
public void setDayOfWeek(final int dayOfWeek) {
setMillis(getChronology().dayOfWeek().set(getMillis(), dayOfWeek));
}
/**
* Add a number of days to the date.
*
* @param days the days to add
* @throws IllegalArgumentException if the value is invalid
*/
public void addDays(final int days) {
if (days != 0) {
setMillis(getChronology().days().add(getMillis(), days));
}
}
//-----------------------------------------------------------------------
/**
* Set the hour of the day to the specified value.
*
* @param hourOfDay the hour of day
* @throws IllegalArgumentException if the value is invalid
*/
public void setHourOfDay(final int hourOfDay) {
setMillis(getChronology().hourOfDay().set(getMillis(), hourOfDay));
}
/**
* Add a number of hours to the date.
*
* @param hours the hours to add
* @throws IllegalArgumentException if the value is invalid
*/
public void addHours(final int hours) {
if (hours != 0) {
setMillis(getChronology().hours().add(getMillis(), hours));
}
}
//-----------------------------------------------------------------------
/**
* Set the minute of the day to the specified value.
*
* @param minuteOfDay the minute of day
* @throws IllegalArgumentException if the value is invalid
*/
public void setMinuteOfDay(final int minuteOfDay) {
setMillis(getChronology().minuteOfDay().set(getMillis(), minuteOfDay));
}
/**
* Set the minute of the hour to the specified value.
*
* @param minuteOfHour the minute of hour
* @throws IllegalArgumentException if the value is invalid
*/
public void setMinuteOfHour(final int minuteOfHour) {
setMillis(getChronology().minuteOfHour().set(getMillis(), minuteOfHour));
}
/**
* Add a number of minutes to the date.
*
* @param minutes the minutes to add
* @throws IllegalArgumentException if the value is invalid
*/
public void addMinutes(final int minutes) {
if (minutes != 0) {
setMillis(getChronology().minutes().add(getMillis(), minutes));
}
}
//-----------------------------------------------------------------------
/**
* Set the second of the day to the specified value.
*
* @param secondOfDay the second of day
* @throws IllegalArgumentException if the value is invalid
*/
public void setSecondOfDay(final int secondOfDay) {
setMillis(getChronology().secondOfDay().set(getMillis(), secondOfDay));
}
/**
* Set the second of the minute to the specified value.
*
* @param secondOfMinute the second of minute
* @throws IllegalArgumentException if the value is invalid
*/
public void setSecondOfMinute(final int secondOfMinute) {
setMillis(getChronology().secondOfMinute().set(getMillis(), secondOfMinute));
}
/**
* Add a number of seconds to the date.
*
* @param seconds the seconds to add
* @throws IllegalArgumentException if the value is invalid
*/
public void addSeconds(final int seconds) {
if (seconds != 0) {
setMillis(getChronology().seconds().add(getMillis(), seconds));
}
}
//-----------------------------------------------------------------------
/**
* Set the millis of the day to the specified value.
*
* @param millisOfDay the millis of day
* @throws IllegalArgumentException if the value is invalid
*/
public void setMillisOfDay(final int millisOfDay) {
setMillis(getChronology().millisOfDay().set(getMillis(), millisOfDay));
}
/**
* Set the millis of the second to the specified value.
*
* @param millisOfSecond the millis of second
* @throws IllegalArgumentException if the value is invalid
*/
public void setMillisOfSecond(final int millisOfSecond) {
setMillis(getChronology().millisOfSecond().set(getMillis(), millisOfSecond));
}
/**
* Add a number of milliseconds to the date. The implementation of this
* method differs from the {@link #add(long)} method in that a
* DateTimeField performs the addition.
*
* @param millis the milliseconds to add
* @throws IllegalArgumentException if the value is invalid
*/
public void addMillis(final int millis) {
if (millis != 0) {
setMillis(getChronology().millis().add(getMillis(), millis));
}
}
//-----------------------------------------------------------------------
/**
* Set the date from milliseconds.
* The time part of this object will be unaffected.
*
* @param instant an instant to copy the date from, time part ignored
* @throws IllegalArgumentException if the value is invalid
*/
public void setDate(final long instant) {
setMillis(getChronology().millisOfDay().set(instant, getMillisOfDay()));
}
/**
* Set the date from another instant.
* The time part of this object will be unaffected.
* <p>
* If the input is a {@code ReadableDateTime} then it is converted to the
* same time-zone as this object before using the instant millis.
*
* @param instant an instant to copy the date from, time part ignored
* @throws IllegalArgumentException if the object is invalid
*/
public void setDate(final ReadableInstant instant) {
long instantMillis = DateTimeUtils.getInstantMillis(instant);
if (instant instanceof ReadableDateTime) {
ReadableDateTime rdt = (ReadableDateTime) instant;
Chronology instantChrono = DateTimeUtils.getChronology(rdt.getChronology());
DateTimeZone zone = instantChrono.getZone();
if (zone != null) {
instantMillis = zone.getMillisKeepLocal(getZone(), instantMillis);
}
}
setDate(instantMillis);
}
/**
* Set the date from fields.
* The time part of this object will be unaffected.
*
* @param year the year
* @param monthOfYear the month of the year
* @param dayOfMonth the day of the month
* @throws IllegalArgumentException if the value is invalid
*/
public void setDate(
final int year,
final int monthOfYear,
final int dayOfMonth) {
Chronology c = getChronology();
long instantMidnight = c.getDateTimeMillis(year, monthOfYear, dayOfMonth, 0);
setDate(instantMidnight);
}
//-----------------------------------------------------------------------
/**
* Set the time from milliseconds.
* The date part of this object will be unaffected.
*
* @param millis an instant to copy the time from, date part ignored
* @throws IllegalArgumentException if the value is invalid
*/
public void setTime(final long millis) {
int millisOfDay = ISOChronology.getInstanceUTC().millisOfDay().get(millis);
setMillis(getChronology().millisOfDay().set(getMillis(), millisOfDay));
}
/**
* Set the time from another instant.
* The date part of this object will be unaffected.
*
* @param instant an instant to copy the time from, date part ignored
* @throws IllegalArgumentException if the object is invalid
*/
public void setTime(final ReadableInstant instant) {
long instantMillis = DateTimeUtils.getInstantMillis(instant);
Chronology instantChrono = DateTimeUtils.getInstantChronology(instant);
DateTimeZone zone = instantChrono.getZone();
if (zone != null) {
instantMillis = zone.getMillisKeepLocal(DateTimeZone.UTC, instantMillis);
}
setTime(instantMillis);
}
/**
* Set the time from fields.
* The date part of this object will be unaffected.
*
* @param hour the hour
* @param minuteOfHour the minute of the hour
* @param secondOfMinute the second of the minute
* @param millisOfSecond the millisecond of the second
* @throws IllegalArgumentException if the value is invalid
*/
public void setTime(
final int hour,
final int minuteOfHour,
final int secondOfMinute,
final int millisOfSecond) {