jdk/src/share/classes/java/time/Period.java
author sherman
Tue, 22 Jan 2013 20:59:21 -0800
changeset 15289 3ac550392e43
child 15658 55b829ca2334
permissions -rw-r--r--
8003680: JSR 310 Date/Time API Summary: Integration of JSR310 Date/Time API for M6 Reviewed-by: alanb, naoto, dholmes Contributed-by: scolebourne@joda.org, roger.riggs@oracle.com, richard.warburton@gmail.com, misterm@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    30
 * file:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    31
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    32
 * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    33
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    34
 * All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    35
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    38
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    41
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    45
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    49
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    61
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    62
package java.time;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    64
import static java.time.LocalTime.NANOS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.LocalTime.NANOS_PER_HOUR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.LocalTime.NANOS_PER_MINUTE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
import static java.time.LocalTime.NANOS_PER_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
import static java.time.temporal.ChronoField.EPOCH_MONTH;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
import static java.time.temporal.ChronoField.NANO_OF_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
import static java.time.temporal.ChronoField.YEAR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
import static java.time.temporal.ChronoUnit.DAYS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
import static java.time.temporal.ChronoUnit.MONTHS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import static java.time.temporal.ChronoUnit.NANOS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import static java.time.temporal.ChronoUnit.YEARS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.io.Serializable;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.time.format.DateTimeParseException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
import java.time.temporal.Chrono;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
import java.time.temporal.ChronoField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
import java.time.temporal.ChronoUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
import java.time.temporal.Temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
import java.time.temporal.TemporalAccessor;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
import java.time.temporal.TemporalAdder;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
import java.time.temporal.TemporalSubtractor;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
import java.time.temporal.TemporalUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
import java.time.temporal.ValueRange;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
import java.util.Objects;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
 * A period of time, measured using the most common units, such as '3 Months, 4 Days and 7 Hours'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
 * A {@code Period} represents an amount of time measured in terms of the most commonly used units:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
 * <p><ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
 * <li>{@link ChronoUnit#YEARS YEARS}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
 * <li>{@link ChronoUnit#MONTHS MONTHS}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 * <li>{@link ChronoUnit#DAYS DAYS}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 * <li>time units with an {@linkplain TemporalUnit#isDurationEstimated() exact duration}</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
 * </ul><p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
 * The period may be used with any calendar system with the exception is methods with an "ISO" suffix.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
 * The meaning of a "year" or a "month" is only applied when the object is added to a date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
 * The period is modeled as a directed amount of time, meaning that individual parts of the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
 * period may be negative.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
 * <h3>Specification for implementors</h3>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
 * This class is immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
 * The maximum number of hours that can be stored is about 2.5 million, limited by storing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
 * a single {@code long} nanoseconds for all time units internally.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
public final class Period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
        implements TemporalAdder, TemporalSubtractor, Serializable {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
    // maximum hours is 2,562,047
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
     * A constant for a period of zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
    public static final Period ZERO = new Period(0, 0, 0, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
     * Serialization version.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
    private static final long serialVersionUID = -8290556941213247973L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
     * The number of years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
    private final int years;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
     * The number of months.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
    private final int months;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
     * The number of days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
    private final int days;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
     * The number of nanoseconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
    private final long nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
     * Obtains a {@code Period} from date-based and time-based fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
     * This creates an instance based on years, months, days, hours, minutes and seconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
     * Within a period, the time fields are always normalized.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
     * @param years  the amount of years, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
     * @param months  the amount of months, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
     * @param days  the amount of days, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
     * @param hours  the amount of hours, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
     * @param minutes  the amount of minutes, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
     * @param seconds  the amount of seconds, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
     * @return the period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
    public static Period of(int years, int months, int days, int hours, int minutes, int seconds) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
        return of(years, months, days, hours, minutes, seconds, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
     * Obtains a {@code Period} from date-based and time-based fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
     * This creates an instance based on years, months, days, hours, minutes, seconds and nanoseconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
     * Within a period, the time fields are always normalized.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
     * @param years  the amount of years, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
     * @param months  the amount of months, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
     * @param days  the amount of days, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
     * @param hours  the amount of hours, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
     * @param minutes  the amount of minutes, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
     * @param seconds  the amount of seconds, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
     * @param nanos  the amount of nanos, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
     * @return the period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
    public static Period of(int years, int months, int days, int hours, int minutes, int seconds, long nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
        if ((years | months | days | hours | minutes | seconds | nanos) == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
            return ZERO;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
        long totSecs = Math.addExact(hours * 3600L, minutes * 60L) + seconds;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
        long totNanos = Math.addExact(Math.multiplyExact(totSecs, 1_000_000_000L), nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
        return create(years, months, days, totNanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
     * Obtains a {@code Period} from date-based fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
     * This creates an instance based on years, months and days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
     * @param years  the amount of years, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
     * @param months  the amount of months, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
     * @param days  the amount of days, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
     * @return the period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
    public static Period ofDate(int years, int months, int days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
        return of(years, months, days, 0, 0, 0, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
     * Obtains a {@code Period} from time-based fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
     * This creates an instance based on hours, minutes and seconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
     * Within a period, the time fields are always normalized.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
     * @param hours  the amount of hours, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
     * @param minutes  the amount of minutes, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
     * @param seconds  the amount of seconds, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
     * @return the period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
    public static Period ofTime(int hours, int minutes, int seconds) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
        return of(0, 0, 0, hours, minutes, seconds, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     * Obtains a {@code Period} from time-based fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
     * This creates an instance based on hours, minutes, seconds and nanoseconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
     * Within a period, the time fields are always normalized.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
     * @param hours  the amount of hours, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
     * @param minutes  the amount of minutes, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
     * @param seconds  the amount of seconds, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
     * @param nanos  the amount of nanos, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
     * @return the period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
    public static Period ofTime(int hours, int minutes, int seconds, long nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
        return of(0, 0, 0, hours, minutes, seconds, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
     * Obtains an instance of {@code Period} from a period in the specified unit.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
     * The parameters represent the two parts of a phrase like '6 Days'. For example:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
     *  Period.of(3, SECONDS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
     *  Period.of(5, YEARS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
     * The specified unit must be one of the supported units from {@link ChronoUnit},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
     * {@code YEARS}, {@code MONTHS} or {@code DAYS} or be a time unit with an
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
     * {@linkplain TemporalUnit#isDurationEstimated() exact duration}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
     * Other units throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
     * @param amount  the amount of the period, measured in terms of the unit, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
     * @param unit  the unit that the period is measured in, must have an exact duration, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
     * @return the period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
     * @throws DateTimeException if the period unit is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
     * @throws ArithmeticException if a numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
    public static Period of(long amount, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
        return ZERO.plus(amount, unit);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
     * Obtains a {@code Period} from a {@code Duration}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
     * This converts the duration to a period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
     * Within a period, the time fields are always normalized.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
     * The years, months and days fields will be zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
     * To populate the days field, call {@link #normalizedHoursToDays()} on the created period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
     * @param duration  the duration to convert, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
     * @return the period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
    public static Period of(Duration duration) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
        Objects.requireNonNull(duration, "duration");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
        if (duration.isZero()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
            return ZERO;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
        return new Period(0, 0, 0, duration.toNanos());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
     * Returns a {@code Period} consisting of the number of years, months, days,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
     * hours, minutes, seconds, and nanoseconds between two {@code TemporalAccessor} instances.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
     * The start date is included, but the end date is not. Only whole years count.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
     * For example, from {@code 2010-01-15} to {@code 2011-03-18} is one year, two months and three days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
     * This method examines the {@link ChronoField fields} {@code YEAR}, {@code MONTH_OF_YEAR},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
     * {@code DAY_OF_MONTH} and {@code NANO_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
     * The difference between each of the fields is calculated independently from the others.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
     * At least one of the four fields must be present.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
     * The four units are typically retained without normalization.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
     * However, years and months are normalized if the range of months is fixed, as it is with ISO.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
     * The result of this method can be a negative period if the end is before the start.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
     * The negative sign can be different in each of the four major units.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
     * @param start  the start date, inclusive, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
     * @param end  the end date, exclusive, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
     * @return the period between the date-times, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
     * @throws DateTimeException if the two date-times do have similar available fields
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
    public static Period between(TemporalAccessor start, TemporalAccessor end) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
        if (Chrono.from(start).equals(Chrono.from(end)) == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
            throw new DateTimeException("Unable to calculate period as date-times have different chronologies");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   309
        int years = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
        int months = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
        int days = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
        long nanos = 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
        boolean valid = false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
        if (start.isSupported(YEAR)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
            years = Math.toIntExact(Math.subtractExact(end.getLong(YEAR), start.getLong(YEAR)));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
            valid = true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
        if (start.isSupported(MONTH_OF_YEAR)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
            months = Math.toIntExact(Math.subtractExact(end.getLong(MONTH_OF_YEAR), start.getLong(MONTH_OF_YEAR)));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
            ValueRange startRange = Chrono.from(start).range(MONTH_OF_YEAR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
            ValueRange endRange = Chrono.from(end).range(MONTH_OF_YEAR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
            if (startRange.isFixed() && startRange.isIntValue() && startRange.equals(endRange)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
                int monthCount = (int) (startRange.getMaximum() - startRange.getMinimum() + 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   324
                long totMonths = ((long) months) + years * monthCount;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
                months = (int) (totMonths % monthCount);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
                years = Math.toIntExact(totMonths / monthCount);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
            valid = true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   330
        if (start.isSupported(DAY_OF_MONTH)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
            days = Math.toIntExact(Math.subtractExact(end.getLong(DAY_OF_MONTH), start.getLong(DAY_OF_MONTH)));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
            valid = true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   333
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   334
        if (start.isSupported(NANO_OF_DAY)) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
            nanos = Math.subtractExact(end.getLong(NANO_OF_DAY), start.getLong(NANO_OF_DAY));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
            valid = true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
        if (valid == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
            throw new DateTimeException("Unable to calculate period as date-times do not have any valid fields");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
        return create(years, months, days, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   346
     * Obtains a {@code Period} consisting of the number of years, months,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   347
     * and days between two dates.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   348
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
     * The start date is included, but the end date is not.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   350
     * The period is calculated by removing complete months, then calculating
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   351
     * the remaining number of days, adjusting to ensure that both have the same sign.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   352
     * The number of months is then split into years and months based on a 12 month year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
     * A month is considered if the end day-of-month is greater than or equal to the start day-of-month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
     * For example, from {@code 2010-01-15} to {@code 2011-03-18} is one year, two months and three days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
     * The result of this method can be a negative period if the end is before the start.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
     * The negative sign will be the same in each of year, month and day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   359
     * @param startDate  the start date, inclusive, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   360
     * @param endDate  the end date, exclusive, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   361
     * @return the period between the dates, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   362
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
    public static Period betweenISO(LocalDate startDate, LocalDate endDate) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   365
        long startMonth = startDate.getLong(EPOCH_MONTH);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   366
        long endMonth = endDate.getLong(EPOCH_MONTH);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
        long totalMonths = endMonth - startMonth;  // safe
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   368
        int days = endDate.getDayOfMonth() - startDate.getDayOfMonth();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   369
        if (totalMonths > 0 && days < 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   370
            totalMonths--;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   371
            LocalDate calcDate = startDate.plusMonths(totalMonths);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   372
            days = (int) (endDate.toEpochDay() - calcDate.toEpochDay());  // safe
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   373
        } else if (totalMonths < 0 && days > 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   374
            totalMonths++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   375
            days -= endDate.lengthOfMonth();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   376
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   377
        long years = totalMonths / 12;  // safe
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
        int months = (int) (totalMonths % 12);  // safe
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
        return ofDate(Math.toIntExact(years), months, days);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
     * Obtains a {@code Period} consisting of the number of hours, minutes,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   385
     * seconds and nanoseconds between two times.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   386
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   387
     * The start time is included, but the end time is not.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
     * The period is calculated from the difference between the nano-of-day values
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
     * of the two times. For example, from {@code 13:45:00} to {@code 14:50:30.123456789}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   390
     * is {@code P1H5M30.123456789S}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   392
     * The result of this method can be a negative period if the end is before the start.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
     * @param startTime  the start time, inclusive, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
     * @param endTime  the end time, exclusive, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
     * @return the period between the times, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   398
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   399
    public static Period betweenISO(LocalTime startTime, LocalTime endTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   400
        return create(0, 0, 0, endTime.toNanoOfDay() - startTime.toNanoOfDay());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   403
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   405
     * Obtains a {@code Period} from a text string such as {@code PnYnMnDTnHnMn.nS}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
     * This will parse the string produced by {@code toString()} which is
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
     * a subset of the ISO-8601 period format {@code PnYnMnDTnHnMn.nS}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
     * The string consists of a series of numbers with a suffix identifying their meaning.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   411
     * The values, and suffixes, must be in the sequence year, month, day, hour, minute, second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
     * Any of the number/suffix pairs may be omitted providing at least one is present.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
     * If the period is zero, the value is normally represented as {@code PT0S}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
     * The numbers must consist of ASCII digits.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
     * Any of the numbers may be negative. Negative zero is not accepted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
     * The number of nanoseconds is expressed as an optional fraction of the seconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
     * There must be at least one digit before any decimal point.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
     * There must be between 1 and 9 inclusive digits after any decimal point.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
     * The letters will all be accepted in upper or lower case.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
     * The decimal point may be either a dot or a comma.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
     * @param text  the text to parse, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   423
     * @return the parsed period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
     * @throws DateTimeParseException if the text cannot be parsed to a period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
    public static Period parse(final CharSequence text) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
        Objects.requireNonNull(text, "text");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   428
        return new PeriodParser(text).parse();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   429
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   430
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   432
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   433
     * Creates an instance.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   434
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   435
     * @param years  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   436
     * @param months  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   437
     * @param days  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
     * @param nanos  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   439
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   440
    private static Period create(int years, int months, int days, long nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   441
        if ((years | months | days | nanos) == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
            return ZERO;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   443
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   444
        return new Period(years, months, days, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   445
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   446
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   448
     * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   450
     * @param years  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
     * @param months  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   452
     * @param days  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   453
     * @param nanos  the amount
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   455
    private Period(int years, int months, int days, long nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
        this.years = years;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   457
        this.months = months;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   458
        this.days = days;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
        this.nanos = nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   461
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   462
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   463
     * Resolves singletons.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   464
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   465
     * @return the resolved instance
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   466
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   467
    private Object readResolve() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   468
        if ((years | months | days | nanos) == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   469
            return ZERO;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   470
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   471
        return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   472
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   473
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   474
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   475
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   476
     * Checks if this period is zero-length.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   477
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   478
     * @return true if this period is zero-length
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   479
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   480
    public boolean isZero() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   481
        return (this == ZERO);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   482
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   483
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   484
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   485
     * Checks if this period is fully positive, excluding zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   486
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   487
     * This checks whether all the amounts in the period are positive,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   488
     * defined as greater than zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   489
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   490
     * @return true if this period is fully positive excluding zero
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   491
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   492
    public boolean isPositive() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   493
        return ((years | months | days | nanos) > 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   494
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   495
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   496
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   497
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   498
     * Gets the amount of years of this period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   499
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   500
     * @return the amount of years of this period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   501
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   502
    public int getYears() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   503
        return years;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   504
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   505
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   506
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   507
     * Gets the amount of months of this period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   508
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   509
     * @return the amount of months of this period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   510
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   511
    public int getMonths() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   512
        return months;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   513
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   514
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   515
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   516
     * Gets the amount of days of this period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   517
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   518
     * @return the amount of days of this period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   519
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   520
    public int getDays() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   521
        return days;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   522
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   523
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   524
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   525
     * Gets the amount of hours of this period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   526
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   527
     * Within a period, the time fields are always normalized.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   528
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   529
     * @return the amount of hours of this period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   530
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   531
    public int getHours() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   532
        return (int) (nanos / NANOS_PER_HOUR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   533
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   534
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   535
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   536
     * Gets the amount of minutes within an hour of this period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   537
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   538
     * Within a period, the time fields are always normalized.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   539
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   540
     * @return the amount of minutes within an hour of this period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   541
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   542
    public int getMinutes() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   543
        return (int) ((nanos / NANOS_PER_MINUTE) % 60);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   544
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   545
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   546
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   547
     * Gets the amount of seconds within a minute of this period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   548
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   549
     * Within a period, the time fields are always normalized.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   550
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   551
     * @return the amount of seconds within a minute of this period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   552
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   553
    public int getSeconds() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   554
        return (int) ((nanos / NANOS_PER_SECOND) % 60);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   555
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   556
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   557
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   558
     * Gets the amount of nanoseconds within a second of this period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   559
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   560
     * Within a period, the time fields are always normalized.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   561
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   562
     * @return the amount of nanoseconds within a second of this period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   563
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   564
    public int getNanos() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   565
        return (int) (nanos % NANOS_PER_SECOND);  // safe from overflow
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   566
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   567
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   568
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   569
     * Gets the total amount of the time units of this period, measured in nanoseconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   570
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   571
     * Within a period, the time fields are always normalized.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   572
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   573
     * @return the total amount of time unit nanoseconds of this period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   574
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   575
    public long getTimeNanos() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   576
        return nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   577
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   578
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   579
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   580
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   581
     * Returns a copy of this period with the specified amount of years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   582
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   583
     * This method will only affect the years field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   584
     * All other units are unaffected.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   585
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   586
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   587
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   588
     * @param years  the years to represent
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   589
     * @return a {@code Period} based on this period with the requested years, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   590
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   591
    public Period withYears(int years) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   592
        if (years == this.years) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   593
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   594
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   595
        return create(years, months, days, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   596
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   597
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   598
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   599
     * Returns a copy of this period with the specified amount of months.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   600
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   601
     * This method will only affect the months field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   602
     * All other units are unaffected.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   603
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   604
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   605
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   606
     * @param months  the months to represent
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   607
     * @return a {@code Period} based on this period with the requested months, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   608
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   609
    public Period withMonths(int months) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   610
        if (months == this.months) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   611
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   612
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   613
        return create(years, months, days, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   614
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   615
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   616
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   617
     * Returns a copy of this period with the specified amount of days.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   618
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   619
     * This method will only affect the days field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   620
     * All other units are unaffected.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   621
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   622
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   623
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   624
     * @param days  the days to represent
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   625
     * @return a {@code Period} based on this period with the requested days, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   626
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   627
    public Period withDays(int days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   628
        if (days == this.days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   629
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   630
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   631
        return create(years, months, days, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   632
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   633
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   634
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   635
     * Returns a copy of this period with the specified total amount of time units
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   636
     * expressed in nanoseconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   637
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   638
     * Within a period, the time fields are always normalized.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   639
     * This method will affect all the time units - hours, minutes, seconds and nanos.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   640
     * The date units are unaffected.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   641
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   642
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   643
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   644
     * @param nanos  the nanoseconds to represent
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   645
     * @return a {@code Period} based on this period with the requested nanoseconds, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   646
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   647
    public Period withTimeNanos(long nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   648
        if (nanos == this.nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   649
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   650
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   651
        return create(years, months, days, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   652
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   653
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   654
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   655
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   656
     * Returns a copy of this period with the specified period added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   657
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   658
     * This operates separately on the years, months, days and the normalized time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   659
     * There is no further normalization beyond the normalized time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   660
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   661
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   662
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   663
     * @param other  the period to add, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   664
     * @return a {@code Period} based on this period with the requested period added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   665
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   666
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   667
    public Period plus(Period other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   668
        return create(
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   669
                Math.addExact(years, other.years),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   670
                Math.addExact(months, other.months),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   671
                Math.addExact(days, other.days),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   672
                Math.addExact(nanos, other.nanos));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   673
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   674
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   675
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   676
     * Returns a copy of this period with the specified period added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   677
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   678
     * The specified unit must be one of the supported units from {@link ChronoUnit},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   679
     * {@code YEARS}, {@code MONTHS} or {@code DAYS} or be a time unit with an
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   680
     * {@linkplain TemporalUnit#isDurationEstimated() exact duration}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   681
     * Other units throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   682
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   683
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   684
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   685
     * @param amount  the amount to add, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   686
     * @param unit  the unit that the amount is expressed in, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   687
     * @return a {@code Period} based on this period with the requested amount added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   688
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   689
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   690
    public Period plus(long amount, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   691
        Objects.requireNonNull(unit, "unit");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   692
        if (unit instanceof ChronoUnit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   693
            if (unit == YEARS || unit == MONTHS || unit == DAYS || unit.isDurationEstimated() == false) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   694
                if (amount == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   695
                    return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   696
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   697
                switch((ChronoUnit) unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   698
                    case NANOS: return plusNanos(amount);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   699
                    case MICROS: return plusNanos(Math.multiplyExact(amount, 1000L));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   700
                    case MILLIS: return plusNanos(Math.multiplyExact(amount, 1000_000L));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   701
                    case SECONDS: return plusSeconds(amount);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   702
                    case MINUTES: return plusMinutes(amount);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   703
                    case HOURS: return plusHours(amount);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   704
                    case HALF_DAYS: return plusNanos(Math.multiplyExact(amount, 12 * NANOS_PER_HOUR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   705
                    case DAYS: return plusDays(amount);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   706
                    case MONTHS: return plusMonths(amount);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   707
                    case YEARS: return plusYears(amount);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   708
                    default: throw new DateTimeException("Unsupported unit: " + unit.getName());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   709
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   710
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   711
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   712
        if (unit.isDurationEstimated()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   713
            throw new DateTimeException("Unsupported unit: " + unit.getName());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   714
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   715
        return plusNanos(Duration.of(amount, unit).toNanos());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   716
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   717
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   718
    public Period plusYears(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   719
        return create(Math.toIntExact(Math.addExact(years, amount)), months, days, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   720
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   721
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   722
    public Period plusMonths(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   723
        return create(years, Math.toIntExact(Math.addExact(months, amount)), days, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   724
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   725
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   726
    public Period plusDays(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   727
        return create(years, months, Math.toIntExact(Math.addExact(days, amount)), nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   728
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   729
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   730
    public Period plusHours(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   731
        return plusNanos(Math.multiplyExact(amount, NANOS_PER_HOUR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   732
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   733
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   734
    public Period plusMinutes(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   735
        return plusNanos(Math.multiplyExact(amount, NANOS_PER_MINUTE));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   736
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   737
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   738
    public Period plusSeconds(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   739
        return plusNanos(Math.multiplyExact(amount, NANOS_PER_SECOND));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   740
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   741
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   742
    public Period plusNanos(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   743
        return create(years, months, days, Math.addExact(nanos,  amount));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   744
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   745
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   746
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   747
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   748
     * Returns a copy of this period with the specified period subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   749
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   750
     * This operates separately on the years, months, days and the normalized time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   751
     * There is no further normalization beyond the normalized time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   752
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   753
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   754
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   755
     * @param other  the period to subtract, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   756
     * @return a {@code Period} based on this period with the requested period subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   757
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   758
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   759
    public Period minus(Period other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   760
        return create(
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   761
                Math.subtractExact(years, other.years),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   762
                Math.subtractExact(months, other.months),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   763
                Math.subtractExact(days, other.days),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   764
                Math.subtractExact(nanos, other.nanos));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   765
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   766
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   767
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   768
     * Returns a copy of this period with the specified period subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   769
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   770
     * The specified unit must be one of the supported units from {@link ChronoUnit},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   771
     * {@code YEARS}, {@code MONTHS} or {@code DAYS} or be a time unit with an
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   772
     * {@linkplain TemporalUnit#isDurationEstimated() exact duration}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   773
     * Other units throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   774
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   775
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   776
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   777
     * @param amount  the amount to subtract, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   778
     * @param unit  the unit that the amount is expressed in, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   779
     * @return a {@code Period} based on this period with the requested amount subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   780
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   781
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   782
    public Period minus(long amount, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   783
        return (amount == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amount, unit));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   784
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   785
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   786
    public Period minusYears(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   787
        return (amount == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-amount));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   788
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   789
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   790
    public Period minusMonths(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   791
        return (amount == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-amount));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   792
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   793
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   794
    public Period minusDays(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   795
        return (amount == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-amount));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   796
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   797
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   798
    public Period minusHours(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   799
        return (amount == Long.MIN_VALUE ? plusHours(Long.MAX_VALUE).plusHours(1) : plusHours(-amount));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   800
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   801
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   802
    public Period minusMinutes(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   803
        return (amount == Long.MIN_VALUE ? plusMinutes(Long.MAX_VALUE).plusMinutes(1) : plusMinutes(-amount));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   804
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   805
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   806
    public Period minusSeconds(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   807
        return (amount == Long.MIN_VALUE ? plusSeconds(Long.MAX_VALUE).plusSeconds(1) : plusSeconds(-amount));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   808
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   809
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   810
    public Period minusNanos(long amount) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   811
        return (amount == Long.MIN_VALUE ? plusNanos(Long.MAX_VALUE).plusNanos(1) : plusNanos(-amount));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   812
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   813
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   814
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   815
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   816
     * Returns a new instance with each element in this period multiplied
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   817
     * by the specified scalar.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   818
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   819
     * This simply multiplies each field, years, months, days and normalized time,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   820
     * by the scalar. No normalization is performed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   821
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   822
     * @param scalar  the scalar to multiply by, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   823
     * @return a {@code Period} based on this period with the amounts multiplied by the scalar, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   824
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   825
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   826
    public Period multipliedBy(int scalar) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   827
        if (this == ZERO || scalar == 1) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   828
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   829
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   830
        return create(
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   831
                Math.multiplyExact(years, scalar),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   832
                Math.multiplyExact(months, scalar),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   833
                Math.multiplyExact(days, scalar),
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   834
                Math.multiplyExact(nanos, scalar));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   835
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   836
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   837
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   838
     * Returns a new instance with each amount in this period negated.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   839
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   840
     * @return a {@code Period} based on this period with the amounts negated, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   841
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   842
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   843
    public Period negated() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   844
        return multipliedBy(-1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   845
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   846
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   847
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   848
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   849
     * Returns a copy of this period with the days and hours normalized using a 24 hour day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   850
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   851
     * This normalizes the days and hours units, leaving years and months unchanged.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   852
     * The hours unit is adjusted to have an absolute value less than 23,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   853
     * with the days unit being adjusted to compensate.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   854
     * For example, a period of {@code P1DT27H} will be normalized to {@code P2DT3H}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   855
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   856
     * The sign of the days and hours units will be the same after normalization.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   857
     * For example, a period of {@code P1DT-51H} will be normalized to {@code P-1DT-3H}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   858
     * Since all time units are always normalized, if the hours units changes sign then
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   859
     * other time units will also be affected.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   860
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   861
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   862
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   863
     * @return a {@code Period} based on this period with excess hours normalized to days, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   864
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   865
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   866
    public Period normalizedHoursToDays() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   867
        // logic uses if statements to normalize signs to avoid unnecessary overflows
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   868
        long totalDays = (nanos / NANOS_PER_DAY) + days;  // no overflow
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   869
        long splitNanos = nanos % NANOS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   870
        if (totalDays > 0 && splitNanos < 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   871
            splitNanos += NANOS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   872
            totalDays--;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   873
        } else if (totalDays < 0 && splitNanos > 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   874
            splitNanos -= NANOS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   875
            totalDays++;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   876
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   877
        if (totalDays == days && splitNanos == nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   878
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   879
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   880
        return create(years, months, Math.toIntExact(totalDays), splitNanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   881
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   882
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   883
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   884
     * Returns a copy of this period with any days converted to hours using a 24 hour day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   885
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   886
     * The days unit is reduced to zero, with the hours unit increased by 24 times the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   887
     * days unit to compensate. Other units are unaffected.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   888
     * For example, a period of {@code P2DT4H} will be normalized to {@code PT52H}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   889
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   890
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   891
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   892
     * @return a {@code Period} based on this period with days normalized to hours, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   893
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   894
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   895
    public Period normalizedDaysToHours() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   896
        if (days == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   897
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   898
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   899
        return create(years, months, 0, Math.addExact(Math.multiplyExact(days, NANOS_PER_DAY), nanos));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   900
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   901
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   902
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   903
     * Returns a copy of this period with the years and months normalized using a 12 month year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   904
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   905
     * This normalizes the years and months units, leaving other units unchanged.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   906
     * The months unit is adjusted to have an absolute value less than 11,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   907
     * with the years unit being adjusted to compensate.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   908
     * For example, a period of {@code P1Y15M} will be normalized to {@code P2Y3M}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   909
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   910
     * The sign of the years and months units will be the same after normalization.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   911
     * For example, a period of {@code P1Y-25M} will be normalized to {@code P-1Y-1M}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   912
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   913
     * This normalization uses a 12 month year it is not valid for all calendar systems.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   914
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   915
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   916
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   917
     * @return a {@code Period} based on this period with years and months normalized, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   918
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   919
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   920
    public Period normalizedMonthsISO() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   921
        long totalMonths = years * 12L + months;  // no overflow
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   922
        long splitYears = totalMonths / 12;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   923
        int splitMonths = (int) (totalMonths % 12);  // no overflow
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   924
        if (splitYears == years && splitMonths == months) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   925
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   926
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   927
        return create(Math.toIntExact(splitYears), splitMonths, days, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   928
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   929
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   930
    //-------------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   931
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   932
     * Converts this period to one that only has date units.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   933
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   934
     * The resulting period will have the same years, months and days as this period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   935
     * but the time units will all be zero. No normalization occurs in the calculation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   936
     * For example, a period of {@code P1Y3MT12H} will be converted to {@code P1Y3M}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   937
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   938
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   939
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   940
     * @return a {@code Period} based on this period with the time units set to zero, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   941
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   942
    public Period toDateOnly() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   943
        if (nanos == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   944
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   945
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   946
        return create(years, months, days, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   947
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   948
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   949
    //-------------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   950
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   951
     * Adds this period to the specified temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   952
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   953
     * This returns a temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   954
     * with this period added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   955
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   956
     * In most cases, it is clearer to reverse the calling pattern by using
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   957
     * {@link Temporal#plus(TemporalAdder)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   958
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   959
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   960
     *   dateTime = thisPeriod.addTo(dateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   961
     *   dateTime = dateTime.plus(thisPeriod);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   962
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   963
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   964
     * The calculation will add the years, then months, then days, then nanos.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   965
     * Only non-zero amounts will be added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   966
     * If the date-time has a calendar system with a fixed number of months in a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   967
     * year, then the years and months will be combined before being added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   968
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   969
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   970
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   971
     * @param temporal  the temporal object to adjust, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   972
     * @return an object of the same type with the adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   973
     * @throws DateTimeException if unable to add
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   974
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   975
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   976
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   977
    public Temporal addTo(Temporal temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   978
        Objects.requireNonNull(temporal, "temporal");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   979
        if ((years | months) != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   980
            ValueRange startRange = Chrono.from(temporal).range(MONTH_OF_YEAR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   981
            if (startRange.isFixed() && startRange.isIntValue()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   982
                long monthCount = startRange.getMaximum() - startRange.getMinimum() + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   983
                temporal = temporal.plus(years * monthCount + months, MONTHS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   984
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   985
                if (years != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   986
                    temporal = temporal.plus(years, YEARS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   987
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   988
                if (months != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   989
                    temporal = temporal.plus(months, MONTHS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   990
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   991
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   992
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   993
        if (days != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   994
            temporal = temporal.plus(days, DAYS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   995
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   996
        if (nanos != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   997
            temporal = temporal.plus(nanos, NANOS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   998
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   999
        return temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1000
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1001
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1002
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1003
     * Subtracts this period from the specified temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1004
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1005
     * This returns a temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1006
     * with this period subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1007
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1008
     * In most cases, it is clearer to reverse the calling pattern by using
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1009
     * {@link Temporal#minus(TemporalSubtractor)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1010
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1011
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1012
     *   dateTime = thisPeriod.subtractFrom(dateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1013
     *   dateTime = dateTime.minus(thisPeriod);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1014
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1015
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1016
     * The calculation will subtract the years, then months, then days, then nanos.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1017
     * Only non-zero amounts will be subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1018
     * If the date-time has a calendar system with a fixed number of months in a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1019
     * year, then the years and months will be combined before being subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1020
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1021
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1022
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1023
     * @param temporal  the temporal object to adjust, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1024
     * @return an object of the same type with the adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1025
     * @throws DateTimeException if unable to subtract
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1026
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1027
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1028
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1029
    public Temporal subtractFrom(Temporal temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1030
        Objects.requireNonNull(temporal, "temporal");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1031
        if ((years | months) != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1032
            ValueRange startRange = Chrono.from(temporal).range(MONTH_OF_YEAR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1033
            if (startRange.isFixed() && startRange.isIntValue()) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1034
                long monthCount = startRange.getMaximum() - startRange.getMinimum() + 1;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1035
                temporal = temporal.minus(years * monthCount + months, MONTHS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1036
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1037
                if (years != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1038
                    temporal = temporal.minus(years, YEARS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1039
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1040
                if (months != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1041
                    temporal = temporal.minus(months, MONTHS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1042
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1043
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1044
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1045
        if (days != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1046
            temporal = temporal.minus(days, DAYS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1047
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1048
        if (nanos != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1049
            temporal = temporal.minus(nanos, NANOS);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1050
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1051
        return temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1052
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1053
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1054
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1055
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1056
     * Converts this period to one that only has time units.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1057
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1058
     * The resulting period will have the same time units as this period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1059
     * but the date units will all be zero. No normalization occurs in the calculation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1060
     * For example, a period of {@code P1Y3MT12H} will be converted to {@code PT12H}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1061
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1062
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1063
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1064
     * @return a {@code Period} based on this period with the date units set to zero, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1065
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1066
    public Period toTimeOnly() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1067
        if ((years | months | days) == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1068
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1069
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1070
        return create(0, 0, 0, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1071
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1072
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1073
    //-------------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1074
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1075
     * Calculates the duration of this period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1076
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1077
     * The calculation uses the hours, minutes, seconds and nanoseconds fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1078
     * If years, months or days are present an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1079
     * See {@link #toTimeOnly()} for a way to remove the date units and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1080
     * {@link #normalizedDaysToHours()} for a way to convert days to hours.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1081
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1082
     * @return a {@code Duration} equivalent to this period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1083
     * @throws DateTimeException if the period cannot be converted as it contains years, months or days
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1084
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1085
    public Duration toDuration() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1086
        if ((years | months | days) != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1087
            throw new DateTimeException("Unable to convert period to duration as years/months/days are present: " + this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1088
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1089
        return Duration.ofNanos(nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1090
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1091
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1092
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1093
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1094
     * Checks if this period is equal to another period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1095
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1096
     * The comparison is based on the amounts held in the period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1097
     * To be equal, the years, months, days and normalized time fields must be equal.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1098
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1099
     * @param obj  the object to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1100
     * @return true if this is equal to the other period
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1101
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1102
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1103
    public boolean equals(Object obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1104
        if (this == obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1105
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1106
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1107
        if (obj instanceof Period) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1108
            Period other = (Period) obj;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1109
            return years == other.years && months == other.months &&
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1110
                    days == other.days && nanos == other.nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1111
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1112
        return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1113
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1114
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1115
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1116
     * A hash code for this period.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1117
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1118
     * @return a suitable hash code
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1119
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1120
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1121
    public int hashCode() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1122
        // ordered such that overflow from one field doesn't immediately affect the next field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1123
        return ((years << 27) | (years >>> 5)) ^
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1124
                ((days << 21) | (days >>> 11)) ^
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1125
                ((months << 17) | (months >>> 15)) ^
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1126
                ((int) (nanos ^ (nanos >>> 32)));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1127
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1128
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1129
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1130
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1131
     * Outputs this period as a {@code String}, such as {@code P6Y3M1DT12H}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1132
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1133
     * The output will be in the ISO-8601 period format.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1134
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1135
     * @return a string representation of this period, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1136
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1137
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1138
    public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1139
        if (this == ZERO) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1140
            return "PT0S";
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1141
        } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1142
            StringBuilder buf = new StringBuilder();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1143
            buf.append('P');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1144
            if (years != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1145
                buf.append(years).append('Y');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1146
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1147
            if (months != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1148
                buf.append(months).append('M');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1149
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1150
            if (days != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1151
                buf.append(days).append('D');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1152
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1153
            if (nanos != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1154
                buf.append('T');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1155
                if (getHours() != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1156
                    buf.append(getHours()).append('H');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1157
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1158
                if (getMinutes() != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1159
                    buf.append(getMinutes()).append('M');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1160
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1161
                int secondPart = getSeconds();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1162
                int nanoPart = getNanos();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1163
                int secsNanosOr = secondPart | nanoPart;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1164
                if (secsNanosOr != 0) {  // if either non-zero
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1165
                    if ((secsNanosOr & Integer.MIN_VALUE) != 0) {  // if either less than zero
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1166
                        buf.append('-');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1167
                        secondPart = Math.abs(secondPart);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1168
                        nanoPart = Math.abs(nanoPart);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1169
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1170
                    buf.append(secondPart);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1171
                    if (nanoPart != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1172
                        int dotPos = buf.length();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1173
                        nanoPart += 1000_000_000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1174
                        while (nanoPart % 10 == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1175
                            nanoPart /= 10;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1176
                        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1177
                        buf.append(nanoPart);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1178
                        buf.setCharAt(dotPos, '.');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1179
                    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1180
                    buf.append('S');
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1181
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1182
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1183
            return buf.toString();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1184
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1185
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1186
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1187
}