1 | /* |
2 | * Copyright 2001-2007 Stephen Colebourne |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.joda.time; |
17 | |
18 | import java.io.IOException; |
19 | import java.io.ObjectInputStream; |
20 | import java.io.ObjectOutputStream; |
21 | import java.io.Serializable; |
22 | import java.util.Calendar; |
23 | import java.util.Date; |
24 | import java.util.Locale; |
25 | |
26 | import org.joda.time.base.BaseLocal; |
27 | import org.joda.time.chrono.ISOChronology; |
28 | import org.joda.time.convert.ConverterManager; |
29 | import org.joda.time.convert.PartialConverter; |
30 | import org.joda.time.field.AbstractReadableInstantFieldProperty; |
31 | import org.joda.time.format.DateTimeFormat; |
32 | import org.joda.time.format.ISODateTimeFormat; |
33 | |
34 | /** |
35 | * LocalDateTime is an unmodifiable datetime class representing a |
36 | * datetime without a time zone. |
37 | * <p> |
38 | * LocalDateTime implements the {@link ReadablePartial} interface. |
39 | * To do this, certain methods focus on key fields Year, MonthOfYear, |
40 | * DayOfYear and MillisOfDay. |
41 | * However, <b>all</b> fields may in fact be queried. |
42 | * <p> |
43 | * Internally, LocalDateTime uses a single millisecond-based value to |
44 | * represent the local datetime. This value is only used internally and |
45 | * is not exposed to applications. |
46 | * <p> |
47 | * Calculations on LocalDateTime are performed using a {@link Chronology}. |
48 | * This chronology will be set internally to be in the UTC time zone |
49 | * for all calculations. |
50 | * |
51 | * <p>Each individual field can be queried in two ways: |
52 | * <ul> |
53 | * <li><code>getHourOfDay()</code> |
54 | * <li><code>hourOfDay().get()</code> |
55 | * </ul> |
56 | * The second technique also provides access to other useful methods on the |
57 | * field: |
58 | * <ul> |
59 | * <li>numeric value |
60 | * <li>text value |
61 | * <li>short text value |
62 | * <li>maximum/minimum values |
63 | * <li>add/subtract |
64 | * <li>set |
65 | * <li>rounding |
66 | * </ul> |
67 | * |
68 | * <p> |
69 | * LocalDateTime is thread-safe and immutable, provided that the Chronology is as well. |
70 | * All standard Chronology classes supplied are thread-safe and immutable. |
71 | * |
72 | * @author Stephen Colebourne |
73 | * @since 1.3 |
74 | */ |
75 | public final class LocalDateTime |
76 | extends BaseLocal |
77 | implements ReadablePartial, Serializable { |
78 | |
79 | /** Serialization lock */ |
80 | private static final long serialVersionUID = -268716875315837168L; |
81 | |
82 | /** The index of the year field in the field array */ |
83 | private static final int YEAR = 0; |
84 | /** The index of the monthOfYear field in the field array */ |
85 | private static final int MONTH_OF_YEAR = 1; |
86 | /** The index of the dayOfMonth field in the field array */ |
87 | private static final int DAY_OF_MONTH = 2; |
88 | /** The index of the millis field in the field array */ |
89 | private static final int MILLIS_OF_DAY = 3; |
90 | |
91 | /** The local millis from 1970-01-01T00:00:00 */ |
92 | private long iLocalMillis; |
93 | /** The chronology to use in UTC */ |
94 | private Chronology iChronology; |
95 | |
96 | //----------------------------------------------------------------------- |
97 | /** |
98 | * Constructs a LocalDateTime from a <code>java.util.Calendar</code> |
99 | * using exactly the same field values avoiding any time zone effects. |
100 | * <p> |
101 | * Each field is queried from the Calendar and assigned to the LocalDateTime. |
102 | * This is useful if you have been using the Calendar as a local date, |
103 | * ignoing the zone. |
104 | * <p> |
105 | * This factory method ignores the type of the calendar and always |
106 | * creates a LocalDateTime with ISO chronology. It is expected that you |
107 | * will only pass in instances of <code>GregorianCalendar</code> however |
108 | * this is not validated. |
109 | * |
110 | * @param calendar the Calendar to extract fields from |
111 | * @return the created LocalDateTime |
112 | * @throws IllegalArgumentException if the calendar is null |
113 | * @throws IllegalArgumentException if the date is invalid for the ISO chronology |
114 | */ |
115 | public static LocalDateTime fromCalendarFields(Calendar calendar) { |
116 | if (calendar == null) { |
117 | throw new IllegalArgumentException("The calendar must not be null"); |
118 | } |
119 | return new LocalDateTime( |
120 | calendar.get(Calendar.YEAR), |
121 | calendar.get(Calendar.MONTH) + 1, |
122 | calendar.get(Calendar.DAY_OF_MONTH), |
123 | calendar.get(Calendar.HOUR_OF_DAY), |
124 | calendar.get(Calendar.MINUTE), |
125 | calendar.get(Calendar.SECOND), |
126 | calendar.get(Calendar.MILLISECOND) |
127 | ); |
128 | } |
129 | |
130 | /** |
131 | * Constructs a LocalDateTime from a <code>java.util.Date</code> |
132 | * using exactly the same field values avoiding any time zone effects. |
133 | * <p> |
134 | * Each field is queried from the Date and assigned to the LocalDateTime. |
135 | * This is useful if you have been using the Date as a local date, |
136 | * ignoing the zone. |
137 | * <p> |
138 | * This factory method always creates a LocalDateTime with ISO chronology. |
139 | * |
140 | * @param date the Date to extract fields from |
141 | * @return the created LocalDateTime |
142 | * @throws IllegalArgumentException if the calendar is null |
143 | * @throws IllegalArgumentException if the date is invalid for the ISO chronology |
144 | */ |
145 | public static LocalDateTime fromDateFields(Date date) { |
146 | if (date == null) { |
147 | throw new IllegalArgumentException("The date must not be null"); |
148 | } |
149 | return new LocalDateTime( |
150 | date.getYear() + 1900, |
151 | date.getMonth() + 1, |
152 | date.getDate(), |
153 | date.getHours(), |
154 | date.getMinutes(), |
155 | date.getSeconds(), |
156 | (int) (date.getTime() % 1000) |
157 | ); |
158 | } |
159 | |
160 | //----------------------------------------------------------------------- |
161 | /** |
162 | * Constructs an instance set to the current local time evaluated using |
163 | * ISO chronology in the default zone. |
164 | * <p> |
165 | * Once the constructor is completed, the zone is no longer used. |
166 | */ |
167 | public LocalDateTime() { |
168 | this(DateTimeUtils.currentTimeMillis(), ISOChronology.getInstance()); |
169 | } |
170 | |
171 | /** |
172 | * Constructs an instance set to the current local time evaluated using |
173 | * ISO chronology in the specified zone. |
174 | * <p> |
175 | * If the specified time zone is null, the default zone is used. |
176 | * Once the constructor is completed, the zone is no longer used. |
177 | * |
178 | * @param zone the time zone, null means default zone |
179 | */ |
180 | public LocalDateTime(DateTimeZone zone) { |
181 | this(DateTimeUtils.currentTimeMillis(), ISOChronology.getInstance(zone)); |
182 | } |
183 | |
184 | /** |
185 | * Constructs an instance set to the current local time evaluated using |
186 | * specified chronology. |
187 | * <p> |
188 | * If the chronology is null, ISO chronology in the default time zone is used. |
189 | * Once the constructor is completed, the zone is no longer used. |
190 | * |
191 | * @param chronology the chronology, null means ISOChronology in default zone |
192 | */ |
193 | public LocalDateTime(Chronology chronology) { |
194 | this(DateTimeUtils.currentTimeMillis(), chronology); |
195 | } |
196 | |
197 | //----------------------------------------------------------------------- |
198 | /** |
199 | * Constructs an instance set to the local time defined by the specified |
200 | * instant evaluated using ISO chronology in the default zone. |
201 | * <p> |
202 | * Once the constructor is completed, the zone is no longer used. |
203 | * |
204 | * @param instant the milliseconds from 1970-01-01T00:00:00Z |
205 | */ |
206 | public LocalDateTime(long instant) { |
207 | this(instant, ISOChronology.getInstance()); |
208 | } |
209 | |
210 | /** |
211 | * Constructs an instance set to the local time defined by the specified |
212 | * instant evaluated using ISO chronology in the specified zone. |
213 | * <p> |
214 | * If the specified time zone is null, the default zone is used. |
215 | * Once the constructor is completed, the zone is no longer used. |
216 | * |
217 | * @param instant the milliseconds from 1970-01-01T00:00:00Z |
218 | * @param zone the time zone, null means default zone |
219 | */ |
220 | public LocalDateTime(long instant, DateTimeZone zone) { |
221 | this(instant, ISOChronology.getInstance(zone)); |
222 | } |
223 | |
224 | /** |
225 | * Constructs an instance set to the local time defined by the specified |
226 | * instant evaluated using the specified chronology. |
227 | * <p> |
228 | * If the chronology is null, ISO chronology in the default zone is used. |
229 | * Once the constructor is completed, the zone is no longer used. |
230 | * |
231 | * @param instant the milliseconds from 1970-01-01T00:00:00Z |
232 | * @param chronology the chronology, null means ISOChronology in default zone |
233 | */ |
234 | public LocalDateTime(long instant, Chronology chronology) { |
235 | chronology = DateTimeUtils.getChronology(chronology); |
236 | |
237 | long localMillis = chronology.getZone().getMillisKeepLocal(DateTimeZone.UTC, instant); |
238 | iLocalMillis = localMillis; |
239 | iChronology = chronology.withUTC(); |
240 | } |
241 | |
242 | //----------------------------------------------------------------------- |
243 | /** |
244 | * Constructs an instance from an Object that represents a datetime. |
245 | * <p> |
246 | * If the object contains no chronology, <code>ISOChronology</code> is used. |
247 | * If the object contains no time zone, the default zone is used. |
248 | * Once the constructor is completed, the zone is no longer used. |
249 | * <p> |
250 | * The recognised object types are defined in |
251 | * {@link org.joda.time.convert.ConverterManager ConverterManager} and |
252 | * include ReadablePartial, ReadableInstant, String, Calendar and Date. |
253 | * The String formats are described by {@link ISODateTimeFormat#localDateOptionalTimeParser()}. |
254 | * The default String converter ignores the zone and only parses the field values. |
255 | * |
256 | * @param instant the datetime object |
257 | * @throws IllegalArgumentException if the instant is invalid |
258 | */ |
259 | public LocalDateTime(Object instant) { |
260 | this(instant, (Chronology) null); |
261 | } |
262 | |
263 | /** |
264 | * Constructs an instance from an Object that represents a datetime, |
265 | * forcing the time zone to that specified. |
266 | * <p> |
267 | * If the object contains no chronology, <code>ISOChronology</code> is used. |
268 | * If the specified time zone is null, the default zone is used. |
269 | * Once the constructor is completed, the zone is no longer used. |
270 | * <p> |
271 | * The recognised object types are defined in |
272 | * {@link org.joda.time.convert.ConverterManager ConverterManager} and |
273 | * include ReadablePartial, ReadableInstant, String, Calendar and Date. |
274 | * The String formats are described by {@link ISODateTimeFormat#localDateOptionalTimeParser()}. |
275 | * The default String converter ignores the zone and only parses the field values. |
276 | * |
277 | * @param instant the datetime object |
278 | * @param zone the time zone |
279 | * @throws IllegalArgumentException if the instant is invalid |
280 | */ |
281 | public LocalDateTime(Object instant, DateTimeZone zone) { |
282 | PartialConverter converter = ConverterManager.getInstance().getPartialConverter(instant); |
283 | Chronology chronology = converter.getChronology(instant, zone); |
284 | chronology = DateTimeUtils.getChronology(chronology); |
285 | iChronology = chronology.withUTC(); |
286 | int[] values = converter.getPartialValues(this, instant, chronology, ISODateTimeFormat.localDateOptionalTimeParser()); |
287 | iLocalMillis = iChronology.getDateTimeMillis(values[0], values[1], values[2], values[3]); |
288 | } |
289 | |
290 | /** |
291 | * Constructs an instance from an Object that represents a datetime, |
292 | * using the specified chronology. |
293 | * <p> |
294 | * If the chronology is null, ISO in the default time zone is used. |
295 | * Once the constructor is completed, the zone is no longer used. |
296 | * <p> |
297 | * The recognised object types are defined in |
298 | * {@link org.joda.time.convert.ConverterManager ConverterManager} and |
299 | * include ReadablePartial, ReadableInstant, String, Calendar and Date. |
300 | * The String formats are described by {@link ISODateTimeFormat#localDateOptionalTimeParser()}. |
301 | * The default String converter ignores the zone and only parses the field values. |
302 | * |
303 | * @param instant the datetime object |
304 | * @param chronology the chronology |
305 | * @throws IllegalArgumentException if the instant is invalid |
306 | */ |
307 | public LocalDateTime(Object instant, Chronology chronology) { |
308 | PartialConverter converter = ConverterManager.getInstance().getPartialConverter(instant); |
309 | chronology = converter.getChronology(instant, chronology); |
310 | chronology = DateTimeUtils.getChronology(chronology); |
311 | iChronology = chronology.withUTC(); |
312 | int[] values = converter.getPartialValues(this, instant, chronology, ISODateTimeFormat.localDateOptionalTimeParser()); |
313 | iLocalMillis = iChronology.getDateTimeMillis(values[0], values[1], values[2], values[3]); |
314 | } |
315 | |
316 | //----------------------------------------------------------------------- |
317 | /** |
318 | * Constructs an instance set to the specified date and time |
319 | * using <code>ISOChronology</code>. |
320 | * |
321 | * @param year the year |
322 | * @param monthOfYear the month of the year |
323 | * @param dayOfMonth the day of the month |
324 | * @param hourOfDay the hour of the day |
325 | * @param minuteOfHour the minute of the hour |
326 | */ |
327 | public LocalDateTime( |
328 | int year, |
329 | int monthOfYear, |
330 | int dayOfMonth, |
331 | int hourOfDay, |
332 | int minuteOfHour) { |
333 | this(year, monthOfYear, dayOfMonth, hourOfDay, |
334 | minuteOfHour, 0, 0, ISOChronology.getInstanceUTC()); |
335 | } |
336 | |
337 | /** |
338 | * Constructs an instance set to the specified date and time |
339 | * using <code>ISOChronology</code>. |
340 | * |
341 | * @param year the year |
342 | * @param monthOfYear the month of the year |
343 | * @param dayOfMonth the day of the month |
344 | * @param hourOfDay the hour of the day |
345 | * @param minuteOfHour the minute of the hour |
346 | * @param secondOfMinute the second of the minute |
347 | */ |
348 | public LocalDateTime( |
349 | int year, |
350 | int monthOfYear, |
351 | int dayOfMonth, |
352 | int hourOfDay, |
353 | int minuteOfHour, |
354 | int secondOfMinute) { |
355 | this(year, monthOfYear, dayOfMonth, hourOfDay, |
356 | minuteOfHour, secondOfMinute, 0, ISOChronology.getInstanceUTC()); |
357 | } |
358 | |
359 | /** |
360 | * Constructs an instance set to the specified date and time |
361 | * using <code>ISOChronology</code>. |
362 | * |
363 | * @param year the year |
364 | * @param monthOfYear the month of the year |
365 | * @param dayOfMonth the day of the month |
366 | * @param hourOfDay the hour of the day |
367 | * @param minuteOfHour the minute of the hour |
368 | * @param secondOfMinute the second of the minute |
369 | * @param millisOfSecond the millisecond of the second |
370 | */ |
371 | public LocalDateTime( |
372 | int year, |
373 | int monthOfYear, |
374 | int dayOfMonth, |
375 | int hourOfDay, |
376 | int minuteOfHour, |
377 | int secondOfMinute, |
378 | int millisOfSecond) { |
379 | this(year, monthOfYear, dayOfMonth, hourOfDay, |
380 | minuteOfHour, secondOfMinute, millisOfSecond, ISOChronology.getInstanceUTC()); |
381 | } |
382 | |
383 | /** |
384 | * Constructs an instance set to the specified date and time |
385 | * using the specified chronology, whose zone is ignored. |
386 | * <p> |
387 | * If the chronology is null, <code>ISOChronology</code> is used. |
388 | * |
389 | * @param year the year |
390 | * @param monthOfYear the month of the year |
391 | * @param dayOfMonth the day of the month |
392 | * @param hourOfDay the hour of the day |
393 | * @param minuteOfHour the minute of the hour |
394 | * @param secondOfMinute the second of the minute |
395 | * @param millisOfSecond the millisecond of the second |
396 | * @param chronology the chronology, null means ISOChronology in default zone |
397 | */ |
398 | public LocalDateTime( |
399 | int year, |
400 | int monthOfYear, |
401 | int dayOfMonth, |
402 | int hourOfDay, |
403 | int minuteOfHour, |
404 | int secondOfMinute, |
405 | int millisOfSecond, |
406 | Chronology chronology) { |
407 | super(); |
408 | chronology = DateTimeUtils.getChronology(chronology).withUTC(); |
409 | long instant = chronology.getDateTimeMillis(year, monthOfYear, dayOfMonth, |
410 | hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond); |
411 | iChronology = chronology; |
412 | iLocalMillis = instant; |
413 | } |
414 | |
415 | //----------------------------------------------------------------------- |
416 | /** |
417 | * Gets the number of fields in this partial, which is four. |
418 | * The supported fields are Year, MonthOfDay, DayOfMonth and MillisOfDay. |
419 | * |
420 | * @return the field count, four |
421 | */ |
422 | public int size() { |
423 | return 4; |
424 | } |
425 | |
426 | /** |
427 | * Gets the field for a specific index in the chronology specified. |
428 | * <p> |
429 | * This method must not use any instance variables. |
430 | * |
431 | * @param index the index to retrieve |
432 | * @param chrono the chronology to use |
433 | * @return the field |
434 | */ |
435 | protected DateTimeField getField(int index, Chronology chrono) { |
436 | switch (index) { |
437 | case YEAR: |
438 | return chrono.year(); |
439 | case MONTH_OF_YEAR: |
440 | return chrono.monthOfYear(); |
441 | case DAY_OF_MONTH: |
442 | return chrono.dayOfMonth(); |
443 | case MILLIS_OF_DAY: |
444 | return chrono.millisOfDay(); |
445 | default: |
446 | throw new IndexOutOfBoundsException("Invalid index: " + index); |
447 | } |
448 | } |
449 | |
450 | /** |
451 | * Gets the value of the field at the specifed index. |
452 | * <p> |
453 | * This method is required to support the <code>ReadablePartial</code> |
454 | * interface. The supported fields are Year, MonthOfDay, DayOfMonth and MillisOfDay. |
455 | * |
456 | * @param index the index, zero to two |
457 | * @return the value |
458 | * @throws IndexOutOfBoundsException if the index is invalid |
459 | */ |
460 | public int getValue(int index) { |
461 | switch (index) { |
462 | case YEAR: |
463 | return getChronology().year().get(getLocalMillis()); |
464 | case MONTH_OF_YEAR: |
465 | return getChronology().monthOfYear().get(getLocalMillis()); |
466 | case DAY_OF_MONTH: |
467 | return getChronology().dayOfMonth().get(getLocalMillis()); |
468 | case MILLIS_OF_DAY: |
469 | return getChronology().millisOfDay().get(getLocalMillis()); |
470 | default: |
471 | throw new IndexOutOfBoundsException("Invalid index: " + index); |
472 | } |
473 | } |
474 | |
475 | //----------------------------------------------------------------------- |
476 | /** |
477 | * Get the value of one of the fields of a datetime. |
478 | * <p> |
479 | * This method gets the value of the specified field. |
480 | * For example: |
481 | * <pre> |
482 | * DateTime dt = new DateTime(); |
483 | * int year = dt.get(DateTimeFieldType.year()); |
484 | * </pre> |
485 | * |
486 | * @param type a field type, usually obtained from DateTimeFieldType, not null |
487 | * @return the value of that field |
488 | * @throws IllegalArgumentException if the field type is null |
489 | */ |
490 | public int get(DateTimeFieldType type) { |
491 | if (type == null) { |
492 | throw new IllegalArgumentException("The DateTimeFieldType must not be null"); |
493 | } |
494 | return type.getField(getChronology()).get(getLocalMillis()); |
495 | } |
496 | |
497 | /** |
498 | * Checks if the field type specified is supported by this |
499 | * local datetime and chronology. |
500 | * This can be used to avoid exceptions in {@link #get(DateTimeFieldType)}. |
501 | * |
502 | * @param type a field type, usually obtained from DateTimeFieldType |
503 | * @return true if the field type is supported |
504 | */ |
505 | public boolean isSupported(DateTimeFieldType type) { |
506 | if (type == null) { |
507 | return false; |
508 | } |
509 | return type.getField(getChronology()).isSupported(); |
510 | } |
511 | |
512 | /** |
513 | * Checks if the duration type specified is supported by this |
514 | * local datetime and chronology. |
515 | * |
516 | * @param type a duration type, usually obtained from DurationFieldType |
517 | * @return true if the field type is supported |
518 | */ |
519 | public boolean isSupported(DurationFieldType type) { |
520 | if (type == null) { |
521 | return false; |
522 | } |
523 | return type.getField(getChronology()).isSupported(); |
524 | } |
525 | |
526 | //----------------------------------------------------------------------- |
527 | /** |
528 | * Gets the milliseconds of the datetime instant from the Java epoch |
529 | * of 1970-01-01T00:00:00 (not fixed to any specific time zone). |
530 | * |
531 | * @return the number of milliseconds since 1970-01-01T00:00:00 |
532 | * @since 1.5 (previously private) |
533 | */ |
534 | protected long getLocalMillis() { |
535 | return iLocalMillis; |
536 | } |
537 | |
538 | /** |
539 | * Gets the chronology of the datetime. |
540 | * |
541 | * @return the Chronology that the datetime is using |
542 | */ |
543 | public Chronology getChronology() { |
544 | return iChronology; |
545 | } |
546 | |
547 | //----------------------------------------------------------------------- |
548 | /** |
549 | * Compares this ReadablePartial with another returning true if the chronology, |
550 | * field types and values are equal. |
551 | * |
552 | * @param partial an object to check against |
553 | * @return true if fields and values are equal |
554 | */ |
555 | public boolean equals(Object partial) { |
556 | // override to perform faster |
557 | if (this == partial) { |
558 | return true; |
559 | } |
560 | if (partial instanceof LocalDateTime) { |
561 | LocalDateTime other = (LocalDateTime) partial; |
562 | if (iChronology.equals(other.iChronology)) { |
563 | return iLocalMillis == other.iLocalMillis; |
564 | } |
565 | } |
566 | return super.equals(partial); |
567 | } |
568 | |
569 | /** |
570 | * Compares this partial with another returning an integer |
571 | * indicating the order. |
572 | * <p> |
573 | * The fields are compared in order, from largest to smallest. |
574 | * The first field that is non-equal is used to determine the result. |
575 | * <p> |
576 | * The specified object must be a partial instance whose field types |
577 | * match those of this partial. |
578 | * <p> |
579 | * NOTE: This implementation violates the Comparable contract. |
580 | * This method will accept any instance of ReadablePartial as input. |
581 | * However, it is possible that some implementations of ReadablePartial |
582 | * exist that do not extend AbstractPartial, and thus will throw a |
583 | * ClassCastException if compared in the opposite direction. |
584 | * The cause of this problem is that ReadablePartial doesn't define |
585 | * the compareTo() method, however we can't change that until v2.0. |
586 | * |
587 | * @param partial an object to check against |
588 | * @return negative if this is less, zero if equal, positive if greater |
589 | * @throws ClassCastException if the partial is the wrong class |
590 | * or if it has field types that don't match |
591 | * @throws NullPointerException if the partial is null |
592 | */ |
593 | public int compareTo(Object partial) { |
594 | // override to perform faster |
595 | if (this == partial) { |
596 | return 0; |
597 | } |
598 | if (partial instanceof LocalDateTime) { |
599 | LocalDateTime other = (LocalDateTime) partial; |
600 | if (iChronology.equals(other.iChronology)) { |
601 | return (iLocalMillis < other.iLocalMillis ? -1 : |
602 | (iLocalMillis == other.iLocalMillis ? 0 : 1)); |
603 | |
604 | } |
605 | } |
606 | return super.compareTo(partial); |
607 | } |
608 | |
609 | //----------------------------------------------------------------------- |
610 | /** |
611 | * Converts this object to a DateTime using the default zone. |
612 | * <p> |
613 | * This method will throw an exception if the datetime that would be |
614 | * created does not exist when the time zone is taken into account. |
615 | * |
616 | * @return <code>this</code> |
617 | */ |
618 | public DateTime toDateTime() { |
619 | return toDateTime((DateTimeZone) null); |
620 | } |
621 | |
622 | /** |
623 | * Converts this object to a DateTime using the specified zone. |
624 | * <p> |
625 | * This method will throw an exception if the datetime that would be |
626 | * created does not exist when the time zone is taken into account. |
627 | * |
628 | * @param zone time zone to apply, or default if null |
629 | * @return a DateTime using the same millis |
630 | */ |
631 | public DateTime toDateTime(DateTimeZone zone) { |
632 | zone = DateTimeUtils.getZone(zone); |
633 | Chronology chrono = iChronology.withZone(zone); |
634 | return new DateTime( |
635 | getYear(), getMonthOfYear(), getDayOfMonth(), |
636 | getHourOfDay(), getMinuteOfHour(), |
637 | getSecondOfMinute(), getMillisOfSecond(), chrono); |
638 | } |
639 | |
640 | //----------------------------------------------------------------------- |
641 | /** |
642 | * Converts this object to a LocalDate with the same date and chronology. |
643 | * |
644 | * @return a LocalDate with the same date and chronology |
645 | */ |
646 | public LocalDate toLocalDate() { |
647 | return new LocalDate(getLocalMillis(), getChronology()); |
648 | } |
649 | |
650 | /** |
651 | * Converts this object to a LocalTime with the same time and chronology. |
652 | * |
653 | * @return a LocalTime with the same time and chronology |
654 | */ |
655 | public LocalTime toLocalTime() { |
656 | return new LocalTime(getLocalMillis(), getChronology()); |
657 | } |
658 | |
659 | //----------------------------------------------------------------------- |
660 | /** |
661 | * Returns a copy of this datetime with different local millis. |
662 | * <p> |
663 | * The returned object will be a new instance of the same type. |
664 | * Only the millis will change, the chronology is kept. |
665 | * The returned object will be either be a new instance or <code>this</code>. |
666 | * |
667 | * @param newMillis the new millis, from 1970-01-01T00:00:00 |
668 | * @return a copy of this datetime with different millis |
669 | */ |
670 | LocalDateTime withLocalMillis(long newMillis) { |
671 | return (newMillis == getLocalMillis() ? this : new LocalDateTime(newMillis, getChronology())); |
672 | } |
673 | |
674 | //----------------------------------------------------------------------- |
675 | /** |
676 | * Returns a copy of this datetime with the specified date, |
677 | * retaining the time fields. |
678 | * <p> |
679 | * If the date is already the date passed in, then <code>this</code> is returned. |
680 | * <p> |
681 | * To set a single field use the properties, for example: |
682 | * <pre> |
683 | * DateTime set = dt.monthOfYear().setCopy(6); |
684 | * </pre> |
685 | * |
686 | * @param year the new year value |
687 | * @param monthOfYear the new monthOfYear value |
688 | * @param dayOfMonth the new dayOfMonth value |
689 | * @return a copy of this datetime with a different date |
690 | * @throws IllegalArgumentException if any value if invalid |
691 | */ |
692 | public LocalDateTime withDate(int year, int monthOfYear, int dayOfMonth) { |
693 | Chronology chrono = getChronology(); |
694 | long instant = getLocalMillis(); |
695 | instant = chrono.year().set(instant, year); |
696 | instant = chrono.monthOfYear().set(instant, monthOfYear); |
697 | instant = chrono.dayOfMonth().set(instant, dayOfMonth); |
698 | return withLocalMillis(instant); |
699 | } |
700 | |
701 | /** |
702 | * Returns a copy of this datetime with the specified time, |
703 | * retaining the date fields. |
704 | * <p> |
705 | * If the time is already the time passed in, then <code>this</code> is returned. |
706 | * <p> |
707 | * To set a single field use the properties, for example: |
708 | * <pre> |
709 | * LocalDateTime set = dt.hourOfDay().setCopy(6); |
710 | * </pre> |
711 | * |
712 | * @param hourOfDay the hour of the day |
713 | * @param minuteOfHour the minute of the hour |
714 | * @param secondOfMinute the second of the minute |
715 | * @param millisOfSecond the millisecond of the second |
716 | * @return a copy of this datetime with a different time |
717 | * @throws IllegalArgumentException if any value if invalid |
718 | */ |
719 | public LocalDateTime withTime(int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond) { |
720 | Chronology chrono = getChronology(); |
721 | long instant = getLocalMillis(); |
722 | instant = chrono.hourOfDay().set(instant, hourOfDay); |
723 | instant = chrono.minuteOfHour().set(instant, minuteOfHour); |
724 | instant = chrono.secondOfMinute().set(instant, secondOfMinute); |
725 | instant = chrono.millisOfSecond().set(instant, millisOfSecond); |
726 | return withLocalMillis(instant); |
727 | } |
728 | |
729 | //----------------------------------------------------------------------- |
730 | /** |
731 | * Returns a copy of this datetime with the partial set of fields |
732 | * replacing those from this instance. |
733 | * <p> |
734 | * For example, if the partial is a <code>TimeOfDay</code> then the time fields |
735 | * would be changed in the returned instance. |
736 | * If the partial is null, then <code>this</code> is returned. |
737 | * |
738 | * @param partial the partial set of fields to apply to this datetime, null ignored |
739 | * @return a copy of this datetime with a different set of fields |
740 | * @throws IllegalArgumentException if any value is invalid |
741 | */ |
742 | public LocalDateTime withFields(ReadablePartial partial) { |
743 | if (partial == null) { |
744 | return this; |
745 | } |
746 | return withLocalMillis(getChronology().set(partial, getLocalMillis())); |
747 | } |
748 | |
749 | /** |
750 | * Returns a copy of this datetime with the specified field set to a new value. |
751 | * <p> |
752 | * For example, if the field type is <code>hourOfDay</code> then the hour of day |
753 | * field would be changed in the returned instance. |
754 | * If the field type is null, then <code>this</code> is returned. |
755 | * <p> |
756 | * These three lines are equivalent: |
757 | * <pre> |
758 | * LocalDateTime updated = dt.withField(DateTimeFieldType.dayOfMonth(), 6); |
759 | * LocalDateTime updated = dt.dayOfMonth().setCopy(6); |
760 | * LocalDateTime updated = dt.property(DateTimeFieldType.dayOfMonth()).setCopy(6); |
761 | * </pre> |
762 | * |
763 | * @param fieldType the field type to set, not null |
764 | * @param value the value to set |
765 | * @return a copy of this datetime with the field set |
766 | * @throws IllegalArgumentException if the value is null or invalid |
767 | */ |
768 | public LocalDateTime withField(DateTimeFieldType fieldType, int value) { |
769 | if (fieldType == null) { |
770 | throw new IllegalArgumentException("Field must not be null"); |
771 | } |
772 | long instant = fieldType.getField(getChronology()).set(getLocalMillis(), value); |
773 | return withLocalMillis(instant); |
774 | } |
775 | |
776 | /** |
777 | * Returns a copy of this datetime with the value of the specified |
778 | * field increased. |
779 | * <p> |
780 | * If the addition is zero or the field is null, then <code>this</code> is returned. |
781 | * <p> |
782 | * These three lines are equivalent: |
783 | * <pre> |
784 | * LocalDateTime added = dt.withFieldAdded(DurationFieldType.years(), 6); |
785 | * LocalDateTime added = dt.plusYears(6); |
786 | * LocalDateTime added = dt.plus(Period.years(6)); |
787 | * </pre> |
788 | * |
789 | * @param fieldType the field type to add to, not null |
790 | * @param amount the amount to add |
791 | * @return a copy of this datetime with the field updated |
792 | * @throws IllegalArgumentException if the value is null or invalid |
793 | * @throws ArithmeticException if the result exceeds the internal capacity |
794 | */ |
795 | public LocalDateTime withFieldAdded(DurationFieldType fieldType, int amount) { |
796 | if (fieldType == null) { |
797 | throw new IllegalArgumentException("Field must not be null"); |
798 | } |
799 | if (amount == 0) { |
800 | return this; |
801 | } |
802 | long instant = fieldType.getField(getChronology()).add(getLocalMillis(), amount); |
803 | return withLocalMillis(instant); |
804 | } |
805 | |
806 | //----------------------------------------------------------------------- |
807 | /** |
808 | * Returns a copy of this datetime with the specified duration added. |
809 | * <p> |
810 | * If the addition is zero, then <code>this</code> is returned. |
811 | * |
812 | * @param durationToAdd the duration to add to this one, null means zero |
813 | * @param scalar the amount of times to add, such as -1 to subtract once |
814 | * @return a copy of this datetime with the duration added |
815 | * @throws ArithmeticException if the result exceeds the internal capacity |
816 | */ |
817 | public LocalDateTime withDurationAdded(ReadableDuration durationToAdd, int scalar) { |
818 | if (durationToAdd == null || scalar == 0) { |
819 | return this; |
820 | } |
821 | long instant = getChronology().add(getLocalMillis(), durationToAdd.getMillis(), scalar); |
822 | return withLocalMillis(instant); |
823 | } |
824 | |
825 | /** |
826 | * Returns a copy of this datetime with the specified period added. |
827 | * <p> |
828 | * If the addition is zero, then <code>this</code> is returned. |
829 | * <p> |
830 | * This method is typically used to add multiple copies of complex |
831 | * period instances. Adding one field is best achieved using methods |
832 | * like {@link #withFieldAdded(DurationFieldType, int)} |
833 | * or {@link #plusYears(int)}. |
834 | * |
835 | * @param period the period to add to this one, null means zero |
836 | * @param scalar the amount of times to add, such as -1 to subtract once |
837 | * @return a copy of this datetime with the period added |
838 | * @throws ArithmeticException if the result exceeds the internal capacity |
839 | */ |
840 | public LocalDateTime withPeriodAdded(ReadablePeriod period, int scalar) { |
841 | if (period == null || scalar == 0) { |
842 | return this; |
843 | } |
844 | long instant = getChronology().add(period, getLocalMillis(), scalar); |
845 | return withLocalMillis(instant); |
846 | } |
847 | |
848 | //----------------------------------------------------------------------- |
849 | /** |
850 | * Returns a copy of this datetime with the specified duration added. |
851 | * <p> |
852 | * If the amount is zero or null, then <code>this</code> is returned. |
853 | * |
854 | * @param duration the duration to add to this one, null means zero |
855 | * @return a copy of this datetime with the duration added |
856 | * @throws ArithmeticException if the result exceeds the internal capacity |
857 | */ |
858 | public LocalDateTime plus(ReadableDuration duration) { |
859 | return withDurationAdded(duration, 1); |
860 | } |
861 | |
862 | /** |
863 | * Returns a copy of this datetime with the specified period added. |
864 | * <p> |
865 | * If the amount is zero or null, then <code>this</code> is returned. |
866 | * <p> |
867 | * This method is typically used to add complex period instances. |
868 | * Adding one field is best achieved using methods |
869 | * like {@link #plusYears(int)}. |
870 | * |
871 | * @param period the period to add to this one, null means zero |
872 | * @return a copy of this datetime with the period added |
873 | * @throws ArithmeticException if the result exceeds the internal capacity |
874 | */ |
875 | public LocalDateTime plus(ReadablePeriod period) { |
876 | return withPeriodAdded(period, 1); |
877 | } |
878 | |
879 | //----------------------------------------------------------------------- |
880 | /** |
881 | * Returns a copy of this datetime plus the specified number of years. |
882 | * <p> |
883 | * This LocalDateTime instance is immutable and unaffected by this method call. |
884 | * <p> |
885 | * The following three lines are identical in effect: |
886 | * <pre> |
887 | * LocalDateTime added = dt.plusYears(6); |
888 | * LocalDateTime added = dt.plus(Period.years(6)); |
889 | * LocalDateTime added = dt.withFieldAdded(DurationFieldType.years(), 6); |
890 | * </pre> |
891 | * |
892 | * @param years the amount of years to add, may be negative |
893 | * @return the new LocalDateTime plus the increased years |
894 | */ |
895 | public LocalDateTime plusYears(int years) { |
896 | if (years == 0) { |
897 | return this; |
898 | } |
899 | long instant = getChronology().years().add(getLocalMillis(), years); |
900 | return withLocalMillis(instant); |
901 | } |
902 | |
903 | /** |
904 | * Returns a copy of this datetime plus the specified number of months. |
905 | * <p> |
906 | * This LocalDateTime instance is immutable and unaffected by this method call. |
907 | * <p> |
908 | * The following three lines are identical in effect: |
909 | * <pre> |
910 | * LocalDateTime added = dt.plusMonths(6); |
911 | * LocalDateTime added = dt.plus(Period.months(6)); |
912 | * LocalDateTime added = dt.withFieldAdded(DurationFieldType.months(), 6); |
913 | * </pre> |
914 | * |
915 | * @param months the amount of months to add, may be negative |
916 | * @return the new LocalDateTime plus the increased months |
917 | */ |
918 | public LocalDateTime plusMonths(int months) { |
919 | if (months == 0) { |
920 | return this; |
921 | } |
922 | long instant = getChronology().months().add(getLocalMillis(), months); |
923 | return withLocalMillis(instant); |
924 | } |
925 | |
926 | /** |
927 | * Returns a copy of this datetime plus the specified number of weeks. |
928 | * <p> |
929 | * This LocalDateTime instance is immutable and unaffected by this method call. |
930 | * <p> |
931 | * The following three lines are identical in effect: |
932 | * <pre> |
933 | * LocalDateTime added = dt.plusWeeks(6); |
934 | * LocalDateTime added = dt.plus(Period.weeks(6)); |
935 | * LocalDateTime added = dt.withFieldAdded(DurationFieldType.weeks(), 6); |
936 | * </pre> |
937 | * |
938 | * @param weeks the amount of weeks to add, may be negative |
939 | * @return the new LocalDateTime plus the increased weeks |
940 | */ |
941 | public LocalDateTime plusWeeks(int weeks) { |
942 | if (weeks == 0) { |
943 | return this; |
944 | } |
945 | long instant = getChronology().weeks().add(getLocalMillis(), weeks); |
946 | return withLocalMillis(instant); |
947 | } |
948 | |
949 | /** |
950 | * Returns a copy of this datetime plus the specified number of days. |
951 | * <p> |
952 | * This LocalDateTime instance is immutable and unaffected by this method call. |
953 | * <p> |
954 | * The following three lines are identical in effect: |
955 | * <pre> |
956 | * LocalDateTime added = dt.plusDays(6); |
957 | * LocalDateTime added = dt.plus(Period.days(6)); |
958 | * LocalDateTime added = dt.withFieldAdded(DurationFieldType.days(), 6); |
959 | * </pre> |
960 | * |
961 | * @param days the amount of days to add, may be negative |
962 | * @return the new LocalDateTime plus the increased days |
963 | */ |
964 | public LocalDateTime plusDays(int days) { |
965 | if (days == 0) { |
966 | return this; |
967 | } |
968 | long instant = getChronology().days().add(getLocalMillis(), days); |
969 | return withLocalMillis(instant); |
970 | } |
971 | |
972 | //----------------------------------------------------------------------- |
973 | /** |
974 | * Returns a copy of this datetime plus the specified number of hours. |
975 | * <p> |
976 | * This LocalDateTime instance is immutable and unaffected by this method call. |
977 | * <p> |
978 | * The following three lines are identical in effect: |
979 | * <pre> |
980 | * LocalDateTime added = dt.plusHours(6); |
981 | * LocalDateTime added = dt.plus(Period.hours(6)); |
982 | * LocalDateTime added = dt.withFieldAdded(DurationFieldType.hours(), 6); |
983 | * </pre> |
984 | * |
985 | * @param hours the amount of hours to add, may be negative |
986 | * @return the new LocalDateTime plus the increased hours |
987 | */ |
988 | public LocalDateTime plusHours(int hours) { |
989 | if (hours == 0) { |
990 | return this; |
991 | } |
992 | long instant = getChronology().hours().add(getLocalMillis(), hours); |
993 | return withLocalMillis(instant); |
994 | } |
995 | |
996 | /** |
997 | * Returns a copy of this datetime plus the specified number of minutes. |
998 | * <p> |
999 | * This LocalDateTime instance is immutable and unaffected by this method call. |
1000 | * <p> |
1001 | * The following three lines are identical in effect: |
1002 | * <pre> |
1003 | * LocalDateTime added = dt.plusMinutes(6); |
1004 | * LocalDateTime added = dt.plus(Period.minutes(6)); |
1005 | * LocalDateTime added = dt.withFieldAdded(DurationFieldType.minutes(), 6); |
1006 | * </pre> |
1007 | * |
1008 | * @param minutes the amount of minutes to add, may be negative |
1009 | * @return the new LocalDateTime plus the increased minutes |
1010 | */ |
1011 | public LocalDateTime plusMinutes(int minutes) { |
1012 | if (minutes == 0) { |
1013 | return this; |
1014 | } |
1015 | long instant = getChronology().minutes().add(getLocalMillis(), minutes); |
1016 | return withLocalMillis(instant); |
1017 | } |
1018 | |
1019 | /** |
1020 | * Returns a copy of this datetime plus the specified number of seconds. |
1021 | * <p> |
1022 | * This LocalDateTime instance is immutable and unaffected by this method call. |
1023 | * <p> |
1024 | * The following three lines are identical in effect: |
1025 | * <pre> |
1026 | * LocalDateTime added = dt.plusSeconds(6); |
1027 | * LocalDateTime added = dt.plus(Period.seconds(6)); |
1028 | * LocalDateTime added = dt.withFieldAdded(DurationFieldType.seconds(), 6); |
1029 | * </pre> |
1030 | * |
1031 | * @param seconds the amount of seconds to add, may be negative |
1032 | * @return the new LocalDateTime plus the increased seconds |
1033 | */ |
1034 | public LocalDateTime plusSeconds(int seconds) { |
1035 | if (seconds == 0) { |
1036 | return this; |
1037 | } |
1038 | long instant = getChronology().seconds().add(getLocalMillis(), seconds); |
1039 | return withLocalMillis(instant); |
1040 | } |
1041 | |
1042 | /** |
1043 | * Returns a copy of this datetime plus the specified number of millis. |
1044 | * <p> |
1045 | * This LocalDateTime instance is immutable and unaffected by this method call. |
1046 | * <p> |
1047 | * The following three lines are identical in effect: |
1048 | * <pre> |
1049 | * LocalDateTime added = dt.plusMillis(6); |
1050 | * LocalDateTime added = dt.plus(Period.millis(6)); |
1051 | * LocalDateTime added = dt.withFieldAdded(DurationFieldType.millis(), 6); |
1052 | * </pre> |
1053 | * |
1054 | * @param millis the amount of millis to add, may be negative |
1055 | * @return the new LocalDateTime plus the increased millis |
1056 | */ |
1057 | public LocalDateTime plusMillis(int millis) { |
1058 | if (millis == 0) { |
1059 | return this; |
1060 | } |
1061 | long instant = getChronology().millis().add(getLocalMillis(), millis); |
1062 | return withLocalMillis(instant); |
1063 | } |
1064 | |
1065 | //----------------------------------------------------------------------- |
1066 | /** |
1067 | * Returns a copy of this datetime with the specified duration taken away. |
1068 | * <p> |
1069 | * If the amount is zero or null, then <code>this</code> is returned. |
1070 | * |
1071 | * @param duration the duration to reduce this instant by |
1072 | * @return a copy of this datetime with the duration taken away |
1073 | * @throws ArithmeticException if the result exceeds the internal capacity |
1074 | */ |
1075 | public LocalDateTime minus(ReadableDuration duration) { |
1076 | return withDurationAdded(duration, -1); |
1077 | } |
1078 | |
1079 | /** |
1080 | * Returns a copy of this datetime with the specified period taken away. |
1081 | * <p> |
1082 | * If the amount is zero or null, then <code>this</code> is returned. |
1083 | * <p> |
1084 | * This method is typically used to subtract complex period instances. |
1085 | * Subtracting one field is best achieved using methods |
1086 | * like {@link #minusYears(int)}. |
1087 | * |
1088 | * @param period the period to reduce this instant by |
1089 | * @return a copy of this datetime with the period taken away |
1090 | * @throws ArithmeticException if the result exceeds the internal capacity |
1091 | */ |
1092 | public LocalDateTime minus(ReadablePeriod period) { |
1093 | return withPeriodAdded(period, -1); |
1094 | } |
1095 | |
1096 | //----------------------------------------------------------------------- |
1097 | /** |
1098 | * Returns a copy of this datetime minus the specified number of years. |
1099 | * <p> |
1100 | * This LocalDateTime instance is immutable and unaffected by this method call. |
1101 | * <p> |
1102 | * The following three lines are identical in effect: |
1103 | * <pre> |
1104 | * LocalDateTime subtracted = dt.minusYears(6); |
1105 | * LocalDateTime subtracted = dt.minus(Period.years(6)); |
1106 | * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.years(), -6); |
1107 | * </pre> |
1108 | * |
1109 | * @param years the amount of years to subtract, may be negative |
1110 | * @return the new LocalDateTime minus the increased years |
1111 | */ |
1112 | public LocalDateTime minusYears(int years) { |
1113 | if (years == 0) { |
1114 | return this; |
1115 | } |
1116 | long instant = getChronology().years().subtract(getLocalMillis(), years); |
1117 | return withLocalMillis(instant); |
1118 | } |
1119 | |
1120 | /** |
1121 | * Returns a copy of this datetime minus the specified number of months. |
1122 | * <p> |
1123 | * This LocalDateTime instance is immutable and unaffected by this method call. |
1124 | * <p> |
1125 | * The following three lines are identical in effect: |
1126 | * <pre> |
1127 | * LocalDateTime subtracted = dt.minusMonths(6); |
1128 | * LocalDateTime subtracted = dt.minus(Period.months(6)); |
1129 | * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.months(), -6); |
1130 | * </pre> |
1131 | * |
1132 | * @param months the amount of months to subtract, may be negative |
1133 | * @return the new LocalDateTime minus the increased months |
1134 | */ |
1135 | public LocalDateTime minusMonths(int months) { |
1136 | if (months == 0) { |
1137 | return this; |
1138 | } |
1139 | long instant = getChronology().months().subtract(getLocalMillis(), months); |
1140 | return withLocalMillis(instant); |
1141 | } |
1142 | |
1143 | /** |
1144 | * Returns a copy of this datetime minus the specified number of weeks. |
1145 | * <p> |
1146 | * This LocalDateTime instance is immutable and unaffected by this method call. |
1147 | * <p> |
1148 | * The following three lines are identical in effect: |
1149 | * <pre> |
1150 | * LocalDateTime subtracted = dt.minusWeeks(6); |
1151 | * LocalDateTime subtracted = dt.minus(Period.weeks(6)); |
1152 | * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.weeks(), -6); |
1153 | * </pre> |
1154 | * |
1155 | * @param weeks the amount of weeks to subtract, may be negative |
1156 | * @return the new LocalDateTime minus the increased weeks |
1157 | */ |
1158 | public LocalDateTime minusWeeks(int weeks) { |
1159 | if (weeks == 0) { |
1160 | return this; |
1161 | } |
1162 | long instant = getChronology().weeks().subtract(getLocalMillis(), weeks); |
1163 | return withLocalMillis(instant); |
1164 | } |
1165 | |
1166 | /** |
1167 | * Returns a copy of this datetime minus the specified number of days. |
1168 | * <p> |
1169 | * This LocalDateTime instance is immutable and unaffected by this method call. |
1170 | * <p> |
1171 | * The following three lines are identical in effect: |
1172 | * <pre> |
1173 | * LocalDateTime subtracted = dt.minusDays(6); |
1174 | * LocalDateTime subtracted = dt.minus(Period.days(6)); |
1175 | * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.days(), -6); |
1176 | * </pre> |
1177 | * |
1178 | * @param days the amount of days to subtract, may be negative |
1179 | * @return the new LocalDateTime minus the increased days |
1180 | */ |
1181 | public LocalDateTime minusDays(int days) { |
1182 | if (days == 0) { |
1183 | return this; |
1184 | } |
1185 | long instant = getChronology().days().subtract(getLocalMillis(), days); |
1186 | return withLocalMillis(instant); |
1187 | } |
1188 | |
1189 | //----------------------------------------------------------------------- |
1190 | /** |
1191 | * Returns a copy of this datetime minus the specified number of hours. |
1192 | * <p> |
1193 | * This LocalDateTime instance is immutable and unaffected by this method call. |
1194 | * <p> |
1195 | * The following three lines are identical in effect: |
1196 | * <pre> |
1197 | * LocalDateTime subtracted = dt.minusHours(6); |
1198 | * LocalDateTime subtracted = dt.minus(Period.hours(6)); |
1199 | * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.hours(), -6); |
1200 | * </pre> |
1201 | * |
1202 | * @param hours the amount of hours to subtract, may be negative |
1203 | * @return the new LocalDateTime minus the increased hours |
1204 | */ |
1205 | public LocalDateTime minusHours(int hours) { |
1206 | if (hours == 0) { |
1207 | return this; |
1208 | } |
1209 | long instant = getChronology().hours().subtract(getLocalMillis(), hours); |
1210 | return withLocalMillis(instant); |
1211 | } |
1212 | |
1213 | /** |
1214 | * Returns a copy of this datetime minus the specified number of minutes. |
1215 | * <p> |
1216 | * This LocalDateTime instance is immutable and unaffected by this method call. |
1217 | * <p> |
1218 | * The following three lines are identical in effect: |
1219 | * <pre> |
1220 | * LocalDateTime subtracted = dt.minusMinutes(6); |
1221 | * LocalDateTime subtracted = dt.minus(Period.minutes(6)); |
1222 | * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.minutes(), -6); |
1223 | * </pre> |
1224 | * |
1225 | * @param minutes the amount of minutes to subtract, may be negative |
1226 | * @return the new LocalDateTime minus the increased minutes |
1227 | */ |
1228 | public LocalDateTime minusMinutes(int minutes) { |
1229 | if (minutes == 0) { |
1230 | return this; |
1231 | } |
1232 | long instant = getChronology().minutes().subtract(getLocalMillis(), minutes); |
1233 | return withLocalMillis(instant); |
1234 | } |
1235 | |
1236 | /** |
1237 | * Returns a copy of this datetime minus the specified number of seconds. |
1238 | * <p> |
1239 | * This LocalDateTime instance is immutable and unaffected by this method call. |
1240 | * <p> |
1241 | * The following three lines are identical in effect: |
1242 | * <pre> |
1243 | * LocalDateTime subtracted = dt.minusSeconds(6); |
1244 | * LocalDateTime subtracted = dt.minus(Period.seconds(6)); |
1245 | * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.seconds(), -6); |
1246 | * </pre> |
1247 | * |
1248 | * @param seconds the amount of seconds to subtract, may be negative |
1249 | * @return the new LocalDateTime minus the increased seconds |
1250 | */ |
1251 | public LocalDateTime minusSeconds(int seconds) { |
1252 | if (seconds == 0) { |
1253 | return this; |
1254 | } |
1255 | long instant = getChronology().seconds().subtract(getLocalMillis(), seconds); |
1256 | return withLocalMillis(instant); |
1257 | } |
1258 | |
1259 | /** |
1260 | * Returns a copy of this datetime minus the specified number of millis. |
1261 | * <p> |
1262 | * This LocalDateTime instance is immutable and unaffected by this method call. |
1263 | * <p> |
1264 | * The following three lines are identical in effect: |
1265 | * <pre> |
1266 | * LocalDateTime subtracted = dt.minusMillis(6); |
1267 | * LocalDateTime subtracted = dt.minus(Period.millis(6)); |
1268 | * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.millis(), -6); |
1269 | * </pre> |
1270 | * |
1271 | * @param millis the amount of millis to subtract, may be negative |
1272 | * @return the new LocalDateTime minus the increased millis |
1273 | */ |
1274 | public LocalDateTime minusMillis(int millis) { |
1275 | if (millis == 0) { |
1276 | return this; |
1277 | } |
1278 | long instant = getChronology().millis().subtract(getLocalMillis(), millis); |
1279 | return withLocalMillis(instant); |
1280 | } |
1281 | |
1282 | //----------------------------------------------------------------------- |
1283 | /** |
1284 | * Gets the property object for the specified type, which contains many |
1285 | * useful methods. |
1286 | * |
1287 | * @param fieldType the field type to get the chronology for |
1288 | * @return the property object |
1289 | * @throws IllegalArgumentException if the field is null or unsupported |
1290 | */ |
1291 | public Property property(DateTimeFieldType fieldType) { |
1292 | if (fieldType == null) { |
1293 | throw new IllegalArgumentException("The DateTimeFieldType must not be null"); |
1294 | } |
1295 | if (isSupported(fieldType) == false) { |
1296 | throw new IllegalArgumentException("Field '" + fieldType + "' is not supported"); |
1297 | } |
1298 | return new Property(this, fieldType.getField(getChronology())); |
1299 | } |
1300 | |
1301 | //----------------------------------------------------------------------- |
1302 | /** |
1303 | * Get the era field value. |
1304 | * |
1305 | * @return the era |
1306 | */ |
1307 | public int getEra() { |
1308 | return getChronology().era().get(getLocalMillis()); |
1309 | } |
1310 | |
1311 | /** |
1312 | * Get the year of era field value. |
1313 | * |
1314 | * @return the year of era |
1315 | */ |
1316 | public int getCenturyOfEra() { |
1317 | return getChronology().centuryOfEra().get(getLocalMillis()); |
1318 | } |
1319 | |
1320 | /** |
1321 | * Get the year of era field value. |
1322 | * |
1323 | * @return the year of era |
1324 | */ |
1325 | public int getYearOfEra() { |
1326 | return getChronology().yearOfEra().get(getLocalMillis()); |
1327 | } |
1328 | |
1329 | /** |
1330 | * Get the year of century field value. |
1331 | * |
1332 | * @return the year of century |
1333 | */ |
1334 | public int getYearOfCentury() { |
1335 | return getChronology().yearOfCentury().get(getLocalMillis()); |
1336 | } |
1337 | |
1338 | /** |
1339 | * Get the year field value. |
1340 | * |
1341 | * @return the year |
1342 | */ |
1343 | public int getYear() { |
1344 | return getChronology().year().get(getLocalMillis()); |
1345 | } |
1346 | |
1347 | /** |
1348 | * Get the weekyear field value. |
1349 | * <p> |
1350 | * The weekyear is the year that matches with the weekOfWeekyear field. |
1351 | * In the standard ISO8601 week algorithm, the first week of the year |
1352 | * is that in which at least 4 days are in the year. As a result of this |
1353 | * definition, day 1 of the first week may be in the previous year. |
1354 | * The weekyear allows you to query the effective year for that day. |
1355 | * |
1356 | * @return the weekyear |
1357 | */ |
1358 | public int getWeekyear() { |
1359 | return getChronology().weekyear().get(getLocalMillis()); |
1360 | } |
1361 | |
1362 | /** |
1363 | * Get the month of year field value. |
1364 | * |
1365 | * @return the month of year |
1366 | */ |
1367 | public int getMonthOfYear() { |
1368 | return getChronology().monthOfYear().get(getLocalMillis()); |
1369 | } |
1370 | |
1371 | /** |
1372 | * Get the week of weekyear field value. |
1373 | * |
1374 | * @return the week of a week based year |
1375 | */ |
1376 | public int getWeekOfWeekyear() { |
1377 | return getChronology().weekOfWeekyear().get(getLocalMillis()); |
1378 | } |
1379 | |
1380 | /** |
1381 | * Get the day of year field value. |
1382 | * |
1383 | * @return the day of year |
1384 | */ |
1385 | public int getDayOfYear() { |
1386 | return getChronology().dayOfYear().get(getLocalMillis()); |
1387 | } |
1388 | |
1389 | /** |
1390 | * Get the day of month field value. |
1391 | * <p> |
1392 | * The values for the day of month are defined in {@link org.joda.time.DateTimeConstants}. |
1393 | * |
1394 | * @return the day of month |
1395 | */ |
1396 | public int getDayOfMonth() { |
1397 | return getChronology().dayOfMonth().get(getLocalMillis()); |
1398 | } |
1399 | |
1400 | /** |
1401 | * Get the day of week field value. |
1402 | * <p> |
1403 | * The values for the day of week are defined in {@link org.joda.time.DateTimeConstants}. |
1404 | * |
1405 | * @return the day of week |
1406 | */ |
1407 | public int getDayOfWeek() { |
1408 | return getChronology().dayOfWeek().get(getLocalMillis()); |
1409 | } |
1410 | |
1411 | //----------------------------------------------------------------------- |
1412 | /** |
1413 | * Get the hour of day field value. |
1414 | * |
1415 | * @return the hour of day |
1416 | */ |
1417 | public int getHourOfDay() { |
1418 | return getChronology().hourOfDay().get(getLocalMillis()); |
1419 | } |
1420 | |
1421 | /** |
1422 | * Get the minute of hour field value. |
1423 | * |
1424 | * @return the minute of hour |
1425 | */ |
1426 | public int getMinuteOfHour() { |
1427 | return getChronology().minuteOfHour().get(getLocalMillis()); |
1428 | } |
1429 | |
1430 | /** |
1431 | * Get the second of minute field value. |
1432 | * |
1433 | * @return the second of minute |
1434 | */ |
1435 | public int getSecondOfMinute() { |
1436 | return getChronology().secondOfMinute().get(getLocalMillis()); |
1437 | } |
1438 | |
1439 | /** |
1440 | * Get the millis of second field value. |
1441 | * |
1442 | * @return the millis of second |
1443 | */ |
1444 | public int getMillisOfSecond() { |
1445 | return getChronology().millisOfSecond().get(getLocalMillis()); |
1446 | } |
1447 | |
1448 | /** |
1449 | * Get the millis of day field value. |
1450 | * |
1451 | * @return the millis of day |
1452 | */ |
1453 | public int getMillisOfDay() { |
1454 | return getChronology().millisOfDay().get(getLocalMillis()); |
1455 | } |
1456 | |
1457 | //----------------------------------------------------------------------- |
1458 | /** |
1459 | * Returns a copy of this datetime with the era field updated. |
1460 | * <p> |
1461 | * LocalDateTime is immutable, so there are no set methods. |
1462 | * Instead, this method returns a new instance with the value of |
1463 | * era changed. |
1464 | * |
1465 | * @param era the era to set |
1466 | * @return a copy of this object with the field set |
1467 | * @throws IllegalArgumentException if the value is invalid |
1468 | */ |
1469 | public LocalDateTime withEra(int era) { |
1470 | return withLocalMillis(getChronology().era().set(getLocalMillis(), era)); |
1471 | } |
1472 | |
1473 | /** |
1474 | * Returns a copy of this datetime with the century of era field updated. |
1475 | * <p> |
1476 | * LocalDateTime is immutable, so there are no set methods. |
1477 | * Instead, this method returns a new instance with the value of |
1478 | * century of era changed. |
1479 | * |
1480 | * @param centuryOfEra the centurey of era to set |
1481 | * @return a copy of this object with the field set |
1482 | * @throws IllegalArgumentException if the value is invalid |
1483 | */ |
1484 | public LocalDateTime withCenturyOfEra(int centuryOfEra) { |
1485 | return withLocalMillis(getChronology().centuryOfEra().set(getLocalMillis(), centuryOfEra)); |
1486 | } |
1487 | |
1488 | /** |
1489 | * Returns a copy of this datetime with the year of era field updated. |
1490 | * <p> |
1491 | * LocalDateTime is immutable, so there are no set methods. |
1492 | * Instead, this method returns a new instance with the value of |
1493 | * year of era changed. |
1494 | * |
1495 | * @param yearOfEra the year of era to set |
1496 | * @return a copy of this object with the field set |
1497 | * @throws IllegalArgumentException if the value is invalid |
1498 | */ |
1499 | public LocalDateTime withYearOfEra(int yearOfEra) { |
1500 | return withLocalMillis(getChronology().yearOfEra().set(getLocalMillis(), yearOfEra)); |
1501 | } |
1502 | |
1503 | /** |
1504 | * Returns a copy of this datetime with the year of century field updated. |
1505 | * <p> |
1506 | * LocalDateTime is immutable, so there are no set methods. |
1507 | * Instead, this method returns a new instance with the value of |
1508 | * year of century changed. |
1509 | * |
1510 | * @param yearOfCentury the year of century to set |
1511 | * @return a copy of this object with the field set |
1512 | * @throws IllegalArgumentException if the value is invalid |
1513 | */ |
1514 | public LocalDateTime withYearOfCentury(int yearOfCentury) { |
1515 | return withLocalMillis(getChronology().yearOfCentury().set(getLocalMillis(), yearOfCentury)); |
1516 | } |
1517 | |
1518 | /** |
1519 | * Returns a copy of this datetime with the year field updated. |
1520 | * <p> |
1521 | * LocalDateTime is immutable, so there are no set methods. |
1522 | * Instead, this method returns a new instance with the value of |
1523 | * year changed. |
1524 | * |
1525 | * @param year the year to set |
1526 | * @return a copy of this object with the field set |
1527 | * @throws IllegalArgumentException if the value is invalid |
1528 | */ |
1529 | public LocalDateTime withYear(int year) { |
1530 | return withLocalMillis(getChronology().year().set(getLocalMillis(), year)); |
1531 | } |
1532 | |
1533 | /** |
1534 | * Returns a copy of this datetime with the weekyear field updated. |
1535 | * <p> |
1536 | * LocalDateTime is immutable, so there are no set methods. |
1537 | * Instead, this method returns a new instance with the value of |
1538 | * weekyear changed. |
1539 | * |
1540 | * @param weekyear the weekyear to set |
1541 | * @return a copy of this object with the field set |
1542 | * @throws IllegalArgumentException if the value is invalid |
1543 | */ |
1544 | public LocalDateTime withWeekyear(int weekyear) { |
1545 | return withLocalMillis(getChronology().weekyear().set(getLocalMillis(), weekyear)); |
1546 | } |
1547 | |
1548 | /** |
1549 | * Returns a copy of this datetime with the month of year field updated. |
1550 | * <p> |
1551 | * LocalDateTime is immutable, so there are no set methods. |
1552 | * Instead, this method returns a new instance with the value of |
1553 | * month of year changed. |
1554 | * |
1555 | * @param monthOfYear the month of year to set |
1556 | * @return a copy of this object with the field set |
1557 | * @throws IllegalArgumentException if the value is invalid |
1558 | */ |
1559 | public LocalDateTime withMonthOfYear(int monthOfYear) { |
1560 | return withLocalMillis(getChronology().monthOfYear().set(getLocalMillis(), monthOfYear)); |
1561 | } |
1562 | |
1563 | /** |
1564 | * Returns a copy of this datetime with the week of weekyear field updated. |
1565 | * <p> |
1566 | * LocalDateTime is immutable, so there are no set methods. |
1567 | * Instead, this method returns a new instance with the value of |
1568 | * week of weekyear changed. |
1569 | * |
1570 | * @param weekOfWeekyear the week of weekyear to set |
1571 | * @return a copy of this object with the field set |
1572 | * @throws IllegalArgumentException if the value is invalid |
1573 | */ |
1574 | public LocalDateTime withWeekOfWeekyear(int weekOfWeekyear) { |
1575 | return withLocalMillis(getChronology().weekOfWeekyear().set(getLocalMillis(), weekOfWeekyear)); |
1576 | } |
1577 | |
1578 | /** |
1579 | * Returns a copy of this datetime with the day of year field updated. |
1580 | * <p> |
1581 | * LocalDateTime is immutable, so there are no set methods. |
1582 | * Instead, this method returns a new instance with the value of |
1583 | * day of year changed. |
1584 | * |
1585 | * @param dayOfYear the day of year to set |
1586 | * @return a copy of this object with the field set |
1587 | * @throws IllegalArgumentException if the value is invalid |
1588 | */ |
1589 | public LocalDateTime withDayOfYear(int dayOfYear) { |
1590 | return withLocalMillis(getChronology().dayOfYear().set(getLocalMillis(), dayOfYear)); |
1591 | } |
1592 | |
1593 | /** |
1594 | * Returns a copy of this datetime with the day of month field updated. |
1595 | * <p> |
1596 | * LocalDateTime is immutable, so there are no set methods. |
1597 | * Instead, this method returns a new instance with the value of |
1598 | * day of month changed. |
1599 | * |
1600 | * @param dayOfMonth the day of month to set |
1601 | * @return a copy of this object with the field set |
1602 | * @throws IllegalArgumentException if the value is invalid |
1603 | */ |
1604 | public LocalDateTime withDayOfMonth(int dayOfMonth) { |
1605 | return withLocalMillis(getChronology().dayOfMonth().set(getLocalMillis(), dayOfMonth)); |
1606 | } |
1607 | |
1608 | /** |
1609 | * Returns a copy of this datetime with the day of week field updated. |
1610 | * <p> |
1611 | * LocalDateTime is immutable, so there are no set methods. |
1612 | * Instead, this method returns a new instance with the value of |
1613 | * day of week changed. |
1614 | * |
1615 | * @param dayOfWeek the day of week to set |
1616 | * @return a copy of this object with the field set |
1617 | * @throws IllegalArgumentException if the value is invalid |
1618 | */ |
1619 | public LocalDateTime withDayOfWeek(int dayOfWeek) { |
1620 | return withLocalMillis(getChronology().dayOfWeek().set(getLocalMillis(), dayOfWeek)); |
1621 | } |
1622 | |
1623 | //----------------------------------------------------------------------- |
1624 | /** |
1625 | * Returns a copy of this datetime with the hour of day field updated. |
1626 | * <p> |
1627 | * LocalDateTime is immutable, so there are no set methods. |
1628 | * Instead, this method returns a new instance with the value of |
1629 | * hour of day changed. |
1630 | * |
1631 | * @param hour the hour of day to set |
1632 | * @return a copy of this object with the field set |
1633 | * @throws IllegalArgumentException if the value is invalid |
1634 | */ |
1635 | public LocalDateTime withHourOfDay(int hour) { |
1636 | return withLocalMillis(getChronology().hourOfDay().set(getLocalMillis(), hour)); |
1637 | } |
1638 | |
1639 | /** |
1640 | * Returns a copy of this datetime with the minute of hour field updated. |
1641 | * <p> |
1642 | * LocalDateTime is immutable, so there are no set methods. |
1643 | * Instead, this method returns a new instance with the value of |
1644 | * minute of hour changed. |
1645 | * |
1646 | * @param minute the minute of hour to set |
1647 | * @return a copy of this object with the field set |
1648 | * @throws IllegalArgumentException if the value is invalid |
1649 | */ |
1650 | public LocalDateTime withMinuteOfHour(int minute) { |
1651 | return withLocalMillis(getChronology().minuteOfHour().set(getLocalMillis(), minute)); |
1652 | } |
1653 | |
1654 | /** |
1655 | * Returns a copy of this datetime with the second of minute field updated. |
1656 | * <p> |
1657 | * LocalDateTime is immutable, so there are no set methods. |
1658 | * Instead, this method returns a new instance with the value of |
1659 | * second of minute changed. |
1660 | * |
1661 | * @param second the second of minute to set |
1662 | * @return a copy of this object with the field set |
1663 | * @throws IllegalArgumentException if the value is invalid |
1664 | */ |
1665 | public LocalDateTime withSecondOfMinute(int second) { |
1666 | return withLocalMillis(getChronology().secondOfMinute().set(getLocalMillis(), second)); |
1667 | } |
1668 | |
1669 | /** |
1670 | * Returns a copy of this datetime with the millis of second field updated. |
1671 | * <p> |
1672 | * LocalDateTime is immutable, so there are no set methods. |
1673 | * Instead, this method returns a new instance with the value of |
1674 | * millis of second changed. |
1675 | * |
1676 | * @param millis the millis of second to set |
1677 | * @return a copy of this object with the field set |
1678 | * @throws IllegalArgumentException if the value is invalid |
1679 | */ |
1680 | public LocalDateTime withMillisOfSecond(int millis) { |
1681 | return withLocalMillis(getChronology().millisOfSecond().set(getLocalMillis(), millis)); |
1682 | } |
1683 | |
1684 | /** |
1685 | * Returns a copy of this datetime with the millis of day field updated. |
1686 | * <p> |
1687 | * LocalDateTime is immutable, so there are no set methods. |
1688 | * Instead, this method returns a new instance with the value of |
1689 | * millis of day changed. |
1690 | * |
1691 | * @param millis the millis of day to set |
1692 | * @return a copy of this object with the field set |
1693 | * @throws IllegalArgumentException if the value is invalid |
1694 | */ |
1695 | public LocalDateTime withMillisOfDay(int millis) { |
1696 | return withLocalMillis(getChronology().millisOfDay().set(getLocalMillis(), millis)); |
1697 | } |
1698 | |
1699 | //----------------------------------------------------------------------- |
1700 | /** |
1701 | * Get the era property which provides access to advanced functionality. |
1702 | * |
1703 | * @return the era property |
1704 | */ |
1705 | public Property era() { |
1706 | return new Property(this, getChronology().era()); |
1707 | } |
1708 | |
1709 | /** |
1710 | * Get the century of era property which provides access to advanced functionality. |
1711 | * |
1712 | * @return the year of era property |
1713 | */ |
1714 | public Property centuryOfEra() { |
1715 | return new Property(this, getChronology().centuryOfEra()); |
1716 | } |
1717 | |
1718 | /** |
1719 | * Get the year of century property which provides access to advanced functionality. |
1720 | * |
1721 | * @return the year of era property |
1722 | */ |
1723 | public Property yearOfCentury() { |
1724 | return new Property(this, getChronology().yearOfCentury()); |
1725 | } |
1726 | |
1727 | /** |
1728 | * Get the year of era property which provides access to advanced functionality. |
1729 | * |
1730 | * @return the year of era property |
1731 | */ |
1732 | public Property yearOfEra() { |
1733 | return new Property(this, getChronology().yearOfEra()); |
1734 | } |
1735 | |
1736 | /** |
1737 | * Get the year property which provides access to advanced functionality. |
1738 | * |
1739 | * @return the year property |
1740 | */ |
1741 | public Property year() { |
1742 | return new Property(this, getChronology().year()); |
1743 | } |
1744 | |
1745 | /** |
1746 | * Get the weekyear property which provides access to advanced functionality. |
1747 | * |
1748 | * @return the weekyear property |
1749 | */ |
1750 | public Property weekyear() { |
1751 | return new Property(this, getChronology().weekyear()); |
1752 | } |
1753 | |
1754 | /** |
1755 | * Get the month of year property which provides access to advanced functionality. |
1756 | * |
1757 | * @return the month of year property |
1758 | */ |
1759 | public Property monthOfYear() { |
1760 | return new Property(this, getChronology().monthOfYear()); |
1761 | } |
1762 | |
1763 | /** |
1764 | * Get the week of a week based year property which provides access to advanced functionality. |
1765 | * |
1766 | * @return the week of a week based year property |
1767 | */ |
1768 | public Property weekOfWeekyear() { |
1769 | return new Property(this, getChronology().weekOfWeekyear()); |
1770 | } |
1771 | |
1772 | /** |
1773 | * Get the day of year property which provides access to advanced functionality. |
1774 | * |
1775 | * @return the day of year property |
1776 | */ |
1777 | public Property dayOfYear() { |
1778 | return new Property(this, getChronology().dayOfYear()); |
1779 | } |
1780 | |
1781 | /** |
1782 | * Get the day of month property which provides access to advanced functionality. |
1783 | * |
1784 | * @return the day of month property |
1785 | */ |
1786 | public Property dayOfMonth() { |
1787 | return new Property(this, getChronology().dayOfMonth()); |
1788 | } |
1789 | |
1790 | /** |
1791 | * Get the day of week property which provides access to advanced functionality. |
1792 | * |
1793 | * @return the day of week property |
1794 | */ |
1795 | public Property dayOfWeek() { |
1796 | return new Property(this, getChronology().dayOfWeek()); |
1797 | } |
1798 | |
1799 | //----------------------------------------------------------------------- |
1800 | /** |
1801 | * Get the hour of day field property which provides access to advanced functionality. |
1802 | * |
1803 | * @return the hour of day property |
1804 | */ |
1805 | public Property hourOfDay() { |
1806 | return new Property(this, getChronology().hourOfDay()); |
1807 | } |
1808 | |
1809 | /** |
1810 | * Get the minute of hour field property which provides access to advanced functionality. |
1811 | * |
1812 | * @return the minute of hour property |
1813 | */ |
1814 | public Property minuteOfHour() { |
1815 | return new Property(this, getChronology().minuteOfHour()); |
1816 | } |
1817 | |
1818 | /** |
1819 | * Get the second of minute field property which provides access to advanced functionality. |
1820 | * |
1821 | * @return the second of minute property |
1822 | */ |
1823 | public Property secondOfMinute() { |
1824 | return new Property(this, getChronology().secondOfMinute()); |
1825 | } |
1826 | |
1827 | /** |
1828 | * Get the millis of second property which provides access to advanced functionality. |
1829 | * |
1830 | * @return the millis of second property |
1831 | */ |
1832 | public Property millisOfSecond() { |
1833 | return new Property(this, getChronology().millisOfSecond()); |
1834 | } |
1835 | |
1836 | /** |
1837 | * Get the millis of day property which provides access to advanced functionality. |
1838 | * |
1839 | * @return the millis of day property |
1840 | */ |
1841 | public Property millisOfDay() { |
1842 | return new Property(this, getChronology().millisOfDay()); |
1843 | } |
1844 | |
1845 | //----------------------------------------------------------------------- |
1846 | /** |
1847 | * Output the date time in ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSS). |
1848 | * |
1849 | * @return ISO8601 time formatted string. |
1850 | */ |
1851 | public String toString() { |
1852 | return ISODateTimeFormat.dateTime().print(this); |
1853 | } |
1854 | |
1855 | /** |
1856 | * Output the date using the specified format pattern. |
1857 | * |
1858 | * @param pattern the pattern specification, null means use <code>toString</code> |
1859 | * @see org.joda.time.format.DateTimeFormat |
1860 | */ |
1861 | public String toString(String pattern) { |
1862 | if (pattern == null) { |
1863 | return toString(); |
1864 | } |
1865 | return DateTimeFormat.forPattern(pattern).print(this); |
1866 | } |
1867 | |
1868 | /** |
1869 | * Output the date using the specified format pattern. |
1870 | * |
1871 | * @param pattern the pattern specification, null means use <code>toString</code> |
1872 | * @param locale Locale to use, null means default |
1873 | * @see org.joda.time.format.DateTimeFormat |
1874 | */ |
1875 | public String toString(String pattern, Locale locale) throws IllegalArgumentException { |
1876 | if (pattern == null) { |
1877 | return toString(); |
1878 | } |
1879 | return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this); |
1880 | } |
1881 | |
1882 | //----------------------------------------------------------------------- |
1883 | /** |
1884 | * LocalDateTime.Property binds a LocalDateTime to a DateTimeField allowing |
1885 | * powerful datetime functionality to be easily accessed. |
1886 | * <p> |
1887 | * The simplest use of this class is as an alternative get method, here used to |
1888 | * get the year '1972' (as an int) and the month 'December' (as a String). |
1889 | * <pre> |
1890 | * LocalDateTime dt = new LocalDateTime(1972, 12, 3, 0, 0); |
1891 | * int year = dt.year().get(); |
1892 | * String monthStr = dt.month().getAsText(); |
1893 | * </pre> |
1894 | * <p> |
1895 | * Methods are also provided that allow date modification. These return |
1896 | * new instances of LocalDateTime - they do not modify the original. |
1897 | * The example below yields two independent immutable date objects |
1898 | * 20 years apart. |
1899 | * <pre> |
1900 | * LocalDateTime dt = new LocalDateTime(1972, 12, 3, 0, 0); |
1901 | * LocalDateTime dt1920 = dt.year().setCopy(1920); |
1902 | * </pre> |
1903 | * <p> |
1904 | * LocalDateTime.Property itself is thread-safe and immutable, as well as the |
1905 | * LocalDateTime being operated on. |
1906 | * |
1907 | * @author Stephen Colebourne |
1908 | * @author Brian S O'Neill |
1909 | * @since 1.3 |
1910 | */ |
1911 | public static final class Property extends AbstractReadableInstantFieldProperty { |
1912 | |
1913 | /** Serialization version */ |
1914 | private static final long serialVersionUID = -358138762846288L; |
1915 | |
1916 | /** The instant this property is working against */ |
1917 | private transient LocalDateTime iInstant; |
1918 | /** The field this property is working against */ |
1919 | private transient DateTimeField iField; |
1920 | |
1921 | /** |
1922 | * Constructor. |
1923 | * |
1924 | * @param instant the instant to set |
1925 | * @param field the field to use |
1926 | */ |
1927 | Property(LocalDateTime instant, DateTimeField field) { |
1928 | super(); |
1929 | iInstant = instant; |
1930 | iField = field; |
1931 | } |
1932 | |
1933 | /** |
1934 | * Writes the property in a safe serialization format. |
1935 | */ |
1936 | private void writeObject(ObjectOutputStream oos) throws IOException { |
1937 | oos.writeObject(iInstant); |
1938 | oos.writeObject(iField.getType()); |
1939 | } |
1940 | |
1941 | /** |
1942 | * Reads the property from a safe serialization format. |
1943 | */ |
1944 | private void readObject(ObjectInputStream oos) throws IOException, ClassNotFoundException { |
1945 | iInstant = (LocalDateTime) oos.readObject(); |
1946 | DateTimeFieldType type = (DateTimeFieldType) oos.readObject(); |
1947 | iField = type.getField(iInstant.getChronology()); |
1948 | } |
1949 | |
1950 | //----------------------------------------------------------------------- |
1951 | /** |
1952 | * Gets the field being used. |
1953 | * |
1954 | * @return the field |
1955 | */ |
1956 | public DateTimeField getField() { |
1957 | return iField; |
1958 | } |
1959 | |
1960 | /** |
1961 | * Gets the milliseconds of the datetime that this property is linked to. |
1962 | * |
1963 | * @return the milliseconds |
1964 | */ |
1965 | protected long getMillis() { |
1966 | return iInstant.getLocalMillis(); |
1967 | } |
1968 | |
1969 | /** |
1970 | * Gets the chronology of the datetime that this property is linked to. |
1971 | * |
1972 | * @return the chronology |
1973 | * @since 1.4 |
1974 | */ |
1975 | protected Chronology getChronology() { |
1976 | return iInstant.getChronology(); |
1977 | } |
1978 | |
1979 | /** |
1980 | * Gets the LocalDateTime object linked to this property. |
1981 | * |
1982 | * @return the linked LocalDateTime |
1983 | */ |
1984 | public LocalDateTime getLocalDateTime() { |
1985 | return iInstant; |
1986 | } |
1987 | |
1988 | //----------------------------------------------------------------------- |
1989 | /** |
1990 | * Adds to this field in a copy of this LocalDateTime. |
1991 | * <p> |
1992 | * The LocalDateTime attached to this property is unchanged by this call. |
1993 | * |
1994 | * @param value the value to add to the field in the copy |
1995 | * @return a copy of the LocalDateTime with the field value changed |
1996 | * @throws IllegalArgumentException if the value isn't valid |
1997 | */ |
1998 | public LocalDateTime addToCopy(int value) { |
1999 | return iInstant.withLocalMillis(iField.add(iInstant.getLocalMillis(), value)); |
2000 | } |
2001 | |
2002 | /** |
2003 | * Adds to this field in a copy of this LocalDateTime. |
2004 | * <p> |
2005 | * The LocalDateTime attached to this property is unchanged by this call. |
2006 | * |
2007 | * @param value the value to add to the field in the copy |
2008 | * @return a copy of the LocalDateTime with the field value changed |
2009 | * @throws IllegalArgumentException if the value isn't valid |
2010 | */ |
2011 | public LocalDateTime addToCopy(long value) { |
2012 | return iInstant.withLocalMillis(iField.add(iInstant.getLocalMillis(), value)); |
2013 | } |
2014 | |
2015 | /** |
2016 | * Adds to this field, possibly wrapped, in a copy of this LocalDateTime. |
2017 | * A field wrapped operation only changes this field. |
2018 | * Thus 31st January addWrapField one day goes to the 1st January. |
2019 | * <p> |
2020 | * The LocalDateTime attached to this property is unchanged by this call. |
2021 | * |
2022 | * @param value the value to add to the field in the copy |
2023 | * @return a copy of the LocalDateTime with the field value changed |
2024 | * @throws IllegalArgumentException if the value isn't valid |
2025 | */ |
2026 | public LocalDateTime addWrapFieldToCopy(int value) { |
2027 | return iInstant.withLocalMillis(iField.addWrapField(iInstant.getLocalMillis(), value)); |
2028 | } |
2029 | |
2030 | //----------------------------------------------------------------------- |
2031 | /** |
2032 | * Sets this field in a copy of the LocalDateTime. |
2033 | * <p> |
2034 | * The LocalDateTime attached to this property is unchanged by this call. |
2035 | * |
2036 | * @param value the value to set the field in the copy to |
2037 | * @return a copy of the LocalDateTime with the field value changed |
2038 | * @throws IllegalArgumentException if the value isn't valid |
2039 | */ |
2040 | public LocalDateTime setCopy(int value) { |
2041 | return iInstant.withLocalMillis(iField.set(iInstant.getLocalMillis(), value)); |
2042 | } |
2043 | |
2044 | /** |
2045 | * Sets this field in a copy of the LocalDateTime to a parsed text value. |
2046 | * <p> |
2047 | * The LocalDateTime attached to this property is unchanged by this call. |
2048 | * |
2049 | * @param text the text value to set |
2050 | * @param locale optional locale to use for selecting a text symbol |
2051 | * @return a copy of the LocalDateTime with the field value changed |
2052 | * @throws IllegalArgumentException if the text value isn't valid |
2053 | */ |
2054 | public LocalDateTime setCopy(String text, Locale locale) { |
2055 | return iInstant.withLocalMillis(iField.set(iInstant.getLocalMillis(), text, locale)); |
2056 | } |
2057 | |
2058 | /** |
2059 | * Sets this field in a copy of the LocalDateTime to a parsed text value. |
2060 | * <p> |
2061 | * The LocalDateTime attached to this property is unchanged by this call. |
2062 | * |
2063 | * @param text the text value to set |
2064 | * @return a copy of the LocalDateTime with the field value changed |
2065 | * @throws IllegalArgumentException if the text value isn't valid |
2066 | */ |
2067 | public LocalDateTime setCopy(String text) { |
2068 | return setCopy(text, null); |
2069 | } |
2070 | |
2071 | //----------------------------------------------------------------------- |
2072 | /** |
2073 | * Returns a new LocalDateTime with this field set to the maximum value |
2074 | * for this field. |
2075 | * <p> |
2076 | * This operation is useful for obtaining a LocalDateTime on the last day |
2077 | * of the month, as month lengths vary. |
2078 | * <pre> |
2079 | * LocalDateTime lastDayOfMonth = dt.dayOfMonth().withMaximumValue(); |
2080 | * </pre> |
2081 | * <p> |
2082 | * The LocalDateTime attached to this property is unchanged by this call. |
2083 | * |
2084 | * @return a copy of the LocalDateTime with this field set to its maximum |
2085 | */ |
2086 | public LocalDateTime withMaximumValue() { |
2087 | return setCopy(getMaximumValue()); |
2088 | } |
2089 | |
2090 | /** |
2091 | * Returns a new LocalDateTime with this field set to the minimum value |
2092 | * for this field. |
2093 | * <p> |
2094 | * The LocalDateTime attached to this property is unchanged by this call. |
2095 | * |
2096 | * @return a copy of the LocalDateTime with this field set to its minimum |
2097 | */ |
2098 | public LocalDateTime withMinimumValue() { |
2099 | return setCopy(getMinimumValue()); |
2100 | } |
2101 | |
2102 | //----------------------------------------------------------------------- |
2103 | /** |
2104 | * Rounds to the lowest whole unit of this field on a copy of this |
2105 | * LocalDateTime. |
2106 | * <p> |
2107 | * For example, rounding floor on the hourOfDay field of a LocalDateTime |
2108 | * where the time is 10:30 would result in new LocalDateTime with the |
2109 | * time of 10:00. |
2110 | * |
2111 | * @return a copy of the LocalDateTime with the field value changed |
2112 | */ |
2113 | public LocalDateTime roundFloorCopy() { |
2114 | return iInstant.withLocalMillis(iField.roundFloor(iInstant.getLocalMillis())); |
2115 | } |
2116 | |
2117 | /** |
2118 | * Rounds to the highest whole unit of this field on a copy of this |
2119 | * LocalDateTime. |
2120 | * <p> |
2121 | * For example, rounding floor on the hourOfDay field of a LocalDateTime |
2122 | * where the time is 10:30 would result in new LocalDateTime with the |
2123 | * time of 11:00. |
2124 | * |
2125 | * @return a copy of the LocalDateTime with the field value changed |
2126 | */ |
2127 | public LocalDateTime roundCeilingCopy() { |
2128 | return iInstant.withLocalMillis(iField.roundCeiling(iInstant.getLocalMillis())); |
2129 | } |
2130 | |
2131 | /** |
2132 | * Rounds to the nearest whole unit of this field on a copy of this |
2133 | * LocalDateTime, favoring the floor if halfway. |
2134 | * |
2135 | * @return a copy of the LocalDateTime with the field value changed |
2136 | */ |
2137 | public LocalDateTime roundHalfFloorCopy() { |
2138 | return iInstant.withLocalMillis(iField.roundHalfFloor(iInstant.getLocalMillis())); |
2139 | } |
2140 | |
2141 | /** |
2142 | * Rounds to the nearest whole unit of this field on a copy of this |
2143 | * LocalDateTime, favoring the ceiling if halfway. |
2144 | * |
2145 | * @return a copy of the LocalDateTime with the field value changed |
2146 | */ |
2147 | public LocalDateTime roundHalfCeilingCopy() { |
2148 | return iInstant.withLocalMillis(iField.roundHalfCeiling(iInstant.getLocalMillis())); |
2149 | } |
2150 | |
2151 | /** |
2152 | * Rounds to the nearest whole unit of this field on a copy of this |
2153 | * LocalDateTime. If halfway, the ceiling is favored over the floor |
2154 | * only if it makes this field's value even. |
2155 | * |
2156 | * @return a copy of the LocalDateTime with the field value changed |
2157 | */ |
2158 | public LocalDateTime roundHalfEvenCopy() { |
2159 | return iInstant.withLocalMillis(iField.roundHalfEven(iInstant.getLocalMillis())); |
2160 | } |
2161 | } |
2162 | |
2163 | } |