1 /*
2 * Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
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
106 * the conceptual day and result in a {@code ZonedDateTime} at 18:00 the following day.
107 * By contrast, the {@code Duration} will add exactly 24 hours, resulting in a
108 * {@code ZonedDateTime} at 19:00 the following day (assuming a one hour DST gap).
109 * <p>
110 * The supported units of a period are {@link ChronoUnit#YEARS YEARS},
111 * {@link ChronoUnit#MONTHS MONTHS} and {@link ChronoUnit#DAYS DAYS}.
112 * All three fields are always present, but may be set to zero.
113 * <p>
114 * The ISO-8601 calendar system is the modern civil calendar system used today
115 * in most of the world. It is equivalent to the proleptic Gregorian calendar
116 * system, in which today's rules for leap years are applied for all time.
117 * <p>
118 * The period is modeled as a directed amount of time, meaning that individual parts of the
119 * period may be negative.
120 * <p>
121 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
122 * class; programmers should treat instances that are
123 * {@linkplain #equals(Object) equal} as interchangeable and should not
124 * use instances for synchronization, or unpredictable behavior may
125 * occur. For example, in a future release, synchronization may fail.
126 * The {@code equals} method should be used for comparisons.
127 *
128 * @implSpec
129 * This class is immutable and thread-safe.
130 *
131 * @since 1.8
132 */
133 @jdk.internal.ValueBased
134 public final class Period
135 implements ChronoPeriod, Serializable {
136
137 /**
138 * A constant for a period of zero.
139 */
140 public static final Period ZERO = new Period(0, 0, 0);
141 /**
142 * Serialization version.
143 */
144 @java.io.Serial
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 */
1046 * out.writeByte(14); // identifies a Period
1047 * out.writeInt(years);
1048 * out.writeInt(months);
1049 * out.writeInt(days);
1050 * </pre>
1051 *
1052 * @return the instance of {@code Ser}, not null
1053 */
1054 @java.io.Serial
1055 private Object writeReplace() {
1056 return new Ser(Ser.PERIOD_TYPE, this);
1057 }
1058
1059 /**
1060 * Defend against malicious streams.
1061 *
1062 * @param s the stream to read
1063 * @throws java.io.InvalidObjectException always
1064 */
1065 @java.io.Serial
1066 private void readObject(ObjectInputStream s) throws InvalidObjectException {
1067 throw new InvalidObjectException("Deserialization via serialization delegate");
1068 }
1069
1070 void writeExternal(DataOutput out) throws IOException {
1071 out.writeInt(years);
1072 out.writeInt(months);
1073 out.writeInt(days);
1074 }
1075
1076 static Period readExternal(DataInput in) throws IOException {
1077 int years = in.readInt();
1078 int months = in.readInt();
1079 int days = in.readInt();
1080 return Period.of(years, months, days);
1081 }
1082
1083 }
|
1 /*
2 * Copyright (c) 2012, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
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
106 * the conceptual day and result in a {@code ZonedDateTime} at 18:00 the following day.
107 * By contrast, the {@code Duration} will add exactly 24 hours, resulting in a
108 * {@code ZonedDateTime} at 19:00 the following day (assuming a one hour DST gap).
109 * <p>
110 * The supported units of a period are {@link ChronoUnit#YEARS YEARS},
111 * {@link ChronoUnit#MONTHS MONTHS} and {@link ChronoUnit#DAYS DAYS}.
112 * All three fields are always present, but may be set to zero.
113 * <p>
114 * The ISO-8601 calendar system is the modern civil calendar system used today
115 * in most of the world. It is equivalent to the proleptic Gregorian calendar
116 * system, in which today's rules for leap years are applied for all time.
117 * <p>
118 * The period is modeled as a directed amount of time, meaning that individual parts of the
119 * period may be negative.
120 * <p>
121 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
122 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
123 * as interchangeable and should not use instances for synchronization or
124 * with {@linkplain java.lang.ref.Reference object references}.
125 *
126 * <div class="preview-block">
127 * <div class="preview-comment">
128 * When preview features are enabled, {@code Period} is a {@linkplain Class#isValue value class}.
129 * Use of value class instances for synchronization or with
130 * {@linkplain java.lang.ref.Reference object references} result in
131 * {@link IdentityException}.
132 * </div>
133 * </div>
134 *
135 * @implSpec
136 * This class is immutable and thread-safe.
137 *
138 * @since 1.8
139 */
140 @jdk.internal.ValueBased
141 public final /*value*/ class Period
142 implements ChronoPeriod, Serializable {
143
144 /**
145 * A constant for a period of zero.
146 */
147 public static final Period ZERO = new Period(0, 0, 0);
148 /**
149 * Serialization version.
150 */
151 @java.io.Serial
152 private static final long serialVersionUID = -3587258372562876L;
153 /**
154 * The pattern for parsing.
155 */
156 private static final Pattern PATTERN =
157 Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)Y)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)W)?(?:([-+]?[0-9]+)D)?", Pattern.CASE_INSENSITIVE);
158
159 /**
160 * The set of supported units.
161 */
1053 * out.writeByte(14); // identifies a Period
1054 * out.writeInt(years);
1055 * out.writeInt(months);
1056 * out.writeInt(days);
1057 * </pre>
1058 *
1059 * @return the instance of {@code Ser}, not null
1060 */
1061 @java.io.Serial
1062 private Object writeReplace() {
1063 return new Ser(Ser.PERIOD_TYPE, this);
1064 }
1065
1066 /**
1067 * Defend against malicious streams.
1068 *
1069 * @param s the stream to read
1070 * @throws java.io.InvalidObjectException always
1071 */
1072 @java.io.Serial
1073 @SuppressWarnings("serial") // this method is not invoked for value classes
1074 private void readObject(ObjectInputStream s) throws InvalidObjectException {
1075 throw new InvalidObjectException("Deserialization via serialization delegate");
1076 }
1077
1078 void writeExternal(DataOutput out) throws IOException {
1079 out.writeInt(years);
1080 out.writeInt(months);
1081 out.writeInt(days);
1082 }
1083
1084 static Period readExternal(DataInput in) throws IOException {
1085 int years = in.readInt();
1086 int months = in.readInt();
1087 int days = in.readInt();
1088 return Period.of(years, months, days);
1089 }
1090
1091 }
|