1 /* 2 * Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * This file is available under and governed by the GNU General Public 28 * License version 2 only, as published by the Free Software Foundation. 29 * However, the following notice accompanied the original version of this 30 * file: 31 * 32 * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos 33 * 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions are met: 38 * 39 * * Redistributions of source code must retain the above copyright notice, 40 * this list of conditions and the following disclaimer. 41 * 42 * * Redistributions in binary form must reproduce the above copyright notice, 43 * this list of conditions and the following disclaimer in the documentation 44 * and/or other materials provided with the distribution. 45 * 46 * * Neither the name of JSR-310 nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 54 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 55 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 57 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 58 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 59 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 package java.time; 63 64 import static java.time.temporal.ChronoField.EPOCH_DAY; 65 import static java.time.temporal.ChronoField.INSTANT_SECONDS; 66 import static java.time.temporal.ChronoField.NANO_OF_DAY; 67 import static java.time.temporal.ChronoField.OFFSET_SECONDS; 68 import static java.time.temporal.ChronoUnit.FOREVER; 69 import static java.time.temporal.ChronoUnit.NANOS; 70 71 import java.io.IOException; 72 import java.io.ObjectInput; 73 import java.io.ObjectOutput; 74 import java.io.InvalidObjectException; 75 import java.io.ObjectInputStream; 76 import java.io.Serializable; 77 import java.time.chrono.IsoChronology; 78 import java.time.format.DateTimeFormatter; 79 import java.time.format.DateTimeParseException; 80 import java.time.temporal.ChronoField; 81 import java.time.temporal.ChronoUnit; 82 import java.time.temporal.Temporal; 83 import java.time.temporal.TemporalAccessor; 84 import java.time.temporal.TemporalAdjuster; 85 import java.time.temporal.TemporalAmount; 86 import java.time.temporal.TemporalField; 87 import java.time.temporal.TemporalQueries; 88 import java.time.temporal.TemporalQuery; 89 import java.time.temporal.TemporalUnit; 90 import java.time.temporal.UnsupportedTemporalTypeException; 91 import java.time.temporal.ValueRange; 92 import java.time.zone.ZoneRules; 93 import java.util.Comparator; 94 import java.util.Objects; 95 96 import jdk.internal.util.DateTimeHelper; 97 98 /** 99 * A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, 100 * such as {@code 2007-12-03T10:15:30+01:00}. 101 * <p> 102 * {@code OffsetDateTime} is an immutable representation of a date-time with an offset. 103 * This class stores all date and time fields, to a precision of nanoseconds, 104 * as well as the offset from UTC/Greenwich. For example, the value 105 * "2nd October 2007 at 13:45:30.123456789 +02:00" can be stored in an {@code OffsetDateTime}. 106 * <p> 107 * {@code OffsetDateTime}, {@link java.time.ZonedDateTime} and {@link java.time.Instant} all store an instant 108 * on the time-line to nanosecond precision. 109 * {@code Instant} is the simplest, simply representing the instant. 110 * {@code OffsetDateTime} adds to the instant the offset from UTC/Greenwich, which allows 111 * the local date-time to be obtained. 112 * {@code ZonedDateTime} adds full time-zone rules. 113 * <p> 114 * It is intended that {@code ZonedDateTime} or {@code Instant} is used to model data 115 * in simpler applications. This class may be used when modeling date-time concepts in 116 * more detail, or when communicating to a database or in a network protocol. 117 * <p> 118 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a> 119 * class; programmers should treat instances that are 120 * {@linkplain #equals(Object) equal} as interchangeable and should not 121 * use instances for synchronization, or unpredictable behavior may 122 * occur. For example, in a future release, synchronization may fail. 123 * The {@code equals} method should be used for comparisons. 124 * 125 * @implSpec 126 * This class is immutable and thread-safe. 127 * 128 * @since 1.8 129 */ 130 @jdk.internal.ValueBased 131 public final class OffsetDateTime 132 implements Temporal, TemporalAdjuster, Comparable<OffsetDateTime>, Serializable { 133 134 /** 135 * The minimum supported {@code OffsetDateTime}, '-999999999-01-01T00:00:00+18:00'. 136 * This is the local date-time of midnight at the start of the minimum date 137 * in the maximum offset (larger offsets are earlier on the time-line). 138 * This combines {@link LocalDateTime#MIN} and {@link ZoneOffset#MAX}. 139 * This could be used by an application as a "far past" date-time. 140 */ 141 public static final OffsetDateTime MIN = LocalDateTime.MIN.atOffset(ZoneOffset.MAX); 142 /** 143 * The maximum supported {@code OffsetDateTime}, '+999999999-12-31T23:59:59.999999999-18:00'. 144 * This is the local date-time just before midnight at the end of the maximum date 145 * in the minimum offset (larger negative offsets are later on the time-line). 146 * This combines {@link LocalDateTime#MAX} and {@link ZoneOffset#MIN}. 147 * This could be used by an application as a "far future" date-time. 148 */ 149 public static final OffsetDateTime MAX = LocalDateTime.MAX.atOffset(ZoneOffset.MIN); 150 151 /** 152 * Gets a comparator that compares two {@code OffsetDateTime} instances 153 * based solely on the instant. 154 * <p> 155 * This method differs from the comparison in {@link #compareTo} in that it 156 * only compares the underlying instant. 157 * 158 * @return a comparator that compares in time-line order 159 * 160 * @see #isAfter 161 * @see #isBefore 162 * @see #isEqual 163 */ 164 public static Comparator<OffsetDateTime> timeLineOrder() { 165 return OffsetDateTime::compareInstant; 166 } 167 168 /** 169 * Compares this {@code OffsetDateTime} to another date-time. 170 * The comparison is based on the instant. 171 * 172 * @param datetime1 the first date-time to compare, not null 173 * @param datetime2 the other date-time to compare to, not null 174 * @return the comparator value, that is less than zero if {@code datetime1} is before {@code datetime2}, 175 * zero if they are equal, greater than zero if {@code datetime1} is after {@code datetime2} 176 */ 177 private static int compareInstant(OffsetDateTime datetime1, OffsetDateTime datetime2) { 178 if (datetime1.getOffset().equals(datetime2.getOffset())) { 179 return datetime1.toLocalDateTime().compareTo(datetime2.toLocalDateTime()); 180 } 181 int cmp = Long.compare(datetime1.toEpochSecond(), datetime2.toEpochSecond()); 182 if (cmp == 0) { 183 cmp = datetime1.toLocalTime().getNano() - datetime2.toLocalTime().getNano(); 184 } 185 return cmp; 186 } 187 188 /** 189 * Serialization version. 190 */ 191 @java.io.Serial 192 private static final long serialVersionUID = 2287754244819255394L; 193 194 /** 195 * @serial The local date-time. 196 */ 197 private final LocalDateTime dateTime; 198 /** 199 * @serial The offset from UTC/Greenwich. 200 */ 201 private final ZoneOffset offset; 202 203 //----------------------------------------------------------------------- 204 /** 205 * Obtains the current date-time from the system clock in the default time-zone. 206 * <p> 207 * This will query the {@link Clock#systemDefaultZone() system clock} in the default 208 * time-zone to obtain the current date-time. 209 * The offset will be calculated from the time-zone in the clock. 210 * <p> 211 * Using this method will prevent the ability to use an alternate clock for testing 212 * because the clock is hard-coded. 213 * 214 * @return the current date-time using the system clock, not null 215 */ 216 public static OffsetDateTime now() { 217 return now(Clock.systemDefaultZone()); 218 } 219 220 /** 221 * Obtains the current date-time from the system clock in the specified time-zone. 222 * <p> 223 * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date-time. 224 * Specifying the time-zone avoids dependence on the default time-zone. 225 * The offset will be calculated from the specified time-zone. 226 * <p> 227 * Using this method will prevent the ability to use an alternate clock for testing 228 * because the clock is hard-coded. 229 * 230 * @param zone the zone ID to use, not null 231 * @return the current date-time using the system clock, not null 232 */ 233 public static OffsetDateTime now(ZoneId zone) { 234 return now(Clock.system(zone)); 235 } 236 237 /** 238 * Obtains the current date-time from the specified clock. 239 * <p> 240 * This will query the specified clock to obtain the current date-time. 241 * The offset will be calculated from the time-zone in the clock. 242 * <p> 243 * Using this method allows the use of an alternate clock for testing. 244 * The alternate clock may be introduced using {@link Clock dependency injection}. 245 * 246 * @param clock the clock to use, not null 247 * @return the current date-time, not null 248 */ 249 public static OffsetDateTime now(Clock clock) { 250 Objects.requireNonNull(clock, "clock"); 251 final Instant now = clock.instant(); // called once 252 return ofInstant(now, clock.getZone().getRules().getOffset(now)); 253 } 254 255 //----------------------------------------------------------------------- 256 /** 257 * Obtains an instance of {@code OffsetDateTime} from a date, time and offset. 258 * <p> 259 * This creates an offset date-time with the specified local date, time and offset. 260 * 261 * @param date the local date, not null 262 * @param time the local time, not null 263 * @param offset the zone offset, not null 264 * @return the offset date-time, not null 265 */ 266 public static OffsetDateTime of(LocalDate date, LocalTime time, ZoneOffset offset) { 267 LocalDateTime dt = LocalDateTime.of(date, time); 268 return new OffsetDateTime(dt, offset); 269 } 270 271 /** 272 * Obtains an instance of {@code OffsetDateTime} from a date-time and offset. 273 * <p> 274 * This creates an offset date-time with the specified local date-time and offset. 275 * 276 * @param dateTime the local date-time, not null 277 * @param offset the zone offset, not null 278 * @return the offset date-time, not null 279 */ 280 public static OffsetDateTime of(LocalDateTime dateTime, ZoneOffset offset) { 281 return new OffsetDateTime(dateTime, offset); 282 } 283 284 /** 285 * Obtains an instance of {@code OffsetDateTime} from a year, month, day, 286 * hour, minute, second, nanosecond and offset. 287 * <p> 288 * This creates an offset date-time with the seven specified fields. 289 * <p> 290 * This method exists primarily for writing test cases. 291 * Non test-code will typically use other methods to create an offset time. 292 * {@code LocalDateTime} has five additional convenience variants of the 293 * equivalent factory method taking fewer arguments. 294 * They are not provided here to reduce the footprint of the API. 295 * 296 * @param year the year to represent, from MIN_YEAR to MAX_YEAR 297 * @param month the month-of-year to represent, from 1 (January) to 12 (December) 298 * @param dayOfMonth the day-of-month to represent, from 1 to 31 299 * @param hour the hour-of-day to represent, from 0 to 23 300 * @param minute the minute-of-hour to represent, from 0 to 59 301 * @param second the second-of-minute to represent, from 0 to 59 302 * @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 303 * @param offset the zone offset, not null 304 * @return the offset date-time, not null 305 * @throws DateTimeException if the value of any field is out of range, or 306 * if the day-of-month is invalid for the month-year 307 */ 308 public static OffsetDateTime of( 309 int year, int month, int dayOfMonth, 310 int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset) { 311 LocalDateTime dt = LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond); 312 return new OffsetDateTime(dt, offset); 313 } 314 315 //----------------------------------------------------------------------- 316 /** 317 * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID. 318 * <p> 319 * This creates an offset date-time with the same instant as that specified. 320 * Finding the offset from UTC/Greenwich is simple as there is only one valid 321 * offset for each instant. 322 * 323 * @param instant the instant to create the date-time from, not null 324 * @param zone the time-zone, which may be an offset, not null 325 * @return the offset date-time, not null 326 * @throws DateTimeException if the result exceeds the supported range 327 */ 328 public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) { 329 Objects.requireNonNull(instant, "instant"); 330 Objects.requireNonNull(zone, "zone"); 331 ZoneRules rules = zone.getRules(); 332 ZoneOffset offset = rules.getOffset(instant); 333 LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset); 334 return new OffsetDateTime(ldt, offset); 335 } 336 337 //----------------------------------------------------------------------- 338 /** 339 * Obtains an instance of {@code OffsetDateTime} from a temporal object. 340 * <p> 341 * This obtains an offset date-time based on the specified temporal. 342 * A {@code TemporalAccessor} represents an arbitrary set of date and time information, 343 * which this factory converts to an instance of {@code OffsetDateTime}. 344 * <p> 345 * The conversion will first obtain a {@code ZoneOffset} from the temporal object. 346 * It will then try to obtain a {@code LocalDateTime}, falling back to an {@code Instant} if necessary. 347 * The result will be the combination of {@code ZoneOffset} with either 348 * with {@code LocalDateTime} or {@code Instant}. 349 * Implementations are permitted to perform optimizations such as accessing 350 * those fields that are equivalent to the relevant objects. 351 * <p> 352 * This method matches the signature of the functional interface {@link TemporalQuery} 353 * allowing it to be used as a query via method reference, {@code OffsetDateTime::from}. 354 * 355 * @param temporal the temporal object to convert, not null 356 * @return the offset date-time, not null 357 * @throws DateTimeException if unable to convert to an {@code OffsetDateTime} 358 */ 359 public static OffsetDateTime from(TemporalAccessor temporal) { 360 if (temporal instanceof OffsetDateTime) { 361 return (OffsetDateTime) temporal; 362 } 363 try { 364 ZoneOffset offset = ZoneOffset.from(temporal); 365 LocalDate date = temporal.query(TemporalQueries.localDate()); 366 LocalTime time = temporal.query(TemporalQueries.localTime()); 367 if (date != null && time != null) { 368 return OffsetDateTime.of(date, time, offset); 369 } else { 370 Instant instant = Instant.from(temporal); 371 return OffsetDateTime.ofInstant(instant, offset); 372 } 373 } catch (DateTimeException ex) { 374 throw new DateTimeException("Unable to obtain OffsetDateTime from TemporalAccessor: " + 375 temporal + " of type " + temporal.getClass().getName(), ex); 376 } 377 } 378 379 //----------------------------------------------------------------------- 380 /** 381 * Obtains an instance of {@code OffsetDateTime} from a text string 382 * such as {@code 2007-12-03T10:15:30+01:00}. 383 * <p> 384 * The string must represent a valid date-time and is parsed using 385 * {@link java.time.format.DateTimeFormatter#ISO_OFFSET_DATE_TIME}. 386 * 387 * @param text the text to parse such as "2007-12-03T10:15:30+01:00", not null 388 * @return the parsed offset date-time, not null 389 * @throws DateTimeParseException if the text cannot be parsed 390 */ 391 public static OffsetDateTime parse(CharSequence text) { 392 return parse(text, DateTimeFormatter.ISO_OFFSET_DATE_TIME); 393 } 394 395 /** 396 * Obtains an instance of {@code OffsetDateTime} from a text string using a specific formatter. 397 * <p> 398 * The text is parsed using the formatter, returning a date-time. 399 * 400 * @param text the text to parse, not null 401 * @param formatter the formatter to use, not null 402 * @return the parsed offset date-time, not null 403 * @throws DateTimeParseException if the text cannot be parsed 404 */ 405 public static OffsetDateTime parse(CharSequence text, DateTimeFormatter formatter) { 406 Objects.requireNonNull(formatter, "formatter"); 407 return formatter.parse(text, OffsetDateTime::from); 408 } 409 410 //----------------------------------------------------------------------- 411 /** 412 * Constructor. 413 * 414 * @param dateTime the local date-time, not null 415 * @param offset the zone offset, not null 416 */ 417 private OffsetDateTime(LocalDateTime dateTime, ZoneOffset offset) { 418 this.dateTime = Objects.requireNonNull(dateTime, "dateTime"); 419 this.offset = Objects.requireNonNull(offset, "offset"); 420 } 421 422 /** 423 * Returns a new date-time based on this one, returning {@code this} where possible. 424 * 425 * @param dateTime the date-time to create with, not null 426 * @param offset the zone offset to create with, not null 427 */ 428 private OffsetDateTime with(LocalDateTime dateTime, ZoneOffset offset) { 429 if (this.dateTime == dateTime && this.offset.equals(offset)) { 430 return this; 431 } 432 return new OffsetDateTime(dateTime, offset); 433 } 434 435 //----------------------------------------------------------------------- 436 /** 437 * Checks if the specified field is supported. 438 * <p> 439 * This checks if this date-time can be queried for the specified field. 440 * If false, then calling the {@link #range(TemporalField) range}, 441 * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)} 442 * methods will throw an exception. 443 * <p> 444 * If the field is a {@link ChronoField} then the query is implemented here. 445 * The supported fields are: 446 * <ul> 447 * <li>{@code NANO_OF_SECOND} 448 * <li>{@code NANO_OF_DAY} 449 * <li>{@code MICRO_OF_SECOND} 450 * <li>{@code MICRO_OF_DAY} 451 * <li>{@code MILLI_OF_SECOND} 452 * <li>{@code MILLI_OF_DAY} 453 * <li>{@code SECOND_OF_MINUTE} 454 * <li>{@code SECOND_OF_DAY} 455 * <li>{@code MINUTE_OF_HOUR} 456 * <li>{@code MINUTE_OF_DAY} 457 * <li>{@code HOUR_OF_AMPM} 458 * <li>{@code CLOCK_HOUR_OF_AMPM} 459 * <li>{@code HOUR_OF_DAY} 460 * <li>{@code CLOCK_HOUR_OF_DAY} 461 * <li>{@code AMPM_OF_DAY} 462 * <li>{@code DAY_OF_WEEK} 463 * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} 464 * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} 465 * <li>{@code DAY_OF_MONTH} 466 * <li>{@code DAY_OF_YEAR} 467 * <li>{@code EPOCH_DAY} 468 * <li>{@code ALIGNED_WEEK_OF_MONTH} 469 * <li>{@code ALIGNED_WEEK_OF_YEAR} 470 * <li>{@code MONTH_OF_YEAR} 471 * <li>{@code PROLEPTIC_MONTH} 472 * <li>{@code YEAR_OF_ERA} 473 * <li>{@code YEAR} 474 * <li>{@code ERA} 475 * <li>{@code INSTANT_SECONDS} 476 * <li>{@code OFFSET_SECONDS} 477 * </ul> 478 * All other {@code ChronoField} instances will return false. 479 * <p> 480 * If the field is not a {@code ChronoField}, then the result of this method 481 * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} 482 * passing {@code this} as the argument. 483 * Whether the field is supported is determined by the field. 484 * 485 * @param field the field to check, null returns false 486 * @return true if the field is supported on this date-time, false if not 487 */ 488 @Override 489 public boolean isSupported(TemporalField field) { 490 return field instanceof ChronoField || (field != null && field.isSupportedBy(this)); 491 } 492 493 /** 494 * Checks if the specified unit is supported. 495 * <p> 496 * This checks if the specified unit can be added to, or subtracted from, this date-time. 497 * If false, then calling the {@link #plus(long, TemporalUnit)} and 498 * {@link #minus(long, TemporalUnit) minus} methods will throw an exception. 499 * <p> 500 * If the unit is a {@link ChronoUnit} then the query is implemented here. 501 * The supported units are: 502 * <ul> 503 * <li>{@code NANOS} 504 * <li>{@code MICROS} 505 * <li>{@code MILLIS} 506 * <li>{@code SECONDS} 507 * <li>{@code MINUTES} 508 * <li>{@code HOURS} 509 * <li>{@code HALF_DAYS} 510 * <li>{@code DAYS} 511 * <li>{@code WEEKS} 512 * <li>{@code MONTHS} 513 * <li>{@code YEARS} 514 * <li>{@code DECADES} 515 * <li>{@code CENTURIES} 516 * <li>{@code MILLENNIA} 517 * <li>{@code ERAS} 518 * </ul> 519 * All other {@code ChronoUnit} instances will return false. 520 * <p> 521 * If the unit is not a {@code ChronoUnit}, then the result of this method 522 * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)} 523 * passing {@code this} as the argument. 524 * Whether the unit is supported is determined by the unit. 525 * 526 * @param unit the unit to check, null returns false 527 * @return true if the unit can be added/subtracted, false if not 528 */ 529 @Override // override for Javadoc 530 public boolean isSupported(TemporalUnit unit) { 531 if (unit instanceof ChronoUnit) { 532 return unit != FOREVER; 533 } 534 return unit != null && unit.isSupportedBy(this); 535 } 536 537 //----------------------------------------------------------------------- 538 /** 539 * Gets the range of valid values for the specified field. 540 * <p> 541 * The range object expresses the minimum and maximum valid values for a field. 542 * This date-time is used to enhance the accuracy of the returned range. 543 * If it is not possible to return the range, because the field is not supported 544 * or for some other reason, an exception is thrown. 545 * <p> 546 * If the field is a {@link ChronoField} then the query is implemented here. 547 * The {@link #isSupported(TemporalField) supported fields} will return 548 * appropriate range instances. 549 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. 550 * <p> 551 * If the field is not a {@code ChronoField}, then the result of this method 552 * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} 553 * passing {@code this} as the argument. 554 * Whether the range can be obtained is determined by the field. 555 * 556 * @param field the field to query the range for, not null 557 * @return the range of valid values for the field, not null 558 * @throws DateTimeException if the range for the field cannot be obtained 559 * @throws UnsupportedTemporalTypeException if the field is not supported 560 */ 561 @Override 562 public ValueRange range(TemporalField field) { 563 if (field instanceof ChronoField) { 564 if (field == INSTANT_SECONDS || field == OFFSET_SECONDS) { 565 return field.range(); 566 } 567 return dateTime.range(field); 568 } 569 return field.rangeRefinedBy(this); 570 } 571 572 /** 573 * Gets the value of the specified field from this date-time as an {@code int}. 574 * <p> 575 * This queries this date-time for the value of the specified field. 576 * The returned value will always be within the valid range of values for the field. 577 * If it is not possible to return the value, because the field is not supported 578 * or for some other reason, an exception is thrown. 579 * <p> 580 * If the field is a {@link ChronoField} then the query is implemented here. 581 * The {@link #isSupported(TemporalField) supported fields} will return valid 582 * values based on this date-time, except {@code NANO_OF_DAY}, {@code MICRO_OF_DAY}, 583 * {@code EPOCH_DAY}, {@code PROLEPTIC_MONTH} and {@code INSTANT_SECONDS} which are too 584 * large to fit in an {@code int} and throw an {@code UnsupportedTemporalTypeException}. 585 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. 586 * <p> 587 * If the field is not a {@code ChronoField}, then the result of this method 588 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} 589 * passing {@code this} as the argument. Whether the value can be obtained, 590 * and what the value represents, is determined by the field. 591 * 592 * @param field the field to get, not null 593 * @return the value for the field 594 * @throws DateTimeException if a value for the field cannot be obtained or 595 * the value is outside the range of valid values for the field 596 * @throws UnsupportedTemporalTypeException if the field is not supported or 597 * the range of values exceeds an {@code int} 598 * @throws ArithmeticException if numeric overflow occurs 599 */ 600 @Override 601 public int get(TemporalField field) { 602 if (field instanceof ChronoField chronoField) { 603 return switch (chronoField) { 604 case INSTANT_SECONDS -> throw new UnsupportedTemporalTypeException("Invalid field " + 605 "'InstantSeconds' for get() method, use getLong() instead"); 606 case OFFSET_SECONDS -> getOffset().getTotalSeconds(); 607 default -> dateTime.get(field); 608 }; 609 } 610 return Temporal.super.get(field); 611 } 612 613 /** 614 * Gets the value of the specified field from this date-time as a {@code long}. 615 * <p> 616 * This queries this date-time for the value of the specified field. 617 * If it is not possible to return the value, because the field is not supported 618 * or for some other reason, an exception is thrown. 619 * <p> 620 * If the field is a {@link ChronoField} then the query is implemented here. 621 * The {@link #isSupported(TemporalField) supported fields} will return valid 622 * values based on this date-time. 623 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. 624 * <p> 625 * If the field is not a {@code ChronoField}, then the result of this method 626 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} 627 * passing {@code this} as the argument. Whether the value can be obtained, 628 * and what the value represents, is determined by the field. 629 * 630 * @param field the field to get, not null 631 * @return the value for the field 632 * @throws DateTimeException if a value for the field cannot be obtained 633 * @throws UnsupportedTemporalTypeException if the field is not supported 634 * @throws ArithmeticException if numeric overflow occurs 635 */ 636 @Override 637 public long getLong(TemporalField field) { 638 if (field instanceof ChronoField chronoField) { 639 return switch (chronoField) { 640 case INSTANT_SECONDS -> toEpochSecond(); 641 case OFFSET_SECONDS -> getOffset().getTotalSeconds(); 642 default -> dateTime.getLong(field); 643 }; 644 } 645 return field.getFrom(this); 646 } 647 648 //----------------------------------------------------------------------- 649 /** 650 * Gets the zone offset, such as '+01:00'. 651 * <p> 652 * This is the offset of the local date-time from UTC/Greenwich. 653 * 654 * @return the zone offset, not null 655 */ 656 public ZoneOffset getOffset() { 657 return offset; 658 } 659 660 /** 661 * Returns a copy of this {@code OffsetDateTime} with the specified offset ensuring 662 * that the result has the same local date-time. 663 * <p> 664 * This method returns an object with the same {@code LocalDateTime} and the specified {@code ZoneOffset}. 665 * No calculation is needed or performed. 666 * For example, if this time represents {@code 2007-12-03T10:30+02:00} and the offset specified is 667 * {@code +03:00}, then this method will return {@code 2007-12-03T10:30+03:00}. 668 * <p> 669 * To take into account the difference between the offsets, and adjust the time fields, 670 * use {@link #withOffsetSameInstant}. 671 * <p> 672 * This instance is immutable and unaffected by this method call. 673 * 674 * @param offset the zone offset to change to, not null 675 * @return an {@code OffsetDateTime} based on this date-time with the requested offset, not null 676 */ 677 public OffsetDateTime withOffsetSameLocal(ZoneOffset offset) { 678 return with(dateTime, offset); 679 } 680 681 /** 682 * Returns a copy of this {@code OffsetDateTime} with the specified offset ensuring 683 * that the result is at the same instant. 684 * <p> 685 * This method returns an object with the specified {@code ZoneOffset} and a {@code LocalDateTime} 686 * adjusted by the difference between the two offsets. 687 * This will result in the old and new objects representing the same instant. 688 * This is useful for finding the local time in a different offset. 689 * For example, if this time represents {@code 2007-12-03T10:30+02:00} and the offset specified is 690 * {@code +03:00}, then this method will return {@code 2007-12-03T11:30+03:00}. 691 * <p> 692 * To change the offset without adjusting the local time use {@link #withOffsetSameLocal}. 693 * <p> 694 * This instance is immutable and unaffected by this method call. 695 * 696 * @param offset the zone offset to change to, not null 697 * @return an {@code OffsetDateTime} based on this date-time with the requested offset, not null 698 * @throws DateTimeException if the result exceeds the supported date range 699 */ 700 public OffsetDateTime withOffsetSameInstant(ZoneOffset offset) { 701 if (offset.equals(this.offset)) { 702 return this; 703 } 704 int difference = offset.getTotalSeconds() - this.offset.getTotalSeconds(); 705 LocalDateTime adjusted = dateTime.plusSeconds(difference); 706 return new OffsetDateTime(adjusted, offset); 707 } 708 709 //----------------------------------------------------------------------- 710 /** 711 * Gets the {@code LocalDateTime} part of this date-time. 712 * <p> 713 * This returns a {@code LocalDateTime} with the same year, month, day and time 714 * as this date-time. 715 * 716 * @return the local date-time part of this date-time, not null 717 */ 718 public LocalDateTime toLocalDateTime() { 719 return dateTime; 720 } 721 722 //----------------------------------------------------------------------- 723 /** 724 * Gets the {@code LocalDate} part of this date-time. 725 * <p> 726 * This returns a {@code LocalDate} with the same year, month and day 727 * as this date-time. 728 * 729 * @return the date part of this date-time, not null 730 */ 731 public LocalDate toLocalDate() { 732 return dateTime.toLocalDate(); 733 } 734 735 /** 736 * Gets the year field. 737 * <p> 738 * This method returns the primitive {@code int} value for the year. 739 * <p> 740 * The year returned by this method is proleptic as per {@code get(YEAR)}. 741 * To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}. 742 * 743 * @return the year, from MIN_YEAR to MAX_YEAR 744 */ 745 public int getYear() { 746 return dateTime.getYear(); 747 } 748 749 /** 750 * Gets the month-of-year field from 1 to 12. 751 * <p> 752 * This method returns the month as an {@code int} from 1 to 12. 753 * Application code is frequently clearer if the enum {@link Month} 754 * is used by calling {@link #getMonth()}. 755 * 756 * @return the month-of-year, from 1 to 12 757 * @see #getMonth() 758 */ 759 public int getMonthValue() { 760 return dateTime.getMonthValue(); 761 } 762 763 /** 764 * Gets the month-of-year field using the {@code Month} enum. 765 * <p> 766 * This method returns the enum {@link Month} for the month. 767 * This avoids confusion as to what {@code int} values mean. 768 * If you need access to the primitive {@code int} value then the enum 769 * provides the {@link Month#getValue() int value}. 770 * 771 * @return the month-of-year, not null 772 * @see #getMonthValue() 773 */ 774 public Month getMonth() { 775 return dateTime.getMonth(); 776 } 777 778 /** 779 * Gets the day-of-month field. 780 * <p> 781 * This method returns the primitive {@code int} value for the day-of-month. 782 * 783 * @return the day-of-month, from 1 to 31 784 */ 785 public int getDayOfMonth() { 786 return dateTime.getDayOfMonth(); 787 } 788 789 /** 790 * Gets the day-of-year field. 791 * <p> 792 * This method returns the primitive {@code int} value for the day-of-year. 793 * 794 * @return the day-of-year, from 1 to 365, or 366 in a leap year 795 */ 796 public int getDayOfYear() { 797 return dateTime.getDayOfYear(); 798 } 799 800 /** 801 * Gets the day-of-week field, which is an enum {@code DayOfWeek}. 802 * <p> 803 * This method returns the enum {@link DayOfWeek} for the day-of-week. 804 * This avoids confusion as to what {@code int} values mean. 805 * If you need access to the primitive {@code int} value then the enum 806 * provides the {@link DayOfWeek#getValue() int value}. 807 * <p> 808 * Additional information can be obtained from the {@code DayOfWeek}. 809 * This includes textual names of the values. 810 * 811 * @return the day-of-week, not null 812 */ 813 public DayOfWeek getDayOfWeek() { 814 return dateTime.getDayOfWeek(); 815 } 816 817 //----------------------------------------------------------------------- 818 /** 819 * Gets the {@code LocalTime} part of this date-time. 820 * <p> 821 * This returns a {@code LocalTime} with the same hour, minute, second and 822 * nanosecond as this date-time. 823 * 824 * @return the time part of this date-time, not null 825 */ 826 public LocalTime toLocalTime() { 827 return dateTime.toLocalTime(); 828 } 829 830 /** 831 * Gets the hour-of-day field. 832 * 833 * @return the hour-of-day, from 0 to 23 834 */ 835 public int getHour() { 836 return dateTime.getHour(); 837 } 838 839 /** 840 * Gets the minute-of-hour field. 841 * 842 * @return the minute-of-hour, from 0 to 59 843 */ 844 public int getMinute() { 845 return dateTime.getMinute(); 846 } 847 848 /** 849 * Gets the second-of-minute field. 850 * 851 * @return the second-of-minute, from 0 to 59 852 */ 853 public int getSecond() { 854 return dateTime.getSecond(); 855 } 856 857 /** 858 * Gets the nano-of-second field. 859 * 860 * @return the nano-of-second, from 0 to 999,999,999 861 */ 862 public int getNano() { 863 return dateTime.getNano(); 864 } 865 866 //----------------------------------------------------------------------- 867 /** 868 * Returns an adjusted copy of this date-time. 869 * <p> 870 * This returns an {@code OffsetDateTime}, based on this one, with the date-time adjusted. 871 * The adjustment takes place using the specified adjuster strategy object. 872 * Read the documentation of the adjuster to understand what adjustment will be made. 873 * <p> 874 * A simple adjuster might simply set the one of the fields, such as the year field. 875 * A more complex adjuster might set the date to the last day of the month. 876 * A selection of common adjustments is provided in 877 * {@link java.time.temporal.TemporalAdjusters TemporalAdjusters}. 878 * These include finding the "last day of the month" and "next Wednesday". 879 * Key date-time classes also implement the {@code TemporalAdjuster} interface, 880 * such as {@link Month} and {@link java.time.MonthDay MonthDay}. 881 * The adjuster is responsible for handling special cases, such as the varying 882 * lengths of month and leap years. 883 * <p> 884 * For example this code returns a date on the last day of July: 885 * <pre> 886 * import static java.time.Month.*; 887 * import static java.time.temporal.TemporalAdjusters.*; 888 * 889 * result = offsetDateTime.with(JULY).with(lastDayOfMonth()); 890 * </pre> 891 * <p> 892 * The classes {@link LocalDate}, {@link LocalTime} and {@link ZoneOffset} implement 893 * {@code TemporalAdjuster}, thus this method can be used to change the date, time or offset: 894 * <pre> 895 * result = offsetDateTime.with(date); 896 * result = offsetDateTime.with(time); 897 * result = offsetDateTime.with(offset); 898 * </pre> 899 * <p> 900 * The result of this method is obtained by invoking the 901 * {@link TemporalAdjuster#adjustInto(Temporal)} method on the 902 * specified adjuster passing {@code this} as the argument. 903 * <p> 904 * This instance is immutable and unaffected by this method call. 905 * 906 * @param adjuster the adjuster to use, not null 907 * @return an {@code OffsetDateTime} based on {@code this} with the adjustment made, not null 908 * @throws DateTimeException if the adjustment cannot be made 909 * @throws ArithmeticException if numeric overflow occurs 910 */ 911 @Override 912 public OffsetDateTime with(TemporalAdjuster adjuster) { 913 // optimizations 914 if (adjuster instanceof LocalDate || adjuster instanceof LocalTime || adjuster instanceof LocalDateTime) { 915 return with(dateTime.with(adjuster), offset); 916 } else if (adjuster instanceof Instant) { 917 return ofInstant((Instant) adjuster, offset); 918 } else if (adjuster instanceof ZoneOffset) { 919 return with(dateTime, (ZoneOffset) adjuster); 920 } else if (adjuster instanceof OffsetDateTime) { 921 return (OffsetDateTime) adjuster; 922 } 923 return (OffsetDateTime) adjuster.adjustInto(this); 924 } 925 926 /** 927 * Returns a copy of this date-time with the specified field set to a new value. 928 * <p> 929 * This returns an {@code OffsetDateTime}, based on this one, with the value 930 * for the specified field changed. 931 * This can be used to change any supported field, such as the year, month or day-of-month. 932 * If it is not possible to set the value, because the field is not supported or for 933 * some other reason, an exception is thrown. 934 * <p> 935 * In some cases, changing the specified field can cause the resulting date-time to become invalid, 936 * such as changing the month from 31st January to February would make the day-of-month invalid. 937 * In cases like this, the field is responsible for resolving the date. Typically it will choose 938 * the previous valid date, which would be the last valid day of February in this example. 939 * <p> 940 * If the field is a {@link ChronoField} then the adjustment is implemented here. 941 * <p> 942 * The {@code INSTANT_SECONDS} field will return a date-time with the specified instant. 943 * The offset and nano-of-second are unchanged. 944 * If the new instant value is outside the valid range then a {@code DateTimeException} will be thrown. 945 * <p> 946 * The {@code OFFSET_SECONDS} field will return a date-time with the specified offset. 947 * The local date-time is unaltered. If the new offset value is outside the valid range 948 * then a {@code DateTimeException} will be thrown. 949 * <p> 950 * The other {@link #isSupported(TemporalField) supported fields} will behave as per 951 * the matching method on {@link LocalDateTime#with(TemporalField, long) LocalDateTime}. 952 * In this case, the offset is not part of the calculation and will be unchanged. 953 * <p> 954 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. 955 * <p> 956 * If the field is not a {@code ChronoField}, then the result of this method 957 * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} 958 * passing {@code this} as the argument. In this case, the field determines 959 * whether and how to adjust the instant. 960 * <p> 961 * This instance is immutable and unaffected by this method call. 962 * 963 * @param field the field to set in the result, not null 964 * @param newValue the new value of the field in the result 965 * @return an {@code OffsetDateTime} based on {@code this} with the specified field set, not null 966 * @throws DateTimeException if the field cannot be set 967 * @throws UnsupportedTemporalTypeException if the field is not supported 968 * @throws ArithmeticException if numeric overflow occurs 969 */ 970 @Override 971 public OffsetDateTime with(TemporalField field, long newValue) { 972 if (field instanceof ChronoField chronoField) { 973 return switch (chronoField) { 974 case INSTANT_SECONDS -> ofInstant(Instant.ofEpochSecond(newValue, getNano()), offset); 975 case OFFSET_SECONDS -> 976 with(dateTime, ZoneOffset.ofTotalSeconds(chronoField.checkValidIntValue(newValue))); 977 default -> with(dateTime.with(field, newValue), offset); 978 }; 979 } 980 return field.adjustInto(this, newValue); 981 } 982 983 //----------------------------------------------------------------------- 984 /** 985 * Returns a copy of this {@code OffsetDateTime} with the year altered. 986 * <p> 987 * The time and offset do not affect the calculation and will be the same in the result. 988 * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month. 989 * <p> 990 * This instance is immutable and unaffected by this method call. 991 * 992 * @param year the year to set in the result, from MIN_YEAR to MAX_YEAR 993 * @return an {@code OffsetDateTime} based on this date-time with the requested year, not null 994 * @throws DateTimeException if the year value is invalid 995 */ 996 public OffsetDateTime withYear(int year) { 997 return with(dateTime.withYear(year), offset); 998 } 999 1000 /** 1001 * Returns a copy of this {@code OffsetDateTime} with the month-of-year altered. 1002 * <p> 1003 * The time and offset do not affect the calculation and will be the same in the result. 1004 * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month. 1005 * <p> 1006 * This instance is immutable and unaffected by this method call. 1007 * 1008 * @param month the month-of-year to set in the result, from 1 (January) to 12 (December) 1009 * @return an {@code OffsetDateTime} based on this date-time with the requested month, not null 1010 * @throws DateTimeException if the month-of-year value is invalid 1011 */ 1012 public OffsetDateTime withMonth(int month) { 1013 return with(dateTime.withMonth(month), offset); 1014 } 1015 1016 /** 1017 * Returns a copy of this {@code OffsetDateTime} with the day-of-month altered. 1018 * <p> 1019 * If the resulting {@code OffsetDateTime} is invalid, an exception is thrown. 1020 * The time and offset do not affect the calculation and will be the same in the result. 1021 * <p> 1022 * This instance is immutable and unaffected by this method call. 1023 * 1024 * @param dayOfMonth the day-of-month to set in the result, from 1 to 28-31 1025 * @return an {@code OffsetDateTime} based on this date-time with the requested day, not null 1026 * @throws DateTimeException if the day-of-month value is invalid, 1027 * or if the day-of-month is invalid for the month-year 1028 */ 1029 public OffsetDateTime withDayOfMonth(int dayOfMonth) { 1030 return with(dateTime.withDayOfMonth(dayOfMonth), offset); 1031 } 1032 1033 /** 1034 * Returns a copy of this {@code OffsetDateTime} with the day-of-year altered. 1035 * <p> 1036 * The time and offset do not affect the calculation and will be the same in the result. 1037 * If the resulting {@code OffsetDateTime} is invalid, an exception is thrown. 1038 * <p> 1039 * This instance is immutable and unaffected by this method call. 1040 * 1041 * @param dayOfYear the day-of-year to set in the result, from 1 to 365-366 1042 * @return an {@code OffsetDateTime} based on this date with the requested day, not null 1043 * @throws DateTimeException if the day-of-year value is invalid, 1044 * or if the day-of-year is invalid for the year 1045 */ 1046 public OffsetDateTime withDayOfYear(int dayOfYear) { 1047 return with(dateTime.withDayOfYear(dayOfYear), offset); 1048 } 1049 1050 //----------------------------------------------------------------------- 1051 /** 1052 * Returns a copy of this {@code OffsetDateTime} with the hour-of-day altered. 1053 * <p> 1054 * The date and offset do not affect the calculation and will be the same in the result. 1055 * <p> 1056 * This instance is immutable and unaffected by this method call. 1057 * 1058 * @param hour the hour-of-day to set in the result, from 0 to 23 1059 * @return an {@code OffsetDateTime} based on this date-time with the requested hour, not null 1060 * @throws DateTimeException if the hour value is invalid 1061 */ 1062 public OffsetDateTime withHour(int hour) { 1063 return with(dateTime.withHour(hour), offset); 1064 } 1065 1066 /** 1067 * Returns a copy of this {@code OffsetDateTime} with the minute-of-hour altered. 1068 * <p> 1069 * The date and offset do not affect the calculation and will be the same in the result. 1070 * <p> 1071 * This instance is immutable and unaffected by this method call. 1072 * 1073 * @param minute the minute-of-hour to set in the result, from 0 to 59 1074 * @return an {@code OffsetDateTime} based on this date-time with the requested minute, not null 1075 * @throws DateTimeException if the minute value is invalid 1076 */ 1077 public OffsetDateTime withMinute(int minute) { 1078 return with(dateTime.withMinute(minute), offset); 1079 } 1080 1081 /** 1082 * Returns a copy of this {@code OffsetDateTime} with the second-of-minute altered. 1083 * <p> 1084 * The date and offset do not affect the calculation and will be the same in the result. 1085 * <p> 1086 * This instance is immutable and unaffected by this method call. 1087 * 1088 * @param second the second-of-minute to set in the result, from 0 to 59 1089 * @return an {@code OffsetDateTime} based on this date-time with the requested second, not null 1090 * @throws DateTimeException if the second value is invalid 1091 */ 1092 public OffsetDateTime withSecond(int second) { 1093 return with(dateTime.withSecond(second), offset); 1094 } 1095 1096 /** 1097 * Returns a copy of this {@code OffsetDateTime} with the nano-of-second altered. 1098 * <p> 1099 * The date and offset do not affect the calculation and will be the same in the result. 1100 * <p> 1101 * This instance is immutable and unaffected by this method call. 1102 * 1103 * @param nanoOfSecond the nano-of-second to set in the result, from 0 to 999,999,999 1104 * @return an {@code OffsetDateTime} based on this date-time with the requested nanosecond, not null 1105 * @throws DateTimeException if the nano value is invalid 1106 */ 1107 public OffsetDateTime withNano(int nanoOfSecond) { 1108 return with(dateTime.withNano(nanoOfSecond), offset); 1109 } 1110 1111 //----------------------------------------------------------------------- 1112 /** 1113 * Returns a copy of this {@code OffsetDateTime} with the time truncated. 1114 * <p> 1115 * Truncation returns a copy of the original date-time with fields 1116 * smaller than the specified unit set to zero. 1117 * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit 1118 * will set the second-of-minute and nano-of-second field to zero. 1119 * <p> 1120 * The unit must have a {@linkplain TemporalUnit#getDuration() duration} 1121 * that divides into the length of a standard day without remainder. 1122 * This includes all supplied time units on {@link ChronoUnit} and 1123 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception. 1124 * <p> 1125 * The offset does not affect the calculation and will be the same in the result. 1126 * <p> 1127 * This instance is immutable and unaffected by this method call. 1128 * 1129 * @param unit the unit to truncate to, not null 1130 * @return an {@code OffsetDateTime} based on this date-time with the time truncated, not null 1131 * @throws DateTimeException if unable to truncate 1132 * @throws UnsupportedTemporalTypeException if the unit is not supported 1133 */ 1134 public OffsetDateTime truncatedTo(TemporalUnit unit) { 1135 return with(dateTime.truncatedTo(unit), offset); 1136 } 1137 1138 //----------------------------------------------------------------------- 1139 /** 1140 * Returns a copy of this date-time with the specified amount added. 1141 * <p> 1142 * This returns an {@code OffsetDateTime}, based on this one, with the specified amount added. 1143 * The amount is typically {@link Period} or {@link Duration} but may be 1144 * any other type implementing the {@link TemporalAmount} interface. 1145 * <p> 1146 * The calculation is delegated to the amount object by calling 1147 * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free 1148 * to implement the addition in any way it wishes, however it typically 1149 * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation 1150 * of the amount implementation to determine if it can be successfully added. 1151 * <p> 1152 * This instance is immutable and unaffected by this method call. 1153 * 1154 * @param amountToAdd the amount to add, not null 1155 * @return an {@code OffsetDateTime} based on this date-time with the addition made, not null 1156 * @throws DateTimeException if the addition cannot be made 1157 * @throws ArithmeticException if numeric overflow occurs 1158 */ 1159 @Override 1160 public OffsetDateTime plus(TemporalAmount amountToAdd) { 1161 return (OffsetDateTime) amountToAdd.addTo(this); 1162 } 1163 1164 /** 1165 * Returns a copy of this date-time with the specified amount added. 1166 * <p> 1167 * This returns an {@code OffsetDateTime}, based on this one, with the amount 1168 * in terms of the unit added. If it is not possible to add the amount, because the 1169 * unit is not supported or for some other reason, an exception is thrown. 1170 * <p> 1171 * If the field is a {@link ChronoUnit} then the addition is implemented by 1172 * {@link LocalDateTime#plus(long, TemporalUnit)}. 1173 * The offset is not part of the calculation and will be unchanged in the result. 1174 * <p> 1175 * If the field is not a {@code ChronoUnit}, then the result of this method 1176 * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)} 1177 * passing {@code this} as the argument. In this case, the unit determines 1178 * whether and how to perform the addition. 1179 * <p> 1180 * This instance is immutable and unaffected by this method call. 1181 * 1182 * @param amountToAdd the amount of the unit to add to the result, may be negative 1183 * @param unit the unit of the amount to add, not null 1184 * @return an {@code OffsetDateTime} based on this date-time with the specified amount added, not null 1185 * @throws DateTimeException if the addition cannot be made 1186 * @throws UnsupportedTemporalTypeException if the unit is not supported 1187 * @throws ArithmeticException if numeric overflow occurs 1188 */ 1189 @Override 1190 public OffsetDateTime plus(long amountToAdd, TemporalUnit unit) { 1191 if (unit instanceof ChronoUnit) { 1192 return with(dateTime.plus(amountToAdd, unit), offset); 1193 } 1194 return unit.addTo(this, amountToAdd); 1195 } 1196 1197 //----------------------------------------------------------------------- 1198 /** 1199 * Returns a copy of this {@code OffsetDateTime} with the specified number of years added. 1200 * <p> 1201 * This method adds the specified amount to the years field in three steps: 1202 * <ol> 1203 * <li>Add the input years to the year field</li> 1204 * <li>Check if the resulting date would be invalid</li> 1205 * <li>Adjust the day-of-month to the last valid day if necessary</li> 1206 * </ol> 1207 * <p> 1208 * For example, 2008-02-29 (leap year) plus one year would result in the 1209 * invalid date 2009-02-29 (standard year). Instead of returning an invalid 1210 * result, the last valid day of the month, 2009-02-28, is selected instead. 1211 * <p> 1212 * This instance is immutable and unaffected by this method call. 1213 * 1214 * @param years the years to add, may be negative 1215 * @return an {@code OffsetDateTime} based on this date-time with the years added, not null 1216 * @throws DateTimeException if the result exceeds the supported date range 1217 */ 1218 public OffsetDateTime plusYears(long years) { 1219 return with(dateTime.plusYears(years), offset); 1220 } 1221 1222 /** 1223 * Returns a copy of this {@code OffsetDateTime} with the specified number of months added. 1224 * <p> 1225 * This method adds the specified amount to the months field in three steps: 1226 * <ol> 1227 * <li>Add the input months to the month-of-year field</li> 1228 * <li>Check if the resulting date would be invalid</li> 1229 * <li>Adjust the day-of-month to the last valid day if necessary</li> 1230 * </ol> 1231 * <p> 1232 * For example, 2007-03-31 plus one month would result in the invalid date 1233 * 2007-04-31. Instead of returning an invalid result, the last valid day 1234 * of the month, 2007-04-30, is selected instead. 1235 * <p> 1236 * This instance is immutable and unaffected by this method call. 1237 * 1238 * @param months the months to add, may be negative 1239 * @return an {@code OffsetDateTime} based on this date-time with the months added, not null 1240 * @throws DateTimeException if the result exceeds the supported date range 1241 */ 1242 public OffsetDateTime plusMonths(long months) { 1243 return with(dateTime.plusMonths(months), offset); 1244 } 1245 1246 /** 1247 * Returns a copy of this OffsetDateTime with the specified number of weeks added. 1248 * <p> 1249 * This method adds the specified amount in weeks to the days field incrementing 1250 * the month and year fields as necessary to ensure the result remains valid. 1251 * The result is only invalid if the maximum/minimum year is exceeded. 1252 * <p> 1253 * For example, 2008-12-31 plus one week would result in 2009-01-07. 1254 * <p> 1255 * This instance is immutable and unaffected by this method call. 1256 * 1257 * @param weeks the weeks to add, may be negative 1258 * @return an {@code OffsetDateTime} based on this date-time with the weeks added, not null 1259 * @throws DateTimeException if the result exceeds the supported date range 1260 */ 1261 public OffsetDateTime plusWeeks(long weeks) { 1262 return with(dateTime.plusWeeks(weeks), offset); 1263 } 1264 1265 /** 1266 * Returns a copy of this OffsetDateTime with the specified number of days added. 1267 * <p> 1268 * This method adds the specified amount to the days field incrementing the 1269 * month and year fields as necessary to ensure the result remains valid. 1270 * The result is only invalid if the maximum/minimum year is exceeded. 1271 * <p> 1272 * For example, 2008-12-31 plus one day would result in 2009-01-01. 1273 * <p> 1274 * This instance is immutable and unaffected by this method call. 1275 * 1276 * @param days the days to add, may be negative 1277 * @return an {@code OffsetDateTime} based on this date-time with the days added, not null 1278 * @throws DateTimeException if the result exceeds the supported date range 1279 */ 1280 public OffsetDateTime plusDays(long days) { 1281 return with(dateTime.plusDays(days), offset); 1282 } 1283 1284 /** 1285 * Returns a copy of this {@code OffsetDateTime} with the specified number of hours added. 1286 * <p> 1287 * This instance is immutable and unaffected by this method call. 1288 * 1289 * @param hours the hours to add, may be negative 1290 * @return an {@code OffsetDateTime} based on this date-time with the hours added, not null 1291 * @throws DateTimeException if the result exceeds the supported date range 1292 */ 1293 public OffsetDateTime plusHours(long hours) { 1294 return with(dateTime.plusHours(hours), offset); 1295 } 1296 1297 /** 1298 * Returns a copy of this {@code OffsetDateTime} with the specified number of minutes added. 1299 * <p> 1300 * This instance is immutable and unaffected by this method call. 1301 * 1302 * @param minutes the minutes to add, may be negative 1303 * @return an {@code OffsetDateTime} based on this date-time with the minutes added, not null 1304 * @throws DateTimeException if the result exceeds the supported date range 1305 */ 1306 public OffsetDateTime plusMinutes(long minutes) { 1307 return with(dateTime.plusMinutes(minutes), offset); 1308 } 1309 1310 /** 1311 * Returns a copy of this {@code OffsetDateTime} with the specified number of seconds added. 1312 * <p> 1313 * This instance is immutable and unaffected by this method call. 1314 * 1315 * @param seconds the seconds to add, may be negative 1316 * @return an {@code OffsetDateTime} based on this date-time with the seconds added, not null 1317 * @throws DateTimeException if the result exceeds the supported date range 1318 */ 1319 public OffsetDateTime plusSeconds(long seconds) { 1320 return with(dateTime.plusSeconds(seconds), offset); 1321 } 1322 1323 /** 1324 * Returns a copy of this {@code OffsetDateTime} with the specified number of nanoseconds added. 1325 * <p> 1326 * This instance is immutable and unaffected by this method call. 1327 * 1328 * @param nanos the nanos to add, may be negative 1329 * @return an {@code OffsetDateTime} based on this date-time with the nanoseconds added, not null 1330 * @throws DateTimeException if the unit cannot be added to this type 1331 */ 1332 public OffsetDateTime plusNanos(long nanos) { 1333 return with(dateTime.plusNanos(nanos), offset); 1334 } 1335 1336 //----------------------------------------------------------------------- 1337 /** 1338 * Returns a copy of this date-time with the specified amount subtracted. 1339 * <p> 1340 * This returns an {@code OffsetDateTime}, based on this one, with the specified amount subtracted. 1341 * The amount is typically {@link Period} or {@link Duration} but may be 1342 * any other type implementing the {@link TemporalAmount} interface. 1343 * <p> 1344 * The calculation is delegated to the amount object by calling 1345 * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free 1346 * to implement the subtraction in any way it wishes, however it typically 1347 * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation 1348 * of the amount implementation to determine if it can be successfully subtracted. 1349 * <p> 1350 * This instance is immutable and unaffected by this method call. 1351 * 1352 * @param amountToSubtract the amount to subtract, not null 1353 * @return an {@code OffsetDateTime} based on this date-time with the subtraction made, not null 1354 * @throws DateTimeException if the subtraction cannot be made 1355 * @throws ArithmeticException if numeric overflow occurs 1356 */ 1357 @Override 1358 public OffsetDateTime minus(TemporalAmount amountToSubtract) { 1359 return (OffsetDateTime) amountToSubtract.subtractFrom(this); 1360 } 1361 1362 /** 1363 * Returns a copy of this date-time with the specified amount subtracted. 1364 * <p> 1365 * This returns an {@code OffsetDateTime}, based on this one, with the amount 1366 * in terms of the unit subtracted. If it is not possible to subtract the amount, 1367 * because the unit is not supported or for some other reason, an exception is thrown. 1368 * <p> 1369 * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated. 1370 * See that method for a full description of how addition, and thus subtraction, works. 1371 * <p> 1372 * This instance is immutable and unaffected by this method call. 1373 * 1374 * @param amountToSubtract the amount of the unit to subtract from the result, may be negative 1375 * @param unit the unit of the amount to subtract, not null 1376 * @return an {@code OffsetDateTime} based on this date-time with the specified amount subtracted, not null 1377 * @throws DateTimeException if the subtraction cannot be made 1378 * @throws UnsupportedTemporalTypeException if the unit is not supported 1379 * @throws ArithmeticException if numeric overflow occurs 1380 */ 1381 @Override 1382 public OffsetDateTime minus(long amountToSubtract, TemporalUnit unit) { 1383 return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); 1384 } 1385 1386 //----------------------------------------------------------------------- 1387 /** 1388 * Returns a copy of this {@code OffsetDateTime} with the specified number of years subtracted. 1389 * <p> 1390 * This method subtracts the specified amount from the years field in three steps: 1391 * <ol> 1392 * <li>Subtract the input years from the year field</li> 1393 * <li>Check if the resulting date would be invalid</li> 1394 * <li>Adjust the day-of-month to the last valid day if necessary</li> 1395 * </ol> 1396 * <p> 1397 * For example, 2008-02-29 (leap year) minus one year would result in the 1398 * invalid date 2007-02-29 (standard year). Instead of returning an invalid 1399 * result, the last valid day of the month, 2007-02-28, is selected instead. 1400 * <p> 1401 * This instance is immutable and unaffected by this method call. 1402 * 1403 * @param years the years to subtract, may be negative 1404 * @return an {@code OffsetDateTime} based on this date-time with the years subtracted, not null 1405 * @throws DateTimeException if the result exceeds the supported date range 1406 */ 1407 public OffsetDateTime minusYears(long years) { 1408 return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years)); 1409 } 1410 1411 /** 1412 * Returns a copy of this {@code OffsetDateTime} with the specified number of months subtracted. 1413 * <p> 1414 * This method subtracts the specified amount from the months field in three steps: 1415 * <ol> 1416 * <li>Subtract the input months from the month-of-year field</li> 1417 * <li>Check if the resulting date would be invalid</li> 1418 * <li>Adjust the day-of-month to the last valid day if necessary</li> 1419 * </ol> 1420 * <p> 1421 * For example, 2007-03-31 minus one month would result in the invalid date 1422 * 2007-02-31. Instead of returning an invalid result, the last valid day 1423 * of the month, 2007-02-28, is selected instead. 1424 * <p> 1425 * This instance is immutable and unaffected by this method call. 1426 * 1427 * @param months the months to subtract, may be negative 1428 * @return an {@code OffsetDateTime} based on this date-time with the months subtracted, not null 1429 * @throws DateTimeException if the result exceeds the supported date range 1430 */ 1431 public OffsetDateTime minusMonths(long months) { 1432 return (months == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-months)); 1433 } 1434 1435 /** 1436 * Returns a copy of this {@code OffsetDateTime} with the specified number of weeks subtracted. 1437 * <p> 1438 * This method subtracts the specified amount in weeks from the days field decrementing 1439 * the month and year fields as necessary to ensure the result remains valid. 1440 * The result is only invalid if the maximum/minimum year is exceeded. 1441 * <p> 1442 * For example, 2009-01-07 minus one week would result in 2008-12-31. 1443 * <p> 1444 * This instance is immutable and unaffected by this method call. 1445 * 1446 * @param weeks the weeks to subtract, may be negative 1447 * @return an {@code OffsetDateTime} based on this date-time with the weeks subtracted, not null 1448 * @throws DateTimeException if the result exceeds the supported date range 1449 */ 1450 public OffsetDateTime minusWeeks(long weeks) { 1451 return (weeks == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeks)); 1452 } 1453 1454 /** 1455 * Returns a copy of this {@code OffsetDateTime} with the specified number of days subtracted. 1456 * <p> 1457 * This method subtracts the specified amount from the days field decrementing the 1458 * month and year fields as necessary to ensure the result remains valid. 1459 * The result is only invalid if the maximum/minimum year is exceeded. 1460 * <p> 1461 * For example, 2009-01-01 minus one day would result in 2008-12-31. 1462 * <p> 1463 * This instance is immutable and unaffected by this method call. 1464 * 1465 * @param days the days to subtract, may be negative 1466 * @return an {@code OffsetDateTime} based on this date-time with the days subtracted, not null 1467 * @throws DateTimeException if the result exceeds the supported date range 1468 */ 1469 public OffsetDateTime minusDays(long days) { 1470 return (days == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-days)); 1471 } 1472 1473 /** 1474 * Returns a copy of this {@code OffsetDateTime} with the specified number of hours subtracted. 1475 * <p> 1476 * This instance is immutable and unaffected by this method call. 1477 * 1478 * @param hours the hours to subtract, may be negative 1479 * @return an {@code OffsetDateTime} based on this date-time with the hours subtracted, not null 1480 * @throws DateTimeException if the result exceeds the supported date range 1481 */ 1482 public OffsetDateTime minusHours(long hours) { 1483 return (hours == Long.MIN_VALUE ? plusHours(Long.MAX_VALUE).plusHours(1) : plusHours(-hours)); 1484 } 1485 1486 /** 1487 * Returns a copy of this {@code OffsetDateTime} with the specified number of minutes subtracted. 1488 * <p> 1489 * This instance is immutable and unaffected by this method call. 1490 * 1491 * @param minutes the minutes to subtract, may be negative 1492 * @return an {@code OffsetDateTime} based on this date-time with the minutes subtracted, not null 1493 * @throws DateTimeException if the result exceeds the supported date range 1494 */ 1495 public OffsetDateTime minusMinutes(long minutes) { 1496 return (minutes == Long.MIN_VALUE ? plusMinutes(Long.MAX_VALUE).plusMinutes(1) : plusMinutes(-minutes)); 1497 } 1498 1499 /** 1500 * Returns a copy of this {@code OffsetDateTime} with the specified number of seconds subtracted. 1501 * <p> 1502 * This instance is immutable and unaffected by this method call. 1503 * 1504 * @param seconds the seconds to subtract, may be negative 1505 * @return an {@code OffsetDateTime} based on this date-time with the seconds subtracted, not null 1506 * @throws DateTimeException if the result exceeds the supported date range 1507 */ 1508 public OffsetDateTime minusSeconds(long seconds) { 1509 return (seconds == Long.MIN_VALUE ? plusSeconds(Long.MAX_VALUE).plusSeconds(1) : plusSeconds(-seconds)); 1510 } 1511 1512 /** 1513 * Returns a copy of this {@code OffsetDateTime} with the specified number of nanoseconds subtracted. 1514 * <p> 1515 * This instance is immutable and unaffected by this method call. 1516 * 1517 * @param nanos the nanos to subtract, may be negative 1518 * @return an {@code OffsetDateTime} based on this date-time with the nanoseconds subtracted, not null 1519 * @throws DateTimeException if the result exceeds the supported date range 1520 */ 1521 public OffsetDateTime minusNanos(long nanos) { 1522 return (nanos == Long.MIN_VALUE ? plusNanos(Long.MAX_VALUE).plusNanos(1) : plusNanos(-nanos)); 1523 } 1524 1525 //----------------------------------------------------------------------- 1526 /** 1527 * Queries this date-time using the specified query. 1528 * <p> 1529 * This queries this date-time using the specified query strategy object. 1530 * The {@code TemporalQuery} object defines the logic to be used to 1531 * obtain the result. Read the documentation of the query to understand 1532 * what the result of this method will be. 1533 * <p> 1534 * The result of this method is obtained by invoking the 1535 * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the 1536 * specified query passing {@code this} as the argument. 1537 * 1538 * @param <R> the type of the result 1539 * @param query the query to invoke, not null 1540 * @return the query result, null may be returned (defined by the query) 1541 * @throws DateTimeException if unable to query (defined by the query) 1542 * @throws ArithmeticException if numeric overflow occurs (defined by the query) 1543 */ 1544 @SuppressWarnings("unchecked") 1545 @Override 1546 public <R> R query(TemporalQuery<R> query) { 1547 if (query == TemporalQueries.offset() || query == TemporalQueries.zone()) { 1548 return (R) getOffset(); 1549 } else if (query == TemporalQueries.zoneId()) { 1550 return null; 1551 } else if (query == TemporalQueries.localDate()) { 1552 return (R) toLocalDate(); 1553 } else if (query == TemporalQueries.localTime()) { 1554 return (R) toLocalTime(); 1555 } else if (query == TemporalQueries.chronology()) { 1556 return (R) IsoChronology.INSTANCE; 1557 } else if (query == TemporalQueries.precision()) { 1558 return (R) NANOS; 1559 } 1560 // inline TemporalAccessor.super.query(query) as an optimization 1561 // non-JDK classes are not permitted to make this optimization 1562 return query.queryFrom(this); 1563 } 1564 1565 /** 1566 * Adjusts the specified temporal object to have the same offset, date 1567 * and time as this object. 1568 * <p> 1569 * This returns a temporal object of the same observable type as the input 1570 * with the offset, date and time changed to be the same as this. 1571 * <p> 1572 * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} 1573 * three times, passing {@link ChronoField#EPOCH_DAY}, 1574 * {@link ChronoField#NANO_OF_DAY} and {@link ChronoField#OFFSET_SECONDS} as the fields. 1575 * <p> 1576 * In most cases, it is clearer to reverse the calling pattern by using 1577 * {@link Temporal#with(TemporalAdjuster)}: 1578 * <pre> 1579 * // these two lines are equivalent, but the second approach is recommended 1580 * temporal = thisOffsetDateTime.adjustInto(temporal); 1581 * temporal = temporal.with(thisOffsetDateTime); 1582 * </pre> 1583 * <p> 1584 * This instance is immutable and unaffected by this method call. 1585 * 1586 * @param temporal the target object to be adjusted, not null 1587 * @return the adjusted object, not null 1588 * @throws DateTimeException if unable to make the adjustment 1589 * @throws ArithmeticException if numeric overflow occurs 1590 */ 1591 @Override 1592 public Temporal adjustInto(Temporal temporal) { 1593 // OffsetDateTime is treated as three separate fields, not an instant 1594 // this produces the most consistent set of results overall 1595 // the offset is set after the date and time, as it is typically a small 1596 // tweak to the result, with ZonedDateTime frequently ignoring the offset 1597 return temporal 1598 .with(EPOCH_DAY, toLocalDate().toEpochDay()) 1599 .with(NANO_OF_DAY, toLocalTime().toNanoOfDay()) 1600 .with(OFFSET_SECONDS, getOffset().getTotalSeconds()); 1601 } 1602 1603 /** 1604 * Calculates the amount of time until another date-time in terms of the specified unit. 1605 * <p> 1606 * This calculates the amount of time between two {@code OffsetDateTime} 1607 * objects in terms of a single {@code TemporalUnit}. 1608 * The start and end points are {@code this} and the specified date-time. 1609 * The result will be negative if the end is before the start. 1610 * For example, the amount in days between two date-times can be calculated 1611 * using {@code startDateTime.until(endDateTime, DAYS)}. 1612 * <p> 1613 * The {@code Temporal} passed to this method is converted to a 1614 * {@code OffsetDateTime} using {@link #from(TemporalAccessor)}. 1615 * If the offset differs between the two date-times, the specified 1616 * end date-time is normalized to have the same offset as this date-time. 1617 * <p> 1618 * The calculation returns a whole number, representing the number of 1619 * complete units between the two date-times. 1620 * For example, the amount in months between 2012-06-15T00:00Z and 2012-08-14T23:59Z 1621 * will only be one month as it is one minute short of two months. 1622 * <p> 1623 * There are two equivalent ways of using this method. 1624 * The first is to invoke this method. 1625 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: 1626 * <pre> 1627 * // these two lines are equivalent 1628 * amount = start.until(end, MONTHS); 1629 * amount = MONTHS.between(start, end); 1630 * </pre> 1631 * The choice should be made based on which makes the code more readable. 1632 * <p> 1633 * The calculation is implemented in this method for {@link ChronoUnit}. 1634 * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS}, 1635 * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS}, 1636 * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES}, 1637 * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported. 1638 * Other {@code ChronoUnit} values will throw an exception. 1639 * <p> 1640 * If the unit is not a {@code ChronoUnit}, then the result of this method 1641 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} 1642 * passing {@code this} as the first argument and the converted input temporal 1643 * as the second argument. 1644 * <p> 1645 * This instance is immutable and unaffected by this method call. 1646 * 1647 * @param endExclusive the end date, exclusive, which is converted to an {@code OffsetDateTime}, not null 1648 * @param unit the unit to measure the amount in, not null 1649 * @return the amount of time between this date-time and the end date-time 1650 * @throws DateTimeException if the amount cannot be calculated, or the end 1651 * temporal cannot be converted to an {@code OffsetDateTime} 1652 * @throws UnsupportedTemporalTypeException if the unit is not supported 1653 * @throws ArithmeticException if numeric overflow occurs 1654 */ 1655 @Override 1656 public long until(Temporal endExclusive, TemporalUnit unit) { 1657 OffsetDateTime end = OffsetDateTime.from(endExclusive); 1658 if (unit instanceof ChronoUnit) { 1659 OffsetDateTime start = this; 1660 try { 1661 end = end.withOffsetSameInstant(offset); 1662 } catch (DateTimeException ex) { 1663 // end may be out of valid range. Adjust to end's offset. 1664 start = withOffsetSameInstant(end.offset); 1665 } 1666 return start.dateTime.until(end.dateTime, unit); 1667 } 1668 return unit.between(this, end); 1669 } 1670 1671 /** 1672 * Formats this date-time using the specified formatter. 1673 * <p> 1674 * This date-time will be passed to the formatter to produce a string. 1675 * 1676 * @param formatter the formatter to use, not null 1677 * @return the formatted date-time string, not null 1678 * @throws DateTimeException if an error occurs during printing 1679 */ 1680 public String format(DateTimeFormatter formatter) { 1681 Objects.requireNonNull(formatter, "formatter"); 1682 return formatter.format(this); 1683 } 1684 1685 //----------------------------------------------------------------------- 1686 /** 1687 * Combines this date-time with a time-zone to create a {@code ZonedDateTime} 1688 * ensuring that the result has the same instant. 1689 * <p> 1690 * This returns a {@code ZonedDateTime} formed from this date-time and the specified time-zone. 1691 * This conversion will ignore the visible local date-time and use the underlying instant instead. 1692 * This avoids any problems with local time-line gaps or overlaps. 1693 * The result might have different values for fields such as hour, minute an even day. 1694 * <p> 1695 * To attempt to retain the values of the fields, use {@link #atZoneSimilarLocal(ZoneId)}. 1696 * To use the offset as the zone ID, use {@link #toZonedDateTime()}. 1697 * 1698 * @param zone the time-zone to use, not null 1699 * @return the zoned date-time formed from this date-time, not null 1700 */ 1701 public ZonedDateTime atZoneSameInstant(ZoneId zone) { 1702 return ZonedDateTime.ofInstant(dateTime, offset, zone); 1703 } 1704 1705 /** 1706 * Combines this date-time with a time-zone to create a {@code ZonedDateTime} 1707 * trying to keep the same local date and time. 1708 * <p> 1709 * This returns a {@code ZonedDateTime} formed from this date-time and the specified time-zone. 1710 * Where possible, the result will have the same local date-time as this object. 1711 * <p> 1712 * Time-zone rules, such as daylight savings, mean that not every time on the 1713 * local time-line exists. If the local date-time is in a gap or overlap according to 1714 * the rules then a resolver is used to determine the resultant local time and offset. 1715 * This method uses {@link ZonedDateTime#ofLocal(LocalDateTime, ZoneId, ZoneOffset)} 1716 * to retain the offset from this instance if possible. 1717 * <p> 1718 * Finer control over gaps and overlaps is available in two ways. 1719 * If you simply want to use the later offset at overlaps then call 1720 * {@link ZonedDateTime#withLaterOffsetAtOverlap()} immediately after this method. 1721 * <p> 1722 * To create a zoned date-time at the same instant irrespective of the local time-line, 1723 * use {@link #atZoneSameInstant(ZoneId)}. 1724 * To use the offset as the zone ID, use {@link #toZonedDateTime()}. 1725 * 1726 * @param zone the time-zone to use, not null 1727 * @return the zoned date-time formed from this date and the earliest valid time for the zone, not null 1728 */ 1729 public ZonedDateTime atZoneSimilarLocal(ZoneId zone) { 1730 return ZonedDateTime.ofLocal(dateTime, zone, offset); 1731 } 1732 1733 //----------------------------------------------------------------------- 1734 /** 1735 * Converts this date-time to an {@code OffsetTime}. 1736 * <p> 1737 * This returns an offset time with the same local time and offset. 1738 * 1739 * @return an OffsetTime representing the time and offset, not null 1740 */ 1741 public OffsetTime toOffsetTime() { 1742 return OffsetTime.of(dateTime.toLocalTime(), offset); 1743 } 1744 1745 /** 1746 * Converts this date-time to a {@code ZonedDateTime} using the offset as the zone ID. 1747 * <p> 1748 * This creates the simplest possible {@code ZonedDateTime} using the offset 1749 * as the zone ID. 1750 * <p> 1751 * To control the time-zone used, see {@link #atZoneSameInstant(ZoneId)} and 1752 * {@link #atZoneSimilarLocal(ZoneId)}. 1753 * 1754 * @return a zoned date-time representing the same local date-time and offset, not null 1755 */ 1756 public ZonedDateTime toZonedDateTime() { 1757 return ZonedDateTime.of(dateTime, offset); 1758 } 1759 1760 /** 1761 * Converts this date-time to an {@code Instant}. 1762 * <p> 1763 * This returns an {@code Instant} representing the same point on the 1764 * time-line as this date-time. 1765 * 1766 * @return an {@code Instant} representing the same instant, not null 1767 */ 1768 public Instant toInstant() { 1769 return dateTime.toInstant(offset); 1770 } 1771 1772 /** 1773 * Converts this date-time to the number of seconds from the epoch of 1970-01-01T00:00:00Z. 1774 * <p> 1775 * This allows this date-time to be converted to a value of the 1776 * {@link ChronoField#INSTANT_SECONDS epoch-seconds} field. This is primarily 1777 * intended for low-level conversions rather than general application usage. 1778 * 1779 * @return the number of seconds from the epoch of 1970-01-01T00:00:00Z 1780 */ 1781 public long toEpochSecond() { 1782 return dateTime.toEpochSecond(offset); 1783 } 1784 1785 //----------------------------------------------------------------------- 1786 /** 1787 * Compares this date-time to another date-time. 1788 * <p> 1789 * The comparison is based on the instant then on the local date-time. 1790 * It is "consistent with equals", as defined by {@link Comparable}. 1791 * <p> 1792 * For example, the following is the comparator order: 1793 * <ol> 1794 * <li>{@code 2008-12-03T10:30+01:00}</li> 1795 * <li>{@code 2008-12-03T11:00+01:00}</li> 1796 * <li>{@code 2008-12-03T12:00+02:00}</li> 1797 * <li>{@code 2008-12-03T11:30+01:00}</li> 1798 * <li>{@code 2008-12-03T12:00+01:00}</li> 1799 * <li>{@code 2008-12-03T12:30+01:00}</li> 1800 * </ol> 1801 * Values #2 and #3 represent the same instant on the time-line. 1802 * When two values represent the same instant, the local date-time is compared 1803 * to distinguish them. This step is needed to make the ordering 1804 * consistent with {@code equals()}. 1805 * 1806 * @param other the other date-time to compare to, not null 1807 * @return the comparator value, that is the comparison with the {@code other}'s instant, if they are not equal; 1808 * and if equal to the {@code other}'s instant, the comparison of the {@code other}'s local date-time 1809 * @see #isBefore 1810 * @see #isAfter 1811 */ 1812 @Override 1813 public int compareTo(OffsetDateTime other) { 1814 int cmp = getOffset().compareTo(other.getOffset()); 1815 if (cmp != 0) { 1816 cmp = Long.compare(toEpochSecond(), other.toEpochSecond()); 1817 if (cmp == 0) { 1818 cmp = toLocalTime().getNano() - other.toLocalTime().getNano(); 1819 } 1820 } 1821 if (cmp == 0) { 1822 cmp = toLocalDateTime().compareTo(other.toLocalDateTime()); 1823 } 1824 return cmp; 1825 } 1826 1827 //----------------------------------------------------------------------- 1828 /** 1829 * Checks if the instant of this date-time is after that of the specified date-time. 1830 * <p> 1831 * This method differs from the comparison in {@link #compareTo} and {@link #equals} in that it 1832 * only compares the instant of the date-time. This is equivalent to using 1833 * {@code dateTime1.toInstant().isAfter(dateTime2.toInstant());}. 1834 * 1835 * @param other the other date-time to compare to, not null 1836 * @return true if this is after the instant of the specified date-time 1837 */ 1838 public boolean isAfter(OffsetDateTime other) { 1839 long thisEpochSec = toEpochSecond(); 1840 long otherEpochSec = other.toEpochSecond(); 1841 return thisEpochSec > otherEpochSec || 1842 (thisEpochSec == otherEpochSec && toLocalTime().getNano() > other.toLocalTime().getNano()); 1843 } 1844 1845 /** 1846 * Checks if the instant of this date-time is before that of the specified date-time. 1847 * <p> 1848 * This method differs from the comparison in {@link #compareTo} in that it 1849 * only compares the instant of the date-time. This is equivalent to using 1850 * {@code dateTime1.toInstant().isBefore(dateTime2.toInstant());}. 1851 * 1852 * @param other the other date-time to compare to, not null 1853 * @return true if this is before the instant of the specified date-time 1854 */ 1855 public boolean isBefore(OffsetDateTime other) { 1856 long thisEpochSec = toEpochSecond(); 1857 long otherEpochSec = other.toEpochSecond(); 1858 return thisEpochSec < otherEpochSec || 1859 (thisEpochSec == otherEpochSec && toLocalTime().getNano() < other.toLocalTime().getNano()); 1860 } 1861 1862 /** 1863 * Checks if the instant of this date-time is equal to that of the specified date-time. 1864 * <p> 1865 * This method differs from the comparison in {@link #compareTo} and {@link #equals} 1866 * in that it only compares the instant of the date-time. This is equivalent to using 1867 * {@code dateTime1.toInstant().equals(dateTime2.toInstant());}. 1868 * 1869 * @param other the other date-time to compare to, not null 1870 * @return true if the instant equals the instant of the specified date-time 1871 */ 1872 public boolean isEqual(OffsetDateTime other) { 1873 return toEpochSecond() == other.toEpochSecond() && 1874 toLocalTime().getNano() == other.toLocalTime().getNano(); 1875 } 1876 1877 //----------------------------------------------------------------------- 1878 /** 1879 * Checks if this date-time is equal to another date-time. 1880 * <p> 1881 * The comparison is based on the local date-time and the offset. 1882 * To compare for the same instant on the time-line, use {@link #isEqual}. 1883 * Only objects of type {@code OffsetDateTime} are compared, other types return false. 1884 * 1885 * @param obj the object to check, null returns false 1886 * @return true if this is equal to the other date-time 1887 */ 1888 @Override 1889 public boolean equals(Object obj) { 1890 if (this == obj) { 1891 return true; 1892 } 1893 return (obj instanceof OffsetDateTime other) 1894 && dateTime.equals(other.dateTime) 1895 && offset.equals(other.offset); 1896 } 1897 1898 /** 1899 * A hash code for this date-time. 1900 * 1901 * @return a suitable hash code 1902 */ 1903 @Override 1904 public int hashCode() { 1905 return dateTime.hashCode() ^ offset.hashCode(); 1906 } 1907 1908 //----------------------------------------------------------------------- 1909 /** 1910 * Outputs this date-time as a {@code String}, such as {@code 2007-12-03T10:15:30+01:00}. 1911 * <p> 1912 * The output will be one of the following formats: 1913 * <ul> 1914 * <li>{@code uuuu-MM-dd'T'HH:mmXXXXX}</li> 1915 * <li>{@code uuuu-MM-dd'T'HH:mm:ssXXXXX}</li> 1916 * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX}</li> 1917 * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSSXXXXX}</li> 1918 * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX}</li> 1919 * </ul> 1920 * The format used will be the shortest that outputs the full value of 1921 * the time where the omitted parts are implied to be zero. The output 1922 * is compatible with ISO 8601 if the seconds in the offset are zero. 1923 * 1924 * @return a string representation of this date-time, not null 1925 */ 1926 @Override 1927 public String toString() { 1928 var offsetStr = offset.toString(); 1929 var buf = new StringBuilder(29 + offsetStr.length()); 1930 DateTimeHelper.formatTo(buf, dateTime); 1931 return buf.append(offsetStr).toString(); 1932 } 1933 1934 //----------------------------------------------------------------------- 1935 /** 1936 * Writes the object using a 1937 * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>. 1938 * @serialData 1939 * <pre> 1940 * out.writeByte(10); // identifies an OffsetDateTime 1941 * // the <a href="{@docRoot}/serialized-form.html#java.time.LocalDateTime">datetime</a> excluding the one byte header 1942 * // the <a href="{@docRoot}/serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header 1943 * </pre> 1944 * 1945 * @return the instance of {@code Ser}, not null 1946 */ 1947 @java.io.Serial 1948 private Object writeReplace() { 1949 return new Ser(Ser.OFFSET_DATE_TIME_TYPE, this); 1950 } 1951 1952 /** 1953 * Defend against malicious streams. 1954 * 1955 * @param s the stream to read 1956 * @throws InvalidObjectException always 1957 */ 1958 @java.io.Serial 1959 private void readObject(ObjectInputStream s) throws InvalidObjectException { 1960 throw new InvalidObjectException("Deserialization via serialization delegate"); 1961 } 1962 1963 void writeExternal(ObjectOutput out) throws IOException { 1964 dateTime.writeExternal(out); 1965 offset.writeExternal(out); 1966 } 1967 1968 static OffsetDateTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 1969 LocalDateTime dateTime = LocalDateTime.readExternal(in); 1970 ZoneOffset offset = ZoneOffset.readExternal(in); 1971 return OffsetDateTime.of(dateTime, offset); 1972 } 1973 1974 }