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