-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalDate_1151.java
More file actions
2116 lines (1989 loc) · 79.6 KB
/
LocalDate_1151.java
File metadata and controls
2116 lines (1989 loc) · 79.6 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.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;
import org.joda.convert.FromString;
import org.joda.convert.ToString;
import org.joda.time.base.BaseLocal;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.convert.ConverterManager;
import org.joda.time.convert.PartialConverter;
import org.joda.time.field.AbstractReadableInstantFieldProperty;
import org.joda.time.field.FieldUtils;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
/**
* LocalDate is an immutable datetime class representing a date
* without a time zone.
* <p>
* LocalDate implements the {@link ReadablePartial} interface.
* To do this, the interface methods focus on the key fields -
* Year, MonthOfYear and DayOfMonth.
* However, <b>all</b> date fields may in fact be queried.
* <p>
* LocalDate differs from DateMidnight in that this class does not
* have a time zone and does not represent a single instant in time.
* <p>
* Calculations on LocalDate are performed using a {@link Chronology}.
* This chronology will be set internally to be in the UTC time zone
* for all calculations.
*
* <p>Each individual field can be queried in two ways:
* <ul>
* <li><code>getMonthOfYear()</code>
* <li><code>monthOfYear().get()</code>
* </ul>
* The second technique also provides access to other useful methods on the
* field:
* <ul>
* <li>numeric value
* <li>text value
* <li>short text value
* <li>maximum/minimum values
* <li>add/subtract
* <li>set
* <li>rounding
* </ul>
*
* <p>
* LocalDate is thread-safe and immutable, provided that the Chronology is as well.
* All standard Chronology classes supplied are thread-safe and immutable.
*
* @author Stephen Colebourne
* @since 1.3
*/
public final class LocalDate
extends BaseLocal
implements ReadablePartial, Serializable {
/** Serialization lock */
private static final long serialVersionUID = -8775358157899L;
/** The index of the year field in the field array */
private static final int YEAR = 0;
/** The index of the monthOfYear field in the field array */
private static final int MONTH_OF_YEAR = 1;
/** The index of the dayOfMonth field in the field array */
private static final int DAY_OF_MONTH = 2;
/** Set of known duration types. */
private static final Set<DurationFieldType> DATE_DURATION_TYPES = new HashSet<DurationFieldType>();
static {
DATE_DURATION_TYPES.add(DurationFieldType.days());
DATE_DURATION_TYPES.add(DurationFieldType.weeks());
DATE_DURATION_TYPES.add(DurationFieldType.months());
DATE_DURATION_TYPES.add(DurationFieldType.weekyears());
DATE_DURATION_TYPES.add(DurationFieldType.years());
DATE_DURATION_TYPES.add(DurationFieldType.centuries());
// eras are supported, although the DurationField generally isn't
DATE_DURATION_TYPES.add(DurationFieldType.eras());
}
/** The local millis from 1970-01-01T00:00:00 */
private final long iLocalMillis;
/** The chronology to use in UTC. */
private final Chronology iChronology;
/** The cached hash code. */
private transient int iHash;
//-----------------------------------------------------------------------
/**
* Obtains a {@code LocalDate} 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 LocalDate now() {
return new LocalDate();
}
/**
* Obtains a {@code LocalDate} 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 LocalDate now(DateTimeZone zone) {
if (zone == null) {
throw new NullPointerException("Zone must not be null");
}
return new LocalDate(zone);
}
/**
* Obtains a {@code LocalDate} 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 LocalDate now(Chronology chronology) {
if (chronology == null) {
throw new NullPointerException("Chronology must not be null");
}
return new LocalDate(chronology);
}
//-----------------------------------------------------------------------
/**
* Parses a {@code LocalDate} from the specified string.
* <p>
* This uses {@link ISODateTimeFormat#localDateParser()}.
*
* @param str the string to parse, not null
* @since 2.0
*/
@FromString
public static LocalDate parse(String str) {
return parse(str, ISODateTimeFormat.localDateParser());
}
/**
* Parses a {@code LocalDate} 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 LocalDate parse(String str, DateTimeFormatter formatter) {
return formatter.parseLocalDate(str);
}
//-----------------------------------------------------------------------
/**
* Constructs a LocalDate from a <code>java.util.Calendar</code>
* using exactly the same field values.
* <p>
* Each field is queried from the Calendar and assigned to the LocalDate.
* This is useful if you have been using the Calendar as a local date,
* ignoring the zone.
* <p>
* One advantage of this method is that this method is unaffected if the
* version of the time zone data differs between the JDK and Joda-Time.
* That is because the local field values are transferred, calculated using
* the JDK time zone data and without using the Joda-Time time zone data.
* <p>
* This factory method ignores the type of the calendar and always
* creates a LocalDate with ISO chronology. It is expected that you
* will only pass in instances of <code>GregorianCalendar</code> however
* this is not validated.
*
* @param calendar the Calendar to extract fields from, not null
* @return the created local date, not null
* @throws IllegalArgumentException if the calendar is null
* @throws IllegalArgumentException if the date is invalid for the ISO chronology
*/
public static LocalDate fromCalendarFields(Calendar calendar) {
if (calendar == null) {
throw new IllegalArgumentException("The calendar must not be null");
}
int era = calendar.get(Calendar.ERA);
int yearOfEra = calendar.get(Calendar.YEAR);
return new LocalDate(
(era == GregorianCalendar.AD ? yearOfEra : 1 - yearOfEra),
calendar.get(Calendar.MONTH) + 1,
calendar.get(Calendar.DAY_OF_MONTH)
);
}
/**
* Constructs a LocalDate from a <code>java.util.Date</code>
* using exactly the same field values.
* <p>
* Each field is queried from the Date and assigned to the LocalDate.
* This is useful if you have been using the Date as a local date,
* ignoring the zone.
* <p>
* One advantage of this method is that this method is unaffected if the
* version of the time zone data differs between the JDK and Joda-Time.
* That is because the local field values are transferred, calculated using
* the JDK time zone data and without using the Joda-Time time zone data.
* <p>
* This factory method always creates a LocalDate with ISO chronology.
*
* @param date the Date to extract fields from, not null
* @return the created local date, not null
* @throws IllegalArgumentException if the calendar is null
* @throws IllegalArgumentException if the date is invalid for the ISO chronology
*/
@SuppressWarnings("deprecation")
public static LocalDate fromDateFields(Date date) {
if (date == null) {
throw new IllegalArgumentException("The date must not be null");
}
if (date.getTime() < 0) {
// handle years in era BC
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
return fromCalendarFields(cal);
}
return new LocalDate(
date.getYear() + 1900,
date.getMonth() + 1,
date.getDate()
);
}
//-----------------------------------------------------------------------
/**
* Constructs an instance set to the current local time evaluated using
* ISO chronology in the default zone.
* <p>
* Once the constructor is completed, the zone is no longer used.
*
* @see #now()
*/
public LocalDate() {
this(DateTimeUtils.currentTimeMillis(), ISOChronology.getInstance());
}
/**
* Constructs an instance set to the current local time evaluated using
* ISO chronology in the specified zone.
* <p>
* If the specified time zone is null, the default zone is used.
* Once the constructor is completed, the zone is no longer used.
*
* @param zone the time zone, null means default zone
* @see #now(DateTimeZone)
*/
public LocalDate(DateTimeZone zone) {
this(DateTimeUtils.currentTimeMillis(), ISOChronology.getInstance(zone));
}
/**
* Constructs an instance set to the current local time evaluated using
* specified chronology.
* <p>
* If the chronology is null, ISO chronology in the default time zone is used.
* Once the constructor is completed, the zone is no longer used.
*
* @param chronology the chronology, null means ISOChronology in default zone
* @see #now(Chronology)
*/
public LocalDate(Chronology chronology) {
this(DateTimeUtils.currentTimeMillis(), chronology);
}
//-----------------------------------------------------------------------
/**
* Constructs an instance set to the local time defined by the specified
* instant evaluated using ISO chronology in the default zone.
* <p>
* Once the constructor is completed, the zone is no longer used.
*
* @param instant the milliseconds from 1970-01-01T00:00:00Z
*/
public LocalDate(long instant) {
this(instant, ISOChronology.getInstance());
}
/**
* Constructs an instance set to the local time defined by the specified
* instant evaluated using ISO chronology in the specified zone.
* <p>
* If the specified time zone is null, the default zone is used.
* Once the constructor is completed, the zone is no longer used.
*
* @param instant the milliseconds from 1970-01-01T00:00:00Z
* @param zone the time zone, null means default zone
*/
public LocalDate(long instant, DateTimeZone zone) {
this(instant, ISOChronology.getInstance(zone));
}
/**
* Constructs an instance set to the local time defined by the specified
* instant evaluated using the specified chronology.
* <p>
* If the chronology is null, ISO chronology in the default zone is used.
* Once the constructor is completed, the zone is no longer used.
*
* @param instant the milliseconds from 1970-01-01T00:00:00Z
* @param chronology the chronology, null means ISOChronology in default zone
*/
public LocalDate(long instant, Chronology chronology) {
chronology = DateTimeUtils.getChronology(chronology);
long localMillis = chronology.getZone().getMillisKeepLocal(DateTimeZone.UTC, instant);
chronology = chronology.withUTC();
iLocalMillis = chronology.dayOfMonth().roundFloor(localMillis);
iChronology = chronology;
}
//-----------------------------------------------------------------------
/**
* Constructs an instance from an Object that represents a datetime.
* The time zone will be retrieved from the object if possible,
* otherwise the default time zone will be used.
* <p>
* If the object contains no chronology, <code>ISOChronology</code> is used.
* Once the constructor is completed, the zone is no longer used.
* <p>
* The recognised object types are defined in
* {@link org.joda.time.convert.ConverterManager ConverterManager} and
* include ReadablePartial, ReadableInstant, String, Calendar and Date.
* The String formats are described by {@link ISODateTimeFormat#localDateParser()}.
* The default String converter ignores the zone and only parses the field values.
*
* @param instant the datetime object
* @throws IllegalArgumentException if the instant is invalid
*/
public LocalDate(Object instant) {
this(instant, (Chronology) null);
}
/**
* Constructs an instance from an Object that represents a datetime,
* forcing the time zone to that specified.
* <p>
* If the object contains no chronology, <code>ISOChronology</code> is used.
* If the specified time zone is null, the default zone is used.
* Once the constructor is completed, the zone is no longer used.
* <p>
* The recognised object types are defined in
* {@link org.joda.time.convert.ConverterManager ConverterManager} and
* include ReadablePartial, ReadableInstant, String, Calendar and Date.
* The String formats are described by {@link ISODateTimeFormat#localDateParser()}.
* The default String converter ignores the zone and only parses the field values.
*
* @param instant the datetime object
* @param zone the time zone
* @throws IllegalArgumentException if the instant is invalid
*/
public LocalDate(Object instant, DateTimeZone zone) {
PartialConverter converter = ConverterManager.getInstance().getPartialConverter(instant);
Chronology chronology = converter.getChronology(instant, zone);
chronology = DateTimeUtils.getChronology(chronology);
iChronology = chronology.withUTC();
int[] values = converter.getPartialValues(this, instant, chronology, ISODateTimeFormat.localDateParser());
iLocalMillis = iChronology.getDateTimeMillis(values[0], values[1], values[2], 0);
}
/**
* 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.
* Once the constructor is completed, the zone is no longer used.
* If the instant contains a chronology, it will be ignored.
* For example, passing a {@code LocalDate} and a different chronology
* will return a date with the year/month/day from the date applied
* unaltered to the specified chronology.
* <p>
* The recognised object types are defined in
* {@link org.joda.time.convert.ConverterManager ConverterManager} and
* include ReadablePartial, ReadableInstant, String, Calendar and Date.
* The String formats are described by {@link ISODateTimeFormat#localDateParser()}.
* The default String converter ignores the zone and only parses the field values.
*
* @param instant the datetime object
* @param chronology the chronology
* @throws IllegalArgumentException if the instant is invalid
*/
public LocalDate(Object instant, Chronology chronology) {
PartialConverter converter = ConverterManager.getInstance().getPartialConverter(instant);
chronology = converter.getChronology(instant, chronology);
chronology = DateTimeUtils.getChronology(chronology);
iChronology = chronology.withUTC();
int[] values = converter.getPartialValues(this, instant, chronology, ISODateTimeFormat.localDateParser());
iLocalMillis = iChronology.getDateTimeMillis(values[0], values[1], values[2], 0);
}
//-----------------------------------------------------------------------
/**
* Constructs an instance set to the specified date and time
* using <code>ISOChronology</code>.
*
* @param year the year
* @param monthOfYear the month of the year, from 1 to 12
* @param dayOfMonth the day of the month, from 1 to 31
*/
public LocalDate(
int year,
int monthOfYear,
int dayOfMonth) {
this(year, monthOfYear, dayOfMonth, ISOChronology.getInstanceUTC());
}
/**
* Constructs an instance set to the specified date and time
* using the specified chronology, whose zone is ignored.
* <p>
* If the chronology is null, <code>ISOChronology</code> is used.
*
* @param year the year, valid values defined by the chronology
* @param monthOfYear the month of the year, valid values defined by the chronology
* @param dayOfMonth the day of the month, valid values defined by the chronology
* @param chronology the chronology, null means ISOChronology in default zone
*/
public LocalDate(
int year,
int monthOfYear,
int dayOfMonth,
Chronology chronology) {
super();
chronology = DateTimeUtils.getChronology(chronology).withUTC();
long instant = chronology.getDateTimeMillis(year, monthOfYear, dayOfMonth, 0);
iChronology = chronology;
iLocalMillis = instant;
}
/**
* Handle broken serialization from other tools.
* @return the resolved object, not null
*/
private Object readResolve() {
if (iChronology == null) {
return new LocalDate(iLocalMillis, ISOChronology.getInstanceUTC());
}
if (DateTimeZone.UTC.equals(iChronology.getZone()) == false) {
return new LocalDate(iLocalMillis, iChronology.withUTC());
}
return this;
}
//-----------------------------------------------------------------------
/**
* Gets the number of fields in this partial, which is three.
* The supported fields are Year, MonthOfYear and DayOfMonth.
* Note that all fields from day and above may in fact be queried via
* other methods.
*
* @return the field count, three
*/
public int size() {
return 3;
}
/**
* Gets the field for a specific index in the chronology specified.
* <p>
* This method must not use any instance variables.
*
* @param index the index to retrieve
* @param chrono the chronology to use
* @return the field
*/
protected DateTimeField getField(int index, Chronology chrono) {
switch (index) {
case YEAR:
return chrono.year();
case MONTH_OF_YEAR:
return chrono.monthOfYear();
case DAY_OF_MONTH:
return chrono.dayOfMonth();
default:
throw new IndexOutOfBoundsException("Invalid index: " + index);
}
}
/**
* Gets the value of the field at the specifed index.
* <p>
* This method is required to support the <code>ReadablePartial</code>
* interface. The supported fields are Year, MonthOfYear and DayOfMonth.
* Note that all fields from day and above may in fact be queried via
* other methods.
*
* @param index the index, zero to two
* @return the value
* @throws IndexOutOfBoundsException if the index is invalid
*/
public int getValue(int index) {
switch (index) {
case YEAR:
return getChronology().year().get(getLocalMillis());
case MONTH_OF_YEAR:
return getChronology().monthOfYear().get(getLocalMillis());
case DAY_OF_MONTH:
return getChronology().dayOfMonth().get(getLocalMillis());
default:
throw new IndexOutOfBoundsException("Invalid index: " + index);
}
}
//-----------------------------------------------------------------------
/**
* Get the value of one of the fields of a datetime.
* <p>
* This method gets the value of the specified field.
* For example:
* <pre>
* LocalDate dt = LocalDate.nowDefaultZone();
* int year = dt.get(DateTimeFieldType.year());
* </pre>
*
* @param fieldType a field type, usually obtained from DateTimeFieldType, not null
* @return the value of that field
* @throws IllegalArgumentException if the field type is null or unsupported
*/
public int get(DateTimeFieldType fieldType) {
if (fieldType == null) {
throw new IllegalArgumentException("The DateTimeFieldType must not be null");
}
if (isSupported(fieldType) == false) {
throw new IllegalArgumentException("Field '" + fieldType + "' is not supported");
}
return fieldType.getField(getChronology()).get(getLocalMillis());
}
/**
* Checks if the field type specified is supported by this
* local date and chronology.
* This can be used to avoid exceptions in {@link #get(DateTimeFieldType)}.
*
* @param type a field type, usually obtained from DateTimeFieldType
* @return true if the field type is supported
*/
public boolean isSupported(DateTimeFieldType type) {
if (type == null) {
return false;
}
DurationFieldType durType = type.getDurationType();
if (DATE_DURATION_TYPES.contains(durType) ||
durType.getField(getChronology()).getUnitMillis() >=
getChronology().days().getUnitMillis()) {
return type.getField(getChronology()).isSupported();
}
return false;
}
/**
* Checks if the duration type specified is supported by this
* local date and chronology.
*
* @param type a duration type, usually obtained from DurationFieldType
* @return true if the field type is supported
*/
public boolean isSupported(DurationFieldType type) {
if (type == null) {
return false;
}
DurationField field = type.getField(getChronology());
if (DATE_DURATION_TYPES.contains(type) ||
field.getUnitMillis() >= getChronology().days().getUnitMillis()) {
return field.isSupported();
}
return false;
}
//-----------------------------------------------------------------------
/**
* Gets the local milliseconds from the Java epoch
* of 1970-01-01T00:00:00 (not fixed to any specific time zone).
*
* @return the number of milliseconds since 1970-01-01T00:00:00
* @since 1.5 (previously private)
*/
protected long getLocalMillis() {
return iLocalMillis;
}
/**
* Gets the chronology of the date.
*
* @return the Chronology that the date is using
*/
public Chronology getChronology() {
return iChronology;
}
//-----------------------------------------------------------------------
/**
* Compares this ReadablePartial with another returning true if the chronology,
* field types and values are equal.
*
* @param partial an object to check against
* @return true if fields and values are equal
*/
public boolean equals(Object partial) {
// override to perform faster
if (this == partial) {
return true;
}
if (partial instanceof LocalDate) {
LocalDate other = (LocalDate) partial;
if (iChronology.equals(other.iChronology)) {
return iLocalMillis == other.iLocalMillis;
}
}
return super.equals(partial);
}
/**
* Gets a hash code for the instant as defined in <code>ReadablePartial</code>.
*
* @return a suitable hash code
*/
public int hashCode() {
// override for performance
int hash = iHash;
if (hash == 0) {
hash = iHash = super.hashCode();
}
return hash;
}
/**
* Compares this partial with another returning an integer
* indicating the order.
* <p>
* The fields are compared in order, from largest to smallest.
* The first field that is non-equal is used to determine the result.
* <p>
* The specified object must be a partial instance whose field types
* match those of this partial.
*
* @param partial an object to check against
* @return negative if this is less, zero if equal, positive if greater
* @throws ClassCastException if the partial is the wrong class
* or if it has field types that don't match
* @throws NullPointerException if the partial is null
*/
public int compareTo(ReadablePartial partial) {
// override to perform faster
if (this == partial) {
return 0;
}
if (partial instanceof LocalDate) {
LocalDate other = (LocalDate) partial;
if (iChronology.equals(other.iChronology)) {
return (iLocalMillis < other.iLocalMillis ? -1 :
(iLocalMillis == other.iLocalMillis ? 0 : 1));
}
}
return super.compareTo(partial);
}
//-----------------------------------------------------------------------
/**
* Converts this LocalDate to a full datetime at the earliest valid time
* for the date using the default time zone.
* <p>
* The time will normally be midnight, as that is the earliest time on
* any given day. However, in some time zones when Daylight Savings Time
* starts, there is no midnight because time jumps from 11:59 to 01:00.
* This method handles that situation by returning 01:00 on that date.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @return this date as a datetime at the start of the day
* @since 1.5
*/
public DateTime toDateTimeAtStartOfDay() {
return toDateTimeAtStartOfDay(null);
}
/**
* Converts this LocalDate to a full datetime at the earliest valid time
* for the date using the specified time zone.
* <p>
* The time will normally be midnight, as that is the earliest time on
* any given day. However, in some time zones when Daylight Savings Time
* starts, there is no midnight because time jumps from 11:59 to 01:00.
* This method handles that situation by returning 01:00 on that date.
* <p>
* This method uses the chronology from this instance plus the time zone
* specified.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param zone the zone to use, null means default zone
* @return this date as a datetime at the start of the day
* @since 1.5
*/
public DateTime toDateTimeAtStartOfDay(DateTimeZone zone) {
zone = DateTimeUtils.getZone(zone);
Chronology chrono = getChronology().withZone(zone);
long localMillis = getLocalMillis() + 6L * DateTimeConstants.MILLIS_PER_HOUR;
long instant = zone.convertLocalToUTC(localMillis, false);
instant = chrono.dayOfMonth().roundFloor(instant);
return new DateTime(instant, chrono);
}
//-----------------------------------------------------------------------
/**
* Converts this LocalDate to a full datetime at midnight using the default
* time zone.
* <p>
* This method will throw an exception if the default time zone switches
* to Daylight Savings Time at midnight and this LocalDate represents
* that switchover date. The problem is that there is no such time as
* midnight on the required date, and as such an exception is thrown.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @return this date as a datetime at midnight
* @deprecated Use {@link #toDateTimeAtStartOfDay()} which won't throw an exception
*/
@Deprecated
public DateTime toDateTimeAtMidnight() {
return toDateTimeAtMidnight(null);
}
/**
* Converts this LocalDate to a full datetime at midnight using the
* specified time zone.
* <p>
* This method will throw an exception if the time zone switches
* to Daylight Savings Time at midnight and this LocalDate represents
* that switchover date. The problem is that there is no such time as
* midnight on the required date, and as such an exception is thrown.
* <p>
* This method uses the chronology from this instance plus the time zone
* specified.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param zone the zone to use, null means default zone
* @return this date as a datetime at midnight
* @deprecated Use {@link #toDateTimeAtStartOfDay(DateTimeZone)} which won't throw an exception
*/
@Deprecated
public DateTime toDateTimeAtMidnight(DateTimeZone zone) {
zone = DateTimeUtils.getZone(zone);
Chronology chrono = getChronology().withZone(zone);
return new DateTime(getYear(), getMonthOfYear(), getDayOfMonth(), 0, 0, 0, 0, chrono);
}
//-----------------------------------------------------------------------
/**
* Converts this LocalDate to a full datetime using the default time zone
* setting the date fields from this instance and the time fields from
* the current time.
* <p>
* This method will throw an exception if the datetime that would be
* created does not exist when the time zone is taken into account.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @return this date as a datetime with the time as the current time
*/
public DateTime toDateTimeAtCurrentTime() {
return toDateTimeAtCurrentTime(null);
}
/**
* Converts this LocalDate to a full datetime using the specified time zone
* setting the date fields from this instance and the time fields from
* the current time.
* <p>
* This method uses the chronology from this instance plus the time zone
* specified.
* <p>
* This method will throw an exception if the datetime that would be
* created does not exist when the time zone is taken into account.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param zone the zone to use, null means default zone
* @return this date as a datetime with the time as the current time
*/
public DateTime toDateTimeAtCurrentTime(DateTimeZone zone) {
zone = DateTimeUtils.getZone(zone);
Chronology chrono = getChronology().withZone(zone);
long instantMillis = DateTimeUtils.currentTimeMillis();
long resolved = chrono.set(this, instantMillis);
return new DateTime(resolved, chrono);
}
//-----------------------------------------------------------------------
/**
* Converts this LocalDate to a DateMidnight in the default time zone.
* <p>
* As from v1.5, you are recommended to avoid DateMidnight and use
* {@link #toDateTimeAtStartOfDay()} instead because of the exception
* detailed below.
* <p>
* This method will throw an exception if the default time zone switches
* to Daylight Savings Time at midnight and this LocalDate represents
* that switchover date. The problem is that there is no such time as
* midnight on the required date, and as such an exception is thrown.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @return the DateMidnight instance in the default zone
* @deprecated DateMidnight is deprecated
*/
@Deprecated
public DateMidnight toDateMidnight() {
return toDateMidnight(null);
}
/**
* Converts this LocalDate to a DateMidnight.
* <p>
* As from v1.5, you are recommended to avoid DateMidnight and use
* {@link #toDateTimeAtStartOfDay()} instead because of the exception
* detailed below.
* <p>
* This method will throw an exception if the time zone switches
* to Daylight Savings Time at midnight and this LocalDate represents
* that switchover date. The problem is that there is no such time as
* midnight on the required date, and as such an exception is thrown.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param zone the zone to get the DateMidnight in, null means default zone
* @return the DateMidnight instance
* @deprecated DateMidnight is deprecated
*/
@Deprecated
public DateMidnight toDateMidnight(DateTimeZone zone) {
zone = DateTimeUtils.getZone(zone);
Chronology chrono = getChronology().withZone(zone);
return new DateMidnight(getYear(), getMonthOfYear(), getDayOfMonth(), chrono);
}
//-----------------------------------------------------------------------
/**
* Converts this object to a LocalDateTime using a LocalTime to fill in
* the missing fields.
* <p>
* The resulting chronology is determined by the chronology of this
* LocalDate. The chronology of the time must also match.
* If the time is null an exception is thrown.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param time the time of day to use, must not be null
* @return the LocalDateTime instance
* @throws IllegalArgumentException if the time is null
* @throws IllegalArgumentException if the chronology of the time does not match
* @since 1.5
*/
public LocalDateTime toLocalDateTime(LocalTime time) {
if (time == null) {
throw new IllegalArgumentException("The time must not be null");
}
if (getChronology() != time.getChronology()) {
throw new IllegalArgumentException("The chronology of the time does not match");
}
long localMillis = getLocalMillis() + time.getLocalMillis();
return new LocalDateTime(localMillis, getChronology());
}
//-----------------------------------------------------------------------
/**
* Converts this object to a DateTime using a LocalTime to fill in the
* missing fields and using the default time zone.
* <p>
* The resulting chronology is determined by the chronology of this
* LocalDate. The chronology of the time must match.
* If the time is null, the current time in the date's chronology is used.
* <p>
* This method will throw an exception if the datetime that would be
* created does not exist when the time zone is taken into account.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param time the time of day to use, null means current time
* @return the DateTime instance
* @throws IllegalArgumentException if the chronology of the time does not match
*/
public DateTime toDateTime(LocalTime time) {
return toDateTime(time, null);
}
/**
* Converts this object to a DateTime using a LocalTime to fill in the
* missing fields.
* <p>
* The resulting chronology is determined by the chronology of this
* LocalDate plus the time zone. The chronology of the time must match.
* If the time is null, the current time in the date's chronology is used.
* <p>
* This method will throw an exception if the datetime that would be
* created does not exist when the time zone is taken into account.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param time the time of day to use, null means current time
* @param zone the zone to get the DateTime in, null means default
* @return the DateTime instance
* @throws IllegalArgumentException if the chronology of the time does not match
*/
public DateTime toDateTime(LocalTime time, DateTimeZone zone) {
if (time != null && getChronology() != time.getChronology()) {
throw new IllegalArgumentException("The chronology of the time does not match");
}
Chronology chrono = getChronology().withZone(zone);
long instant = DateTimeUtils.currentTimeMillis();
instant = chrono.set(this, instant);
if (time != null) {
instant = chrono.set(time, instant);
}
return new DateTime(instant, chrono);
}
//-----------------------------------------------------------------------
/**
* Converts this object to an Interval representing the whole day
* in the default time zone.
* <p>
* The interval may have more or less than 24 hours if this is a daylight
* savings cutover date.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @return a interval over the day
*/
public Interval toInterval() {
return toInterval(null);
}
/**
* Converts this object to an Interval representing the whole day.
* <p>
* The interval may have more or less than 24 hours if this is a daylight
* savings cutover date.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param zone the zone to get the Interval in, null means default
* @return a interval over the day
*/
public Interval toInterval(DateTimeZone zone) {
zone = DateTimeUtils.getZone(zone);
DateTime start = toDateTimeAtStartOfDay(zone);
DateTime end = plusDays(1).toDateTimeAtStartOfDay(zone);
return new Interval(start, end);
}
//-----------------------------------------------------------------------
/**
* Get the date time as a <code>java.util.Date</code>.
* <p>
* The <code>Date</code> object created has exactly the same year, month and day
* as this date. The time will be set to the earliest valid time for that date.
* <p>
* Converting to a JDK Date is full of complications as the JDK Date constructor
* doesn't behave as you might expect around DST transitions. This method works
* by taking a first guess and then adjusting the JDK date until it has the
* earliest valid instant. This also handles the situation where the JDK time
* zone data differs from the Joda-Time time zone data.
*
* @return a Date initialised with this date, never null
* @since 2.0
*/