This repository has been archived by the owner on Mar 20, 2018. It is now read-only.
/
OffsetDateTime.java
1948 lines (1836 loc) · 89.6 KB
/
OffsetDateTime.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.Duration;
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-time with a zone offset from UTC in the ISO-8601 calendar system,
* such as {@code 2007-12-03T10:15:30+01:00}.
* <p>
* {@code OffsetDateTime} is an immutable representation of a date-time with an offset.
* This class stores all date and time fields, to a precision of nanoseconds,
* as well as the offset from UTC. Thus, for example, the value
* "2nd October 2007 at 13:45.30.123456789 +02:00" can be stored in an {@code OffsetDateTime}.
* <p>
* OffsetDateTime is immutable and thread-safe.
*
* @author Michael Nascimento Santos
* @author Stephen Colebourne
*/
public final class OffsetDateTime
implements Calendrical, InstantProvider, DateTimeProvider,
CalendricalMatcher, DateAdjuster, TimeAdjuster,
Comparable<OffsetDateTime>, Serializable {
/**
* A serialization identifier for this class.
*/
private static final long serialVersionUID = -456761901L;
/**
* The local date-time.
*/
private final LocalDateTime dateTime;
/**
* The zone offset.
*/
private final ZoneOffset offset;
//-----------------------------------------------------------------------
/**
* Gets the rule for {@code OffsetDateTime}.
*
* @return the rule for the date-time, not null
*/
public static CalendricalRule<OffsetDateTime> rule() {
return ISOCalendricalRule.OFFSET_DATE_TIME;
}
//-----------------------------------------------------------------------
/**
* Obtains the current date-time 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-time.
* 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-time using the system clock, not null
*/
public static OffsetDateTime now() {
return now(Clock.systemDefaultZone());
}
/**
* Obtains the current date-time from the specified clock.
* <p>
* This will query the specified clock to obtain the current date-time.
* 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-time, not null
*/
public static OffsetDateTime 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 OffsetDateTime} from year, month and
* day with the time set to midnight at the start of day.
* <p>
* The time fields will be set to zero by this factory method.
*
* @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-time, 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 OffsetDateTime ofMidnight(
int year, MonthOfYear monthOfYear, int dayOfMonth, ZoneOffset offset) {
LocalDateTime dt = LocalDateTime.ofMidnight(year, monthOfYear, dayOfMonth);
return new OffsetDateTime(dt, offset);
}
/**
* Obtains an instance of {@code OffsetDateTime} from year, month and
* day with the time set to midnight at the start of day.
* <p>
* The time fields will be set to zero by this factory method.
*
* @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-time, 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 OffsetDateTime ofMidnight(
int year, int monthOfYear, int dayOfMonth, ZoneOffset offset) {
LocalDateTime dt = LocalDateTime.ofMidnight(year, monthOfYear, dayOfMonth);
return new OffsetDateTime(dt, offset);
}
/**
* Obtains an instance of {@code OffsetDateTime} from a date with the
* time set to midnight at the start of day.
* <p>
* The time fields will be set to zero by this factory method.
*
* @param dateProvider the date provider to use, not null
* @param offset the zone offset, not null
* @return the offset date-time, not null
*/
public static OffsetDateTime ofMidnight(DateProvider dateProvider, ZoneOffset offset) {
LocalDateTime dt = LocalDateTime.ofMidnight(dateProvider);
return new OffsetDateTime(dt, offset);
}
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code OffsetDateTime} from year, month,
* day, hour and minute, setting the second and nanosecond to zero.
* <p>
* The second and nanosecond fields will be set to zero by this factory method.
*
* @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 hourOfDay the hour-of-day to represent, from 0 to 23
* @param minuteOfHour the minute-of-hour to represent, from 0 to 59
* @param offset the zone offset, not null
* @return the offset date-time, 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 OffsetDateTime of(
int year, MonthOfYear monthOfYear, int dayOfMonth,
int hourOfDay, int minuteOfHour, ZoneOffset offset) {
LocalDateTime dt = LocalDateTime.of(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour);
return new OffsetDateTime(dt, offset);
}
/**
* Obtains an instance of {@code OffsetDateTime} from year, month,
* day, hour, minute and second, setting the nanosecond to zero.
* <p>
* The day must be valid for the year and month or an exception will be thrown.
* <p>
* The nanosecond field will be set to zero by this factory method.
*
* @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 hourOfDay the hour-of-day to represent, from 0 to 23
* @param minuteOfHour the minute-of-hour to represent, from 0 to 59
* @param secondOfMinute the second-of-minute to represent, from 0 to 59
* @param offset the zone offset, not null
* @return the offset date-time, 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 OffsetDateTime of(
int year, MonthOfYear monthOfYear, int dayOfMonth,
int hourOfDay, int minuteOfHour, int secondOfMinute, ZoneOffset offset) {
LocalDateTime dt = LocalDateTime.of(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute);
return new OffsetDateTime(dt, offset);
}
/**
* Obtains an instance of {@code OffsetDateTime} from year, month,
* day, hour, minute, second and nanosecond.
* <p>
* The day must be valid for the year and month or an exception will be thrown.
*
* @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 hourOfDay the hour-of-day to represent, from 0 to 23
* @param minuteOfHour the minute-of-hour to represent, from 0 to 59
* @param secondOfMinute the second-of-minute to represent, from 0 to 59
* @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999
* @param offset the zone offset, not null
* @return the offset date-time, 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 OffsetDateTime of(
int year, MonthOfYear monthOfYear, int dayOfMonth,
int hourOfDay, int minuteOfHour, int secondOfMinute, int nanoOfSecond, ZoneOffset offset) {
LocalDateTime dt = LocalDateTime.of(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, nanoOfSecond);
return new OffsetDateTime(dt, offset);
}
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code OffsetDateTime} from year, month,
* day, hour and minute, setting the second and nanosecond to zero.
* <p>
* The day must be valid for the year and month or an exception will be thrown.
* <p>
* The second and nanosecond fields will be set to zero by this factory method.
*
* @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 hourOfDay the hour-of-day to represent, from 0 to 23
* @param minuteOfHour the minute-of-hour to represent, from 0 to 59
* @param offset the zone offset, not null
* @return the offset date-time, 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 OffsetDateTime of(
int year, int monthOfYear, int dayOfMonth,
int hourOfDay, int minuteOfHour, ZoneOffset offset) {
LocalDateTime dt = LocalDateTime.of(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour);
return new OffsetDateTime(dt, offset);
}
/**
* Obtains an instance of {@code OffsetDateTime} from year, month,
* day, hour, minute and second, setting the nanosecond to zero.
* <p>
* The day must be valid for the year and month or an exception will be thrown.
* <p>
* The nanosecond field will be set to zero by this factory method.
*
* @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 hourOfDay the hour-of-day to represent, from 0 to 23
* @param minuteOfHour the minute-of-hour to represent, from 0 to 59
* @param secondOfMinute the second-of-minute to represent, from 0 to 59
* @param offset the zone offset, not null
* @return the offset date-time, 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 OffsetDateTime of(
int year, int monthOfYear, int dayOfMonth,
int hourOfDay, int minuteOfHour, int secondOfMinute, ZoneOffset offset) {
LocalDateTime dt = LocalDateTime.of(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute);
return new OffsetDateTime(dt, offset);
}
/**
* Obtains an instance of {@code OffsetDateTime} from year, month,
* day, hour, minute, second and nanosecond.
* <p>
* The day must be valid for the year and month or an exception will be thrown.
*
* @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 hourOfDay the hour-of-day to represent, from 0 to 23
* @param minuteOfHour the minute-of-hour to represent, from 0 to 59
* @param secondOfMinute the second-of-minute to represent, from 0 to 59
* @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999
* @param offset the zone offset, not null
* @return the offset date-time, 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 OffsetDateTime of(
int year, int monthOfYear, int dayOfMonth,
int hourOfDay, int minuteOfHour, int secondOfMinute, int nanoOfSecond, ZoneOffset offset) {
LocalDateTime dt = LocalDateTime.of(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, nanoOfSecond);
return new OffsetDateTime(dt, offset);
}
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code OffsetDateTime} from a date, time and zone offset.
*
* @param dateProvider the date provider to use, not null
* @param timeProvider the time provider to use, not null
* @param offset the zone offset, not null
* @return the offset date-time, not null
*/
public static OffsetDateTime of(DateProvider dateProvider, TimeProvider timeProvider, ZoneOffset offset) {
LocalDateTime dt = LocalDateTime.of(dateProvider, timeProvider);
return new OffsetDateTime(dt, offset);
}
/**
* Obtains an instance of {@code OffsetDateTime} from a date-time provider.
*
* @param dateTimeProvider the date-time provider to use, not null
* @param offset the zone offset, not null
* @return the offset date-time, not null
*/
public static OffsetDateTime of(DateTimeProvider dateTimeProvider, ZoneOffset offset) {
LocalDateTime dt = LocalDateTime.of(dateTimeProvider);
return new OffsetDateTime(dt, offset);
}
/**
* Obtains an instance of {@code OffsetDateTime} from a {@code DateProvider}
* and {@code OffsetTime}.
*
* @param dateProvider the date provider to use, not null
* @param offsetTime the offset time to use, not null
* @return the offset date-time, not null
*/
public static OffsetDateTime of(DateProvider dateProvider, OffsetTime offsetTime) {
LocalDateTime dt = LocalDateTime.of(dateProvider, offsetTime);
return new OffsetDateTime(dt, offsetTime.getOffset());
}
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code OffsetDateTime} from an {@code InstantProvider}
* using the UTC offset.
*
* @param instantProvider the instant to convert, not null
* @return the offset date-time in UTC, not null
* @throws CalendricalException if the instant exceeds the supported date range
*/
public static OffsetDateTime ofInstantUTC(InstantProvider instantProvider) {
return ofInstant(instantProvider, ZoneOffset.UTC);
}
/**
* Obtains an instance of {@code OffsetDateTime} from an {@code InstantProvider}.
*
* @param instantProvider the instant to convert, not null
* @param offset the zone offset, not null
* @return the offset date-time, not null
* @throws CalendricalException if the instant exceeds the supported date range
*/
public static OffsetDateTime ofInstant(InstantProvider instantProvider, ZoneOffset offset) {
Instant instant = Instant.of(instantProvider);
ISOChronology.checkNotNull(offset, "ZoneOffset must not be null");
long localSeconds = instant.getEpochSecond() + offset.getAmountSeconds(); // overflow caught later
LocalDateTime ldt = LocalDateTime.create(localSeconds, instant.getNanoOfSecond());
return new OffsetDateTime(ldt, offset);
}
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code OffsetDateTime} using seconds from the
* epoch of 1970-01-01T00:00:00Z.
* <p>
* The nanosecond field is set to zero.
*
* @param epochSecond the number of seconds from the epoch of 1970-01-01T00:00:00Z
* @return the offset date-time, not null
* @throws CalendricalException if the result exceeds the supported range
*/
public static OffsetDateTime ofEpochSecond(long epochSecond, ZoneOffset offset) {
ISOChronology.checkNotNull(offset, "ZoneOffset must not be null");
long localSeconds = epochSecond + offset.getAmountSeconds(); // overflow caught later
LocalDateTime ldt = LocalDateTime.create(localSeconds, 0);
return new OffsetDateTime(ldt, offset);
}
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code OffsetDateTime} 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-time.
*
* @param calendricals the calendricals to create a date-time from, no nulls, not null
* @return the offset date-time, not null
* @throws CalendricalException if unable to merge to an offset date-time
*/
public static OffsetDateTime from(Calendrical... calendricals) {
return CalendricalNormalizer.merge(calendricals).deriveChecked(rule());
}
/**
* Obtains an instance of {@code OffsetDateTime} from the normalized form.
* <p>
* This internal method is used by the associated rule.
*
* @param normalized the normalized calendrical, not null
* @return the offset date-time, null if unable to obtain the date-time
*/
static OffsetDateTime deriveFrom(CalendricalNormalizer normalized) {
LocalDateTime dateTime = LocalDateTime.deriveFrom(normalized);
ZoneOffset offset = normalized.getOffset(true);
if (dateTime == null || offset == null) {
return null;
}
return new OffsetDateTime(dateTime, offset);
}
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code OffsetDateTime} from a text string such as {@code 2007-12-03T10:15:30+01:00}.
* <p>
* The following formats are accepted in ASCII:
* <ul>
* <li>{@code {Year}-{MonthOfYear}-{DayOfMonth}T{Hour}:{Minute}{OffsetID}}
* <li>{@code {Year}-{MonthOfYear}-{DayOfMonth}T{Hour}:{Minute}:{Second}{OffsetID}}
* <li>{@code {Year}-{MonthOfYear}-{DayOfMonth}T{Hour}:{Minute}:{Second}.{NanosecondFraction}{OffsetID}}
* </ul>
* <p>
* The year has between 4 and 10 digits with values from MIN_YEAR to MAX_YEAR.
* If there are more than 4 digits then the year must be prefixed with the plus symbol.
* Negative years are allowed, but not negative zero.
* <p>
* The month-of-year has 2 digits with values from 1 to 12.
* <p>
* The day-of-month has 2 digits with values from 1 to 31 appropriate to the month.
* <p>
* The hour has 2 digits with values from 0 to 23.
* The minute has 2 digits with values from 0 to 59.
* The second has 2 digits with values from 0 to 59.
* The nanosecond fraction has from 1 to 9 digits with values from 0 to 999,999,999.
* <p>
* The offset ID is the normalized form as defined in {@link ZoneOffset}.
*
* @param text the text to parse such as "2007-12-03T10:15:30+01:00", not null
* @return the parsed offset date-time, not null
* @throws CalendricalParseException if the text cannot be parsed
*/
public static OffsetDateTime parse(CharSequence text) {
return DateTimeFormatters.isoOffsetDateTime().parse(text, rule());
}
/**
* Obtains an instance of {@code OffsetDateTime} from a text string using a specific formatter.
* <p>
* The text is parsed using the formatter, returning a date-time.
*
* @param text the text to parse, not null
* @param formatter the formatter to use, not null
* @return the parsed offset date-time, not null
* @throws UnsupportedOperationException if the formatter cannot parse
* @throws CalendricalParseException if the text cannot be parsed
*/
public static OffsetDateTime parse(CharSequence text, DateTimeFormatter formatter) {
ISOChronology.checkNotNull(formatter, "DateTimeFormatter must not be null");
return formatter.parse(text, rule());
}
//-----------------------------------------------------------------------
/**
* Constructor.
*
* @param dateTime the date-time, not null
* @param offset the zone offset, not null
*/
private OffsetDateTime(LocalDateTime dateTime, ZoneOffset offset) {
if (dateTime == null) {
throw new NullPointerException("The date-time must not be null");
}
if (offset == null) {
throw new NullPointerException("The zone offset must not be null");
}
this.dateTime = dateTime;
this.offset = offset;
}
/**
* Returns a new date-time based on this one, returning {@code this} where possible.
*
* @param dateTime the date-time to create with, not null
* @param offset the zone offset to create with, not null
*/
private OffsetDateTime with(LocalDateTime dateTime, ZoneOffset offset) {
if (this.dateTime == dateTime && this.offset.equals(offset)) {
return this;
}
return new OffsetDateTime(dateTime, 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-time 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) {
// optimize, especially for LocalDateTime, OffsetDate and OffsetTime
if (ruleToDerive instanceof ISOCalendricalRule<?>) {
switch (((ISOCalendricalRule<?>) ruleToDerive).ordinal) {
case ISOCalendricalRule.LOCAL_DATE_ORDINAL: return (T) toLocalDate();
case ISOCalendricalRule.LOCAL_TIME_ORDINAL: return (T) toLocalTime();
case ISOCalendricalRule.LOCAL_DATE_TIME_ORDINAL: return (T) dateTime;
case ISOCalendricalRule.OFFSET_DATE_ORDINAL: return (T) toOffsetDate();
case ISOCalendricalRule.OFFSET_TIME_ORDINAL: return (T) toOffsetTime();
case ISOCalendricalRule.OFFSET_DATE_TIME_ORDINAL: return (T) this;
case ISOCalendricalRule.ZONE_OFFSET_ORDINAL: return (T) offset;
case ISOCalendricalRule.CHRONOLOGY_ORDINAL: return (T) ISOChronology.INSTANCE;
}
return null;
}
return CalendricalNormalizer.derive(ruleToDerive, rule(), toLocalDate(), toLocalTime(), offset, null, ISOChronology.INSTANCE, null);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code OffsetDateTime} with the time altered and the offset retained.
* <p>
* This method returns an object with the same {@code ZoneOffset} and the specified {@code LocalDateTime}.
* No calculation is needed or performed.
*
* @param dateTimeProvider the local date-time to change to, not null
* @return an {@code OffsetDateTime} based on this time with the requested time, not null
*/
public OffsetDateTime withDateTime(DateTimeProvider dateTimeProvider) {
LocalDateTime localDateTime = LocalDateTime.of(dateTimeProvider);
return localDateTime.equals(this.dateTime) ? this : new OffsetDateTime(localDateTime, offset);
}
//-----------------------------------------------------------------------
/**
* Gets the zone offset.
*
* @return the zone offset, not null
*/
public ZoneOffset getOffset() {
return offset;
}
/**
* Returns a copy of this {@code OffsetDateTime} with the specified offset ensuring
* that the result has the same local date-time.
* <p>
* This method returns an object with the same {@code LocalDateTime} and the specified {@code ZoneOffset}.
* No calculation is needed or performed.
* For example, if this time represents {@code 2007-12-03T10:30+02:00} and the offset specified is
* {@code +03:00}, then this method will return {@code 2007-12-03T10:30+03:00}.
* <p>
* To take into account the difference between the offsets, and adjust the time fields,
* use {@link #withOffsetSameInstant}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param offset the zone offset to change to, not null
* @return an {@code OffsetDateTime} based on this date-time with the requested offset, not null
*/
public OffsetDateTime withOffsetSameLocal(ZoneOffset offset) {
return with(dateTime, offset);
}
/**
* Returns a copy of this {@code OffsetDateTime} with the specified offset ensuring
* that the result is at the same instant.
* <p>
* This method returns an object with the the specified {@code ZoneOffset} and a {@code LocalDateTime}
* adjusted by the difference between the two offsets.
* This will result in the old and new objects representing the same instant.
* This is useful for finding the local time in a different offset.
* For example, if this time represents {@code 2007-12-03T10:30+02:00} and the offset specified is
* {@code +03:00}, then this method will return {@code 2007-12-03T11:30+03:00}.
* <p>
* To change the offset without adjusting the local time use {@link #withOffsetSameLocal}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param offset the zone offset to change to, not null
* @return an {@code OffsetDateTime} based on this date-time with the requested offset, not null
* @throws CalendricalException if the result exceeds the supported date range
*/
public OffsetDateTime withOffsetSameInstant(ZoneOffset offset) {
if (offset.equals(this.offset)) {
return this;
}
int difference = offset.getAmountSeconds() - this.offset.getAmountSeconds();
LocalDateTime adjusted = dateTime.plusSeconds(difference);
return new OffsetDateTime(adjusted, 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 dateTime.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 dateTime.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 dateTime.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 dateTime.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 dateTime.getDayOfWeek();
}
//-----------------------------------------------------------------------
/**
* Gets the hour-of-day field.
*
* @return the hour-of-day, from 0 to 23
*/
public int getHourOfDay() {
return dateTime.getHourOfDay();
}
/**
* Gets the minute-of-hour field.
*
* @return the minute-of-hour, from 0 to 59
*/
public int getMinuteOfHour() {
return dateTime.getMinuteOfHour();
}
/**
* Gets the second-of-minute field.
*
* @return the second-of-minute, from 0 to 59
*/
public int getSecondOfMinute() {
return dateTime.getSecondOfMinute();
}
/**
* Gets the nano-of-second field.
*
* @return the nano-of-second, from 0 to 999,999,999
*/
public int getNanoOfSecond() {
return dateTime.getNanoOfSecond();
}
//-----------------------------------------------------------------------
/**
* 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 dateTime.isLeapYear();
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this OffsetDateTime with the date altered using the adjuster.
* <p>
* Adjusters can be used to alter the date in various ways.
* A simple adjuster might simply set the one of the fields, such as the year field.
* A more complex adjuster might set the date to the last day of the month.
* <p>
* The offset and time do not affect the calculation and will be the same in the result.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param adjuster the adjuster to use, not null
* @return an {@code OffsetDateTime} based on this date-time with the date adjusted, not null
* @throws NullPointerException if the adjuster returned null
*/
public OffsetDateTime with(DateAdjuster adjuster) {
return with(dateTime.with(adjuster), offset);
}
/**
* Returns a copy of this OffsetDateTime with the time altered using the adjuster.
* <p>
* Adjusters can be used to alter the time in various ways.
* A simple adjuster might simply set the one of the fields, such as the hour field.
* A more complex adjuster might set the time to end of the working day.
* <p>
* The offset and date do not affect the calculation and will be the same in the result.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param adjuster the adjuster to use, not null
* @return an {@code OffsetDateTime} based on this date-time with the time adjusted, not null
* @throws IllegalArgumentException if the adjuster returned null
*/
public OffsetDateTime with(TimeAdjuster adjuster) {
return with(dateTime.with(adjuster), offset);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code OffsetDateTime} with the year altered.
* If the resulting {@code OffsetDateTime} 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 OffsetDateTime} based on this date-time with the requested year, not null
* @throws IllegalCalendarFieldValueException if the year value is invalid
*/
public OffsetDateTime withYear(int year) {
return with(dateTime.withYear(year), offset);
}
/**
* Returns a copy of this {@code OffsetDateTime} with the year altered.
* If the resulting {@code OffsetDateTime} 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 OffsetDateTime} based on this date-time with the requested year, not null
* @throws IllegalCalendarFieldValueException if the year value is invalid
*/
public OffsetDateTime withYear(int year, DateResolver dateResolver) {
return with(dateTime.withYear(year, dateResolver), offset);
}
/**
* Returns a copy of this {@code OffsetDateTime} with the month-of-year altered.
* If the resulting {@code OffsetDateTime} 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 OffsetDateTime} based on this date-time with the requested month, not null
* @throws IllegalCalendarFieldValueException if the month-of-year value is invalid
*/
public OffsetDateTime withMonthOfYear(int monthOfYear) {
return with(dateTime.withMonthOfYear(monthOfYear), offset);
}
/**
* Returns a copy of this {@code OffsetDateTime} with the month-of-year altered.
* If the resulting {@code OffsetDateTime} 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 OffsetDateTime} based on this date-time with the requested month, not null
* @throws IllegalCalendarFieldValueException if the month-of-year value is invalid
*/
public OffsetDateTime withMonthOfYear(int monthOfYear, DateResolver dateResolver) {
return with(dateTime.withMonthOfYear(monthOfYear, dateResolver), offset);
}
/**
* Returns a copy of this {@code OffsetDateTime} with the month-of-year altered.
* If the resulting {@code OffsetDateTime} 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 OffsetDateTime} based on this date-time with the requested month, not null
*/
public OffsetDateTime with(MonthOfYear monthOfYear) {
return with(dateTime.with(monthOfYear), offset);
}
/**
* Returns a copy of this {@code OffsetDateTime} with the month-of-year altered.
* If the resulting {@code OffsetDateTime} 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 OffsetDateTime} based on this date-time with the requested month, not null
*/
public OffsetDateTime with(MonthOfYear monthOfYear, DateResolver dateResolver) {
return with(dateTime.with(monthOfYear, dateResolver), offset);
}
/**
* Returns a copy of this {@code OffsetDateTime} with the day-of-month altered.
* If the resulting {@code OffsetDateTime} 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 OffsetDateTime} based on this date-time 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 OffsetDateTime withDayOfMonth(int dayOfMonth) {
return with(dateTime.withDayOfMonth(dayOfMonth), offset);
}
/**
* Returns a copy of this {@code OffsetDateTime} with the day-of-month altered.
* If the resulting {@code OffsetDateTime} 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 OffsetDateTime} based on this date-time with the requested day, not null
* @throws IllegalCalendarFieldValueException if the day-of-month value is invalid
*/
public OffsetDateTime withDayOfMonth(int dayOfMonth, DateResolver dateResolver) {
return with(dateTime.withDayOfMonth(dayOfMonth, dateResolver), offset);
}
/**
* Returns a copy of this {@code OffsetDateTime} with the day-of-year altered.
* If the resulting {@code OffsetDateTime} 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 OffsetDateTime} 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 OffsetDateTime withDayOfYear(int dayOfYear) {
return with(dateTime.withDayOfYear(dayOfYear), offset);
}
// //-----------------------------------------------------------------------
// /**
// * Returns a copy of this OffsetDateTime with a different local date.
// * <p>
// * This method changes the date stored to a different date.
// * No calculation is performed. The result simply represents the same
// * offset and the new date.
// *
// * @param dateProvider the local date to change to, not null
// * @return a new updated OffsetDateTime, not null
// */
// public OffsetDateTime withDate(DateProvider dateProvider) {
// return with(dateTime.with(dateProvider), offset);
// }
/**
* Returns a copy of this {@code OffsetDateTime} with the date values altered.
* <p>
* This method will return a new instance with the same time fields,
* but altered date fields.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @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
* @return an {@code OffsetDateTime} based on this date-time with the requested date, not null
* @throws IllegalCalendarFieldValueException if any field value is invalid
* @throws InvalidCalendarFieldException if the day-of-month is invalid for the month-year
*/
public OffsetDateTime withDate(int year, MonthOfYear monthOfYear, int dayOfMonth) {
return with(dateTime.withDate(year, monthOfYear, dayOfMonth), offset);
}
/**
* Returns a copy of this {@code OffsetDateTime} with the date values altered.
* <p>
* This method will return a new instance with the same time fields,
* but altered date fields.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param year the year to represent, from MIN_VALUE + 1 to MAX_VALUE
* @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
* @return an {@code OffsetDateTime} based on this date-time with the requested date, not null
* @throws IllegalCalendarFieldValueException if any field value is invalid