< prev index next >

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

Print this page




 123  * class; use of identity-sensitive operations (including reference equality
 124  * ({@code ==}), identity hash code, or synchronization) on instances of
 125  * {@code Period} may have unpredictable results and should be avoided.
 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 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;


1035                 buf.append(days).append('D');
1036             }
1037             return buf.toString();
1038         }
1039     }
1040 
1041     //-----------------------------------------------------------------------
1042     /**
1043      * Writes the object using a
1044      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1045      * @serialData
1046      * <pre>
1047      *  out.writeByte(14);  // identifies a Period
1048      *  out.writeInt(years);
1049      *  out.writeInt(months);
1050      *  out.writeInt(days);
1051      * </pre>
1052      *
1053      * @return the instance of {@code Ser}, not null
1054      */

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     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1066         throw new InvalidObjectException("Deserialization via serialization delegate");
1067     }
1068 
1069     void writeExternal(DataOutput out) throws IOException {
1070         out.writeInt(years);
1071         out.writeInt(months);
1072         out.writeInt(days);
1073     }
1074 
1075     static Period readExternal(DataInput in) throws IOException {
1076         int years = in.readInt();
1077         int months = in.readInt();
1078         int days = in.readInt();
1079         return Period.of(years, months, days);
1080     }
1081 
1082 }


 123  * class; use of identity-sensitive operations (including reference equality
 124  * ({@code ==}), identity hash code, or synchronization) on instances of
 125  * {@code Period} may have unpredictable results and should be avoided.
 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 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     @java.io.Serial
 144     private static final long serialVersionUID = -3587258372562876L;
 145     /**
 146      * The pattern for parsing.
 147      */
 148     private static final Pattern PATTERN =
 149             Pattern.compile("([-+]?)P(?:([-+]?[0-9]+)Y)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)W)?(?:([-+]?[0-9]+)D)?", Pattern.CASE_INSENSITIVE);
 150 
 151     /**
 152      * The set of supported units.
 153      */
 154     private static final List<TemporalUnit> SUPPORTED_UNITS = List.of(YEARS, MONTHS, DAYS);
 155 
 156     /**
 157      * The number of years.
 158      */
 159     private final int years;
 160     /**
 161      * The number of months.
 162      */
 163     private final int months;


1036                 buf.append(days).append('D');
1037             }
1038             return buf.toString();
1039         }
1040     }
1041 
1042     //-----------------------------------------------------------------------
1043     /**
1044      * Writes the object using a
1045      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1046      * @serialData
1047      * <pre>
1048      *  out.writeByte(14);  // identifies a Period
1049      *  out.writeInt(years);
1050      *  out.writeInt(months);
1051      *  out.writeInt(days);
1052      * </pre>
1053      *
1054      * @return the instance of {@code Ser}, not null
1055      */
1056     @java.io.Serial
1057     private Object writeReplace() {
1058         return new Ser(Ser.PERIOD_TYPE, this);
1059     }
1060 
1061     /**
1062      * Defend against malicious streams.
1063      *
1064      * @param s the stream to read
1065      * @throws java.io.InvalidObjectException always
1066      */
1067     @java.io.Serial
1068     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1069         throw new InvalidObjectException("Deserialization via serialization delegate");
1070     }
1071 
1072     void writeExternal(DataOutput out) throws IOException {
1073         out.writeInt(years);
1074         out.writeInt(months);
1075         out.writeInt(days);
1076     }
1077 
1078     static Period readExternal(DataInput in) throws IOException {
1079         int years = in.readInt();
1080         int months = in.readInt();
1081         int days = in.readInt();
1082         return Period.of(years, months, days);
1083     }
1084 
1085 }
< prev index next >