This repository has been archived by the owner on Mar 20, 2018. It is now read-only.
/
OffsetDate.java
1247 lines (1174 loc) · 54.7 KB
/
OffsetDate.java
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 (c) 2007-2011, Stephen Colebourne & Michael Nascimento Santos
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of JSR-310 nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package javax.time.calendar;
import java.io.Serializable;
import javax.time.CalendricalException;
import javax.time.Instant;
import javax.time.InstantProvider;
import javax.time.MathUtils;
import javax.time.calendar.format.CalendricalParseException;
import javax.time.calendar.format.DateTimeFormatter;
import javax.time.calendar.format.DateTimeFormatters;
/**
* A date with a zone offset from UTC in the ISO-8601 calendar system,
* such as {@code 2007-12-03+01:00}.
* <p>
* {@code OffsetDate} is an immutable calendrical that represents a date, often viewed
* as year-month-day-offset. This object can also access other date fields such as
* day-of-year, day-of-week and week-of-year.
* <p>
* This class does not store or represent a time.
* For example, the value "2nd October 2007 +02:00" can be stored
* in a {@code OffsetDate}.
* <p>
* OffsetDate is immutable and thread-safe.
*
* @author Michael Nascimento Santos
* @author Stephen Colebourne
*/
public final class OffsetDate
implements Calendrical, CalendricalMatcher, Comparable<OffsetDate>, Serializable {
/**
* Serialization version.
*/
private static final long serialVersionUID = -3618963189L;
/**
* The date.
*/
private final LocalDate date;
/**
* The zone offset.
*/
private final ZoneOffset offset;
//-----------------------------------------------------------------------
/**
* Gets the rule for {@code OffsetDate}.
*
* @return the rule for the date, not null
*/
public static CalendricalRule<OffsetDate> rule() {
return ISOCalendricalRule.OFFSET_DATE;
}
//-----------------------------------------------------------------------
/**
* Obtains the current date from the system clock in the default time-zone.
* <p>
* This will query the {@link Clock#systemDefaultZone() system clock} in the default
* time-zone to obtain the current date.
* The offset will be calculated from the time-zone in the clock.
* <p>
* Using this method will prevent the ability to use an alternate clock for testing
* because the clock is hard-coded.
*
* @return the current date using the system clock, not null
*/
public static OffsetDate now() {
return now(Clock.systemDefaultZone());
}
/**
* Obtains the current date from the specified clock.
* <p>
* This will query the specified clock to obtain the current date - today.
* The offset will be calculated from the time-zone in the clock.
* <p>
* Using this method allows the use of an alternate clock for testing.
* The alternate clock may be introduced using {@link Clock dependency injection}.
*
* @param clock the clock to use, not null
* @return the current date, not null
*/
public static OffsetDate now(Clock clock) {
ISOChronology.checkNotNull(clock, "Clock must not be null");
final Instant now = clock.instant(); // called once
return ofInstant(now, clock.getZone().getRules().getOffset(now));
}
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code OffsetDate} from a year, month and day.
*
* @param year the year to represent, from MIN_YEAR to MAX_YEAR
* @param monthOfYear the month-of-year to represent, not null
* @param dayOfMonth the day-of-month to represent, from 1 to 31
* @param offset the zone offset, not null
* @return the offset date, not null
* @throws IllegalCalendarFieldValueException if the value of any field is out of range
* @throws InvalidCalendarFieldException if the day-of-month is invalid for the month-year
*/
public static OffsetDate of(int year, MonthOfYear monthOfYear, int dayOfMonth, ZoneOffset offset) {
LocalDate date = LocalDate.of(year, monthOfYear, dayOfMonth);
return new OffsetDate(date, offset);
}
/**
* Obtains an instance of {@code OffsetDate} from a year, month and day.
*
* @param year the year to represent, from MIN_YEAR to MAX_YEAR
* @param monthOfYear the month-of-year to represent, from 1 (January) to 12 (December)
* @param dayOfMonth the day-of-month to represent, from 1 to 31
* @param offset the zone offset, not null
* @return the offset date, not null
* @throws IllegalCalendarFieldValueException if the value of any field is out of range
* @throws InvalidCalendarFieldException if the day-of-month is invalid for the month-year
*/
public static OffsetDate of(int year, int monthOfYear, int dayOfMonth, ZoneOffset offset) {
LocalDate date = LocalDate.of(year, monthOfYear, dayOfMonth);
return new OffsetDate(date, offset);
}
/**
* Obtains an instance of {@code OffsetDate} from a local date and an offset.
*
* @param date the local date, not null
* @param offset the zone offset, not null
* @return the offset date, not null
*/
public static OffsetDate of(LocalDate date, ZoneOffset offset) {
return new OffsetDate(date, offset);
}
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code OffsetDate} from an {@code InstantProvider}
* using the UTC offset.
* <p>
* This conversion drops the time component of the instant effectively
* converting at midnight at the start of the UTC day.
*
* @param instantProvider the instant to convert, not null
* @return the offset date in UTC, not null
* @throws CalendricalException if the instant exceeds the supported date range
*/
public static OffsetDate ofInstantUTC(InstantProvider instantProvider) {
return ofInstant(instantProvider, ZoneOffset.UTC);
}
/**
* Obtains an instance of {@code OffsetDate} from an {@code InstantProvider}.
* <p>
* This conversion drops the time component of the instant effectively
* converting at midnight at the start of the day.
*
* @param instantProvider the instant to convert, not null
* @param offset the zone offset, not null
* @return the offset date, not null
* @throws CalendricalException if the instant exceeds the supported date range
*/
public static OffsetDate ofInstant(InstantProvider instantProvider, ZoneOffset offset) {
Instant instant = Instant.of(instantProvider);
ISOChronology.checkNotNull(offset, "ZoneOffset must not be null");
long epochSec = instant.getEpochSecond() + offset.getAmountSeconds(); // overflow caught later
long yearZeroDay = MathUtils.floorDiv(epochSec, ISOChronology.SECONDS_PER_DAY) + ISOChronology.DAYS_0000_TO_1970;
LocalDate date = LocalDate.ofYearZeroDay(yearZeroDay);
return new OffsetDate(date, offset);
}
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code OffsetDate} from a set of calendricals.
* <p>
* A calendrical represents some form of date and time information.
* This method combines the input calendricals into a date.
*
* @param calendricals the calendricals to create a date from, no nulls, not null
* @return the offset date, not null
* @throws CalendricalException if unable to merge to an offset date
*/
public static OffsetDate from(Calendrical... calendricals) {
return CalendricalEngine.merge(calendricals).deriveChecked(rule());
}
/**
* Obtains an instance of {@code OffsetDate} from the engine.
* <p>
* This internal method is used by the associated rule.
*
* @param engine the engine to derive from, not null
* @return the offset date, null if unable to obtain the date
*/
static OffsetDate deriveFrom(CalendricalEngine engine) {
LocalDate date = engine.getDate(true);
ZoneOffset offset = engine.getOffset(true);
if (date == null || offset == null) {
return null;
}
return new OffsetDate(date, offset);
}
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code OffsetDate} from a text string such as {@code 2007-12-03+01:00}.
* <p>
* The string must represent a valid date and is parsed using
* {@link DateTimeFormatters#isoOffsetDate()}.
* Year, month, day-of-month and offset are required.
* Years outside the range 0000 to 9999 must be prefixed by the plus or minus symbol.
*
* @param text the text to parse such as "2007-12-03+01:00", not null
* @return the parsed offset date, not null
* @throws CalendricalParseException if the text cannot be parsed
*/
public static OffsetDate parse(CharSequence text) {
return DateTimeFormatters.isoOffsetDate().parse(text, rule());
}
/**
* Obtains an instance of {@code OffsetDate} from a text string using a specific formatter.
* <p>
* The text is parsed using the formatter, returning a date.
*
* @param text the text to parse, not null
* @param formatter the formatter to use, not null
* @return the parsed offset date, not null
* @throws UnsupportedOperationException if the formatter cannot parse
* @throws CalendricalParseException if the text cannot be parsed
*/
public static OffsetDate parse(CharSequence text, DateTimeFormatter formatter) {
ISOChronology.checkNotNull(formatter, "DateTimeFormatter must not be null");
return formatter.parse(text, rule());
}
//-----------------------------------------------------------------------
/**
* Constructor.
*
* @param date the date, validated as not null
* @param offset the zone offset, validated as not null
*/
private OffsetDate(LocalDate date, ZoneOffset offset) {
if (date == null) {
throw new NullPointerException("LocalDate must not be null");
}
if (offset == null) {
throw new NullPointerException("ZoneOffset must not be null");
}
this.date = date;
this.offset = offset;
}
/**
* Returns a new date based on this one, returning {@code this} where possible.
*
* @param date the date to create with, not null
* @param offset the zone offset to create with, not null
*/
private OffsetDate with(LocalDate date, ZoneOffset offset) {
if (this.date == date && this.offset.equals(offset)) {
return this;
}
return new OffsetDate(date, offset);
}
//-----------------------------------------------------------------------
/**
* Gets the value of the specified calendrical rule.
* <p>
* This method queries the value of the specified calendrical rule.
* If the value cannot be returned for the rule from this date then
* {@code null} will be returned.
*
* @param ruleToDerive the rule to derive, not null
* @return the value for the rule, null if the value cannot be returned
*/
@SuppressWarnings("unchecked")
public <T> T get(CalendricalRule<T> ruleToDerive) {
if (ruleToDerive == rule()) {
return (T) this;
}
return CalendricalEngine.derive(ruleToDerive, rule(), date, null, offset, null, ISOChronology.INSTANCE, null);
}
//-----------------------------------------------------------------------
/**
* Gets the zone offset.
*
* @return the zone offset, not null
*/
public ZoneOffset getOffset() {
return offset;
}
/**
* Returns a copy of this {@code OffsetDate} with the specified offset.
* <p>
* This method returns an object with the same {@code LocalDate} and the specified {@code ZoneOffset}.
* No calculation is needed or performed.
* For example, if this time represents {@code 2007-12-03+02:00} and the offset specified is
* {@code +03:00}, then this method will return {@code 2007-12-03+03:00}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param offset the zone offset to change to, not null
* @return an {@code OffsetDate} based on this date with the requested offset, not null
*/
public OffsetDate withOffset(ZoneOffset offset) {
ISOChronology.checkNotNull(offset, "ZoneOffset must not be null");
return with(date, offset);
}
//-----------------------------------------------------------------------
/**
* Gets the year field.
* <p>
* This method returns the primitive {@code int} value for the year.
* Additional information about the year can be obtained by creating a {@link Year}.
*
* @return the year, from MIN_YEAR to MAX_YEAR
*/
public int getYear() {
return date.getYear();
}
/**
* Gets the month-of-year field, which is an enum {@code MonthOfYear}.
* <p>
* This method returns the enum {@link MonthOfYear} for the month.
* This avoids confusion as to what {@code int} values mean.
* If you need access to the primitive {@code int} value then the enum
* provides the {@link MonthOfYear#getValue() int value}.
* <p>
* Additional information can be obtained from the {@code MonthOfYear}.
* This includes month lengths, textual names and access to the quarter-of-year
* and month-of-quarter values.
*
* @return the month-of-year, not null
*/
public MonthOfYear getMonthOfYear() {
return date.getMonthOfYear();
}
/**
* Gets the day-of-month field.
* <p>
* This method returns the primitive {@code int} value for the day-of-month.
*
* @return the day-of-month, from 1 to 31
*/
public int getDayOfMonth() {
return date.getDayOfMonth();
}
/**
* Gets the day-of-year field.
* <p>
* This method returns the primitive {@code int} value for the day-of-year.
*
* @return the day-of-year, from 1 to 365, or 366 in a leap year
*/
public int getDayOfYear() {
return date.getDayOfYear();
}
/**
* Gets the day-of-week field, which is an enum {@code DayOfWeek}.
* <p>
* This method returns the enum {@link DayOfWeek} for the day-of-week.
* This avoids confusion as to what {@code int} values mean.
* If you need access to the primitive {@code int} value then the enum
* provides the {@link DayOfWeek#getValue() int value}.
* <p>
* Additional information can be obtained from the {@code DayOfWeek}.
* This includes textual names of the values.
*
* @return the day-of-week, not null
*/
public DayOfWeek getDayOfWeek() {
return date.getDayOfWeek();
}
//-----------------------------------------------------------------------
/**
* Checks if the year is a leap year, according to the ISO proleptic
* calendar system rules.
* <p>
* This method applies the current rules for leap years across the whole time-line.
* In general, a year is a leap year if it is divisible by four without
* remainder. However, years divisible by 100, are not leap years, with
* the exception of years divisible by 400 which are.
* <p>
* For example, 1904 is a leap year it is divisible by 4.
* 1900 was not a leap year as it is divisible by 100, however 2000 was a
* leap year as it is divisible by 400.
* <p>
* The calculation is proleptic - applying the same rules into the far future and far past.
* This is historically inaccurate, but is correct for the ISO-8601 standard.
*
* @return true if the year is leap, false otherwise
*/
public boolean isLeapYear() {
return date.isLeapYear();
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code OffsetDate} with the date altered using the adjuster.
* <p>
* This adjusts the date according to the rules of the specified adjuster.
* The offset is not part of the calculation and will be unchanged in the result.
* Note that {@link LocalDate} implements {@code DateAdjuster}, thus this method
* can be used to change the entire date.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param adjuster the adjuster to use, not null
* @return an {@code OffsetDate} based on this date adjusted as necessary, not null
*/
public OffsetDate with(DateAdjuster adjuster) {
return with(date.with(adjuster), offset);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code OffsetDate} with the year altered.
* If the resulting date is invalid, it will be resolved using {@link DateResolvers#previousValid()}.
* The offset does not affect the calculation and will be the same in the result.
* <p>
* This method does the same as {@code withYear(year, DateResolvers.previousValid())}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param year the year to set in the returned date, from MIN_YEAR to MAX_YEAR
* @return an {@code OffsetDate} based on this date with the requested year, not null
* @throws IllegalCalendarFieldValueException if the year value is invalid
*/
public OffsetDate withYear(int year) {
return with(date.withYear(year), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the year altered.
* If the resulting date is invalid, it will be resolved using {@code dateResolver}.
* The offset does not affect the calculation and will be the same in the result.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param year the year to set in the returned date, from MIN_YEAR to MAX_YEAR
* @param dateResolver the DateResolver to be used if the resulting date would be invalid
* @return an {@code OffsetDate} based on this date with the requested year, not null
* @throws IllegalCalendarFieldValueException if the year value is invalid
*/
public OffsetDate withYear(int year, DateResolver dateResolver) {
return with(date.withYear(year, dateResolver), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the month-of-year altered.
* If the resulting date is invalid, it will be resolved using {@link DateResolvers#previousValid()}.
* The offset does not affect the calculation and will be the same in the result.
* <p>
* This method does the same as {@code withMonthOfYear(monthOfYear, DateResolvers.previousValid())}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param monthOfYear the month-of-year to set in the returned date, from 1 (January) to 12 (December)
* @return an {@code OffsetDate} based on this date with the requested month, not null
* @throws IllegalCalendarFieldValueException if the month-of-year value is invalid
*/
public OffsetDate withMonthOfYear(int monthOfYear) {
return with(date.withMonthOfYear(monthOfYear), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the month-of-year altered.
* If the resulting date is invalid, it will be resolved using {@code dateResolver}.
* The offset does not affect the calculation and will be the same in the result.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param monthOfYear the month-of-year to set in the returned date, from 1 (January) to 12 (December)
* @param dateResolver the DateResolver to be used if the resulting date would be invalid
* @return an {@code OffsetDate} based on this date with the requested month, not null
* @throws IllegalCalendarFieldValueException if the month-of-year value is invalid
*/
public OffsetDate withMonthOfYear(int monthOfYear, DateResolver dateResolver) {
return with(date.withMonthOfYear(monthOfYear, dateResolver), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the month-of-year altered.
* If the resulting date is invalid, it will be resolved using {@link DateResolvers#previousValid()}.
* The offset does not affect the calculation and will be the same in the result.
* <p>
* This method does the same as {@code with(monthOfYear, DateResolvers.previousValid())}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param monthOfYear the month-of-year to set in the returned date, not null
* @return an {@code OffsetDate} based on this date with the requested month, not null
*/
public OffsetDate with(MonthOfYear monthOfYear) {
return with(date.with(monthOfYear), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the month-of-year altered.
* If the resulting date is invalid, it will be resolved using {@code dateResolver}.
* The offset does not affect the calculation and will be the same in the result.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param monthOfYear the month-of-year to set in the returned date, not null
* @param dateResolver the DateResolver to be used if the resulting date would be invalid
* @return an {@code OffsetDate} based on this date with the requested month, not null
*/
public OffsetDate with(MonthOfYear monthOfYear, DateResolver dateResolver) {
return with(date.with(monthOfYear, dateResolver), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the day-of-month altered.
* If the resulting date is invalid, an exception is thrown.
* The offset does not affect the calculation and will be the same in the result.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param dayOfMonth the day-of-month to set in the returned date, from 1 to 28-31
* @return an {@code OffsetDate} based on this date with the requested day, not null
* @throws IllegalCalendarFieldValueException if the day-of-month value is invalid
* @throws InvalidCalendarFieldException if the day-of-month is invalid for the month-year
*/
public OffsetDate withDayOfMonth(int dayOfMonth) {
return with(date.withDayOfMonth(dayOfMonth), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the day-of-month altered.
* If the resulting date is invalid, it will be resolved using {@code dateResolver}.
* The offset does not affect the calculation and will be the same in the result.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param dayOfMonth the day-of-month to set in the returned date, from 1 to 31
* @param dateResolver the DateResolver to be used if the resulting date would be invalid
* @return an {@code OffsetDate} based on this date with the requested day, not null
* @throws IllegalCalendarFieldValueException if the day-of-month value is invalid
*/
public OffsetDate withDayOfMonth(int dayOfMonth, DateResolver dateResolver) {
return with(date.withDayOfMonth(dayOfMonth, dateResolver), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the day-of-year altered.
* If the resulting date is invalid, an exception is thrown.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param dayOfYear the day-of-year to set in the returned date, from 1 to 365-366
* @return an {@code OffsetDate} based on this date with the requested day, not null
* @throws IllegalCalendarFieldValueException if the day-of-year value is invalid
* @throws InvalidCalendarFieldException if the day-of-year is invalid for the year
*/
public OffsetDate withDayOfYear(int dayOfYear) {
return with(date.withDayOfYear(dayOfYear), offset);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code OffsetDate} with the specified date period added.
* <p>
* This adds the specified period to this date, returning a new date.
* Before addition, the period is converted to a date-based {@code Period} using
* {@link Period#ofDateFields(PeriodProvider)}.
* That factory ignores any time-based ISO fields, thus adding a time-based
* period to this date will have no effect. If you want to take time fields into
* account, call {@link Period#normalizedWith24HourDays()} on the input period.
* <p>
* The detailed rules for the addition have some complexity due to variable length months.
* See {@link LocalDate#plus(PeriodProvider)} for details.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param periodProvider the period to add, not null
* @return an {@code OffsetDate} based on this date with the period added, not null
* @throws CalendricalException if the specified period cannot be converted to a {@code Period}
* @throws CalendricalException if the result exceeds the supported date range
*/
public OffsetDate plus(PeriodProvider periodProvider) {
return with(date.plus(periodProvider), offset);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code OffsetDate} with the specified period in years added.
* <p>
* This method adds the specified amount to the years field in three steps:
* <ol>
* <li>Add the input years to the year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2008-02-29 (leap year) plus one year would result in the
* invalid date 2009-02-29 (standard year). Instead of returning an invalid
* result, the last valid day of the month, 2009-02-28, is selected instead.
* <p>
* This method does the same as {@code plusYears(years, DateResolvers.previousValid())}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param years the years to add, may be negative
* @return an {@code OffsetDate} based on this date with the years added, not null
* @throws CalendricalException if the result exceeds the supported date range
* @see #plusYears(long, javax.time.calendar.DateResolver)
*/
public OffsetDate plusYears(long years) {
return with(date.plusYears(years), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the specified period in years added.
* <p>
* This method adds the specified amount to the years field in three steps:
* <ol>
* <li>Add the input years to the year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the date using {@code dateResolver} if necessary</li>
* </ol>
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param years the years to add, may be negative
* @param dateResolver the DateResolver to be used if the resulting date would be invalid
* @return an {@code OffsetDate} based on this date with the years added, not null
* @throws CalendricalException if the result exceeds the supported date range
*/
public OffsetDate plusYears(long years, DateResolver dateResolver) {
return with(date.plusYears(years, dateResolver), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the specified period in months added.
* <p>
* This method adds the specified amount to the months field in three steps:
* <ol>
* <li>Add the input months to the month-of-year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2007-03-31 plus one month would result in the invalid date
* 2007-04-31. Instead of returning an invalid result, the last valid day
* of the month, 2007-04-30, is selected instead.
* <p>
* This method does the same as {@code plusMonths(months, DateResolvers.previousValid())}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param months the months to add, may be negative
* @return an {@code OffsetDate} based on this date with the months added, not null
* @throws CalendricalException if the result exceeds the supported date range
* @see #plusMonths(long, javax.time.calendar.DateResolver)
*/
public OffsetDate plusMonths(long months) {
return with(date.plusMonths(months), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the specified period in months added.
* <p>
* This method adds the specified amount to the months field in three steps:
* <ol>
* <li>Add the input months to the month-of-year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the date using {@code dateResolver} if necessary</li>
* </ol>
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param months the months to add, may be negative
* @param dateResolver the DateResolver to be used if the resulting date would be invalid
* @return an {@code OffsetDate} based on this date with the months added, not null
* @throws CalendricalException if the result exceeds the supported date range
*/
public OffsetDate plusMonths(long months, DateResolver dateResolver) {
return with(date.plusMonths(months, dateResolver), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the specified period in weeks added.
* <p>
* This method adds the specified amount in weeks to the days field incrementing
* the month and year fields as necessary to ensure the result remains valid.
* The result is only invalid if the maximum/minimum year is exceeded.
* <p>
* For example, 2008-12-31 plus one week would result in 2009-01-07.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param weeks the weeks to add, may be negative
* @return an {@code OffsetDate} based on this date with the weeks added, not null
* @throws CalendricalException if the result exceeds the supported date range
*/
public OffsetDate plusWeeks(long weeks) {
return with(date.plusWeeks(weeks), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the specified period in days added.
* <p>
* This method adds the specified amount to the days field incrementing the
* month and year fields as necessary to ensure the result remains valid.
* The result is only invalid if the maximum/minimum year is exceeded.
* <p>
* For example, 2008-12-31 plus one day would result in 2009-01-01.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param days the days to add, may be negative
* @return an {@code OffsetDate} based on this date with the days added, not null
* @throws CalendricalException if the result exceeds the supported date range
*/
public OffsetDate plusDays(long days) {
return with(date.plusDays(days), offset);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code OffsetDate} with the specified date period subtracted.
* <p>
* This subtracts the specified period from this date, returning a new date.
* Before subtraction, the period is converted to a date-based {@code Period} using
* {@link Period#ofDateFields(PeriodProvider)}.
* That factory ignores any time-based ISO fields, thus subtracting a time-based
* period from this date will have no effect. If you want to take time fields into
* account, call {@link Period#normalizedWith24HourDays()} on the input period.
* <p>
* The detailed rules for the subtraction have some complexity due to variable length months.
* See {@link LocalDate#minus(PeriodProvider)} for details.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param periodProvider the period to subtract, not null
* @return an {@code OffsetDate} based on this date with the period subtracted, not null
* @throws CalendricalException if the specified period cannot be converted to a {@code Period}
* @throws CalendricalException if the result exceeds the supported date range
*/
public OffsetDate minus(PeriodProvider periodProvider) {
return with(date.minus(periodProvider), offset);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code OffsetDate} with the specified period in years subtracted.
* <p>
* This method subtracts the specified amount from the years field in three steps:
* <ol>
* <li>Subtract the input years to the year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2008-02-29 (leap year) minus one year would result in the
* invalid date 2007-02-29 (standard year). Instead of returning an invalid
* result, the last valid day of the month, 2007-02-28, is selected instead.
* <p>
* This method does the same as {@code minusYears(years, DateResolvers.previousValid())}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param years the years to subtract, may be negative
* @return an {@code OffsetDate} based on this date with the years subtracted, not null
* @throws CalendricalException if the result exceeds the supported date range
* @see #minusYears(long, javax.time.calendar.DateResolver)
*/
public OffsetDate minusYears(long years) {
return with(date.minusYears(years), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the specified period in years subtracted.
* <p>
* This method subtracts the specified amount from the years field in three steps:
* <ol>
* <li>Subtract the input years to the year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the date using {@code dateResolver} if necessary</li>
* </ol>
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param years the years to subtract, may be negative
* @param dateResolver the DateResolver to be used if the resulting date would be invalid
* @return an {@code OffsetDate} based on this date with the years subtracted, not null
* @throws CalendricalException if the result exceeds the supported date range
*/
public OffsetDate minusYears(long years, DateResolver dateResolver) {
return with(date.minusYears(years, dateResolver), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the specified period in months subtracted.
* <p>
* This method subtracts the specified amount from the months field in three steps:
* <ol>
* <li>Subtract the input months to the month-of-year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2007-03-31 minus one month would result in the invalid date
* 2007-02-31. Instead of returning an invalid result, the last valid day
* of the month, 2007-02-28, is selected instead.
* <p>
* This method does the same as {@code minusMonths(months, DateResolvers.previousValid())}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param months the months to subtract, may be negative
* @return an {@code OffsetDate} based on this date with the months subtracted, not null
* @throws CalendricalException if the result exceeds the supported date range
* @see #minusMonths(long, javax.time.calendar.DateResolver)
*/
public OffsetDate minusMonths(long months) {
return with(date.minusMonths(months), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the specified period in months subtracted.
* <p>
* This method subtracts the specified amount from the months field in three steps:
* <ol>
* <li>Subtract the input months to the month-of-year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the date using {@code dateResolver} if necessary</li>
* </ol>
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param months the months to subtract, may be negative
* @param dateResolver the DateResolver to be used if the resulting date would be invalid
* @return an {@code OffsetDate} based on this date with the months subtracted, not null
* @throws CalendricalException if the result exceeds the supported date range
*/
public OffsetDate minusMonths(long months, DateResolver dateResolver) {
return with(date.minusMonths(months, dateResolver), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the specified period in weeks subtracted.
* <p>
* This method subtracts the specified amount in weeks from the days field decrementing
* the month and year fields as necessary to ensure the result remains valid.
* The result is only invalid if the maximum/minimum year is exceeded.
* <p>
* For example, 2009-01-07 minus one week would result in 2008-12-31.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param weeks the weeks to subtract, may be negative
* @return an {@code OffsetDate} based on this date with the weeks subtracted, not null
* @throws CalendricalException if the result exceeds the supported date range
*/
public OffsetDate minusWeeks(long weeks) {
return with(date.minusWeeks(weeks), offset);
}
/**
* Returns a copy of this {@code OffsetDate} with the specified number of days subtracted.
* <p>
* This method subtracts the specified amount from the days field decrementing the
* month and year fields as necessary to ensure the result remains valid.
* The result is only invalid if the maximum/minimum year is exceeded.
* <p>
* For example, 2009-01-01 minus one day would result in 2008-12-31.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param days the days to subtract, may be negative
* @return an {@code OffsetDate} based on this date with the days subtracted, not null
* @throws CalendricalException if the result exceeds the supported date range
*/
public OffsetDate minusDays(long days) {
return with(date.minusDays(days), offset);
}
//-----------------------------------------------------------------------
/**
* Checks whether this {@code OffsetDate} matches the specified matcher.
* <p>
* Matchers can be used to query the date.
* A simple matcher might simply compare one of the fields, such as the year field.
* A more complex matcher might check if the date is the last day of the month.
*
* @param matcher the matcher to use, not null
* @return true if this date matches the matcher, false otherwise
*/
public boolean matches(CalendricalMatcher matcher) {
return matcher.matchesCalendrical(this);
}
//-----------------------------------------------------------------------
/**
* Checks if the date extracted from the calendrical matches this date.
* <p>
* This method implements the {@code CalendricalMatcher} interface.
* It is intended that applications use {@link #matches} rather than this method.
*
* @param calendrical the calendrical to match, not null
* @return true if the calendrical matches, false otherwise
*/
public boolean matchesCalendrical(Calendrical calendrical) {
return this.equals(calendrical.get(rule()));
}
//-----------------------------------------------------------------------
/**
* Returns an offset date-time formed from this date at the specified time.
* <p>
* This merges the two objects - {@code this} and the specified time -
* to form an instance of {@code OffsetDateTime}.
* If the offset of the time differs from the offset of the date, then the
* result will have the offset of the date and the time will be adjusted to match.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param time the time to use, not null
* @return the offset date-time formed from this date and the specified time, not null
*/
public OffsetDateTime atTime(OffsetTime time) {
return date.atTime(time.withOffsetSameInstant(offset));
}
/**
* Returns an offset date-time formed from this date at the specified time.
* <p>
* This merges the two objects - {@code this} and the specified time -
* to form an instance of {@code OffsetDateTime}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param time the time to use, not null
* @return the offset date-time formed from this date and the specified time, not null
*/
public OffsetDateTime atTime(LocalTime time) {
return OffsetDateTime.of(date, time, offset);
}
/**
* Returns an offset date-time formed from this date at the specified time.
* <p>
* This merges the three values - {@code this} and the specified time -
* to form an instance of {@code OffsetDateTime}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param hourOfDay the hour-of-day to use, from 0 to 23
* @param minuteOfHour the minute-of-hour to use, from 0 to 59
* @return the offset date-time formed from this date and the specified time, not null
* @throws IllegalCalendarFieldValueException if the value of any field is out of range
*/
public OffsetDateTime atTime(int hourOfDay, int minuteOfHour) {
return atTime(LocalTime.of(hourOfDay, minuteOfHour));
}