66 import static java.time.temporal.ChronoUnit.YEARS;
67
68 import java.io.DataInput;
69 import java.io.DataOutput;
70 import java.io.IOException;
71 import java.io.InvalidObjectException;
72 import java.io.ObjectInputStream;
73 import java.io.Serializable;
74 import java.time.chrono.ChronoLocalDate;
75 import java.time.chrono.ChronoPeriod;
76 import java.time.chrono.Chronology;
77 import java.time.chrono.IsoChronology;
78 import java.time.format.DateTimeParseException;
79 import java.time.temporal.ChronoUnit;
80 import java.time.temporal.Temporal;
81 import java.time.temporal.TemporalAccessor;
82 import java.time.temporal.TemporalAmount;
83 import java.time.temporal.TemporalQueries;
84 import java.time.temporal.TemporalUnit;
85 import java.time.temporal.UnsupportedTemporalTypeException;
86 import java.util.Arrays;
87 import java.util.Collections;
88 import java.util.List;
89 import java.util.Objects;
90 import java.util.regex.Matcher;
91 import java.util.regex.Pattern;
92
93 /**
94 * A date-based amount of time in the ISO-8601 calendar system,
95 * such as '2 years, 3 months and 4 days'.
96 * <p>
97 * This class models a quantity or amount of time in terms of years, months and days.
98 * See {@link Duration} for the time-based equivalent to this class.
99 * <p>
100 * Durations and periods differ in their treatment of daylight savings time
101 * when added to {@link ZonedDateTime}. A {@code Duration} will add an exact
102 * number of seconds, thus a duration of one day is always exactly 24 hours.
103 * By contrast, a {@code Period} will add a conceptual day, trying to maintain
104 * the local time.
105 * <p>
106 * For example, consider adding a period of one day and a duration of one day to
107 * 18:00 on the evening before a daylight savings gap. The {@code Period} will add
135 public final class Period
136 implements ChronoPeriod, Serializable {
137
138 /**
139 * A constant for a period of zero.
140 */
141 public static final Period ZERO = new Period(0, 0, 0);
142 /**
143 * Serialization version.
144 */
145 private static final long serialVersionUID = -3587258372562876L;
146 /**
147 * The pattern for parsing.
148 */
149 private static final Pattern PATTERN =
150 Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)Y)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)W)?(?:([-+]?[0-9]+)D)?", Pattern.CASE_INSENSITIVE);
151
152 /**
153 * The set of supported units.
154 */
155 private static final List<TemporalUnit> SUPPORTED_UNITS =
156 Collections.unmodifiableList(Arrays.<TemporalUnit>asList(YEARS, MONTHS, DAYS));
157
158 /**
159 * The number of years.
160 */
161 private final int years;
162 /**
163 * The number of months.
164 */
165 private final int months;
166 /**
167 * The number of days.
168 */
169 private final int days;
170
171 //-----------------------------------------------------------------------
172 /**
173 * Obtains a {@code Period} representing a number of years.
174 * <p>
175 * The resulting period will have the specified years.
176 * The months and days units will be zero.
|
66 import static java.time.temporal.ChronoUnit.YEARS;
67
68 import java.io.DataInput;
69 import java.io.DataOutput;
70 import java.io.IOException;
71 import java.io.InvalidObjectException;
72 import java.io.ObjectInputStream;
73 import java.io.Serializable;
74 import java.time.chrono.ChronoLocalDate;
75 import java.time.chrono.ChronoPeriod;
76 import java.time.chrono.Chronology;
77 import java.time.chrono.IsoChronology;
78 import java.time.format.DateTimeParseException;
79 import java.time.temporal.ChronoUnit;
80 import java.time.temporal.Temporal;
81 import java.time.temporal.TemporalAccessor;
82 import java.time.temporal.TemporalAmount;
83 import java.time.temporal.TemporalQueries;
84 import java.time.temporal.TemporalUnit;
85 import java.time.temporal.UnsupportedTemporalTypeException;
86 import java.util.List;
87 import java.util.Objects;
88 import java.util.regex.Matcher;
89 import java.util.regex.Pattern;
90
91 /**
92 * A date-based amount of time in the ISO-8601 calendar system,
93 * such as '2 years, 3 months and 4 days'.
94 * <p>
95 * This class models a quantity or amount of time in terms of years, months and days.
96 * See {@link Duration} for the time-based equivalent to this class.
97 * <p>
98 * Durations and periods differ in their treatment of daylight savings time
99 * when added to {@link ZonedDateTime}. A {@code Duration} will add an exact
100 * number of seconds, thus a duration of one day is always exactly 24 hours.
101 * By contrast, a {@code Period} will add a conceptual day, trying to maintain
102 * the local time.
103 * <p>
104 * For example, consider adding a period of one day and a duration of one day to
105 * 18:00 on the evening before a daylight savings gap. The {@code Period} will add
133 public final class Period
134 implements ChronoPeriod, Serializable {
135
136 /**
137 * A constant for a period of zero.
138 */
139 public static final Period ZERO = new Period(0, 0, 0);
140 /**
141 * Serialization version.
142 */
143 private static final long serialVersionUID = -3587258372562876L;
144 /**
145 * The pattern for parsing.
146 */
147 private static final Pattern PATTERN =
148 Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)Y)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)W)?(?:([-+]?[0-9]+)D)?", Pattern.CASE_INSENSITIVE);
149
150 /**
151 * The set of supported units.
152 */
153 private static final List<TemporalUnit> SUPPORTED_UNITS = List.of(YEARS, MONTHS, DAYS);
154
155 /**
156 * The number of years.
157 */
158 private final int years;
159 /**
160 * The number of months.
161 */
162 private final int months;
163 /**
164 * The number of days.
165 */
166 private final int days;
167
168 //-----------------------------------------------------------------------
169 /**
170 * Obtains a {@code Period} representing a number of years.
171 * <p>
172 * The resulting period will have the specified years.
173 * The months and days units will be zero.
|