< prev index next >

src/java.base/share/classes/java/time/Period.java

Print this page

 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.

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 }

 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, mutexes, 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, mutexes, 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 @jdk.internal.MigratedValueClass
 142 public final class Period
 143         implements ChronoPeriod, Serializable {
 144 
 145     /**
 146      * A constant for a period of zero.
 147      */
 148     public static final Period ZERO = new Period(0, 0, 0);
 149     /**
 150      * Serialization version.
 151      */
 152     @java.io.Serial
 153     private static final long serialVersionUID = -3587258372562876L;
 154     /**
 155      * The pattern for parsing.
 156      */
 157     private static final Pattern PATTERN =
 158             Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)Y)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)W)?(?:([-+]?[0-9]+)D)?", Pattern.CASE_INSENSITIVE);
 159 
 160     /**
 161      * The set of supported units.

1054      *  out.writeByte(14);  // identifies a Period
1055      *  out.writeInt(years);
1056      *  out.writeInt(months);
1057      *  out.writeInt(days);
1058      * </pre>
1059      *
1060      * @return the instance of {@code Ser}, not null
1061      */
1062     @java.io.Serial
1063     private Object writeReplace() {
1064         return new Ser(Ser.PERIOD_TYPE, this);
1065     }
1066 
1067     /**
1068      * Defend against malicious streams.
1069      *
1070      * @param s the stream to read
1071      * @throws java.io.InvalidObjectException always
1072      */
1073     @java.io.Serial
1074     @SuppressWarnings("serial") // this method is not invoked for value classes
1075     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1076         throw new InvalidObjectException("Deserialization via serialization delegate");
1077     }
1078 
1079     void writeExternal(DataOutput out) throws IOException {
1080         out.writeInt(years);
1081         out.writeInt(months);
1082         out.writeInt(days);
1083     }
1084 
1085     static Period readExternal(DataInput in) throws IOException {
1086         int years = in.readInt();
1087         int months = in.readInt();
1088         int days = in.readInt();
1089         return Period.of(years, months, days);
1090     }
1091 
1092 }
< prev index next >