< prev index next >

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

Print this page




 228      * which affect the instant in addition to the local date-time.
 229      * The value is also chosen such that the value of the year fits in
 230      * an {@code int}.
 231      */
 232     public static final Instant MIN = Instant.ofEpochSecond(MIN_SECOND, 0);
 233     /**
 234      * The maximum supported {@code Instant}, '1000000000-12-31T23:59:59.999999999Z'.
 235      * This could be used by an application as a "far future" instant.
 236      * <p>
 237      * This is one year later than the maximum {@code LocalDateTime}.
 238      * This provides sufficient values to handle the range of {@code ZoneOffset}
 239      * which affect the instant in addition to the local date-time.
 240      * The value is also chosen such that the value of the year fits in
 241      * an {@code int}.
 242      */
 243     public static final Instant MAX = Instant.ofEpochSecond(MAX_SECOND, 999_999_999);
 244 
 245     /**
 246      * Serialization version.
 247      */

 248     private static final long serialVersionUID = -665713676816604388L;
 249 
 250     /**
 251      * The number of seconds from the epoch of 1970-01-01T00:00:00Z.
 252      */
 253     private final long seconds;
 254     /**
 255      * The number of nanoseconds, later along the time-line, from the seconds field.
 256      * This is always positive, and never exceeds 999,999,999.
 257      */
 258     private final int nanos;
 259 
 260     //-----------------------------------------------------------------------
 261     /**
 262      * Obtains the current instant from the system clock.
 263      * <p>
 264      * This will query the {@link Clock#systemUTC() system UTC clock} to
 265      * obtain the current instant.
 266      * <p>
 267      * Using this method will prevent the ability to use an alternate time-source for


1325      * @return an ISO-8601 representation of this instant, not null
1326      */
1327     @Override
1328     public String toString() {
1329         return DateTimeFormatter.ISO_INSTANT.format(this);
1330     }
1331 
1332     // -----------------------------------------------------------------------
1333     /**
1334      * Writes the object using a
1335      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1336      * @serialData
1337      * <pre>
1338      *  out.writeByte(2);  // identifies an Instant
1339      *  out.writeLong(seconds);
1340      *  out.writeInt(nanos);
1341      * </pre>
1342      *
1343      * @return the instance of {@code Ser}, not null
1344      */

1345     private Object writeReplace() {
1346         return new Ser(Ser.INSTANT_TYPE, this);
1347     }
1348 
1349     /**
1350      * Defend against malicious streams.
1351      *
1352      * @param s the stream to read
1353      * @throws InvalidObjectException always
1354      */

1355     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1356         throw new InvalidObjectException("Deserialization via serialization delegate");
1357     }
1358 
1359     void writeExternal(DataOutput out) throws IOException {
1360         out.writeLong(seconds);
1361         out.writeInt(nanos);
1362     }
1363 
1364     static Instant readExternal(DataInput in) throws IOException {
1365         long seconds = in.readLong();
1366         int nanos = in.readInt();
1367         return Instant.ofEpochSecond(seconds, nanos);
1368     }
1369 
1370 }


 228      * which affect the instant in addition to the local date-time.
 229      * The value is also chosen such that the value of the year fits in
 230      * an {@code int}.
 231      */
 232     public static final Instant MIN = Instant.ofEpochSecond(MIN_SECOND, 0);
 233     /**
 234      * The maximum supported {@code Instant}, '1000000000-12-31T23:59:59.999999999Z'.
 235      * This could be used by an application as a "far future" instant.
 236      * <p>
 237      * This is one year later than the maximum {@code LocalDateTime}.
 238      * This provides sufficient values to handle the range of {@code ZoneOffset}
 239      * which affect the instant in addition to the local date-time.
 240      * The value is also chosen such that the value of the year fits in
 241      * an {@code int}.
 242      */
 243     public static final Instant MAX = Instant.ofEpochSecond(MAX_SECOND, 999_999_999);
 244 
 245     /**
 246      * Serialization version.
 247      */
 248     @java.io.Serial
 249     private static final long serialVersionUID = -665713676816604388L;
 250 
 251     /**
 252      * The number of seconds from the epoch of 1970-01-01T00:00:00Z.
 253      */
 254     private final long seconds;
 255     /**
 256      * The number of nanoseconds, later along the time-line, from the seconds field.
 257      * This is always positive, and never exceeds 999,999,999.
 258      */
 259     private final int nanos;
 260 
 261     //-----------------------------------------------------------------------
 262     /**
 263      * Obtains the current instant from the system clock.
 264      * <p>
 265      * This will query the {@link Clock#systemUTC() system UTC clock} to
 266      * obtain the current instant.
 267      * <p>
 268      * Using this method will prevent the ability to use an alternate time-source for


1326      * @return an ISO-8601 representation of this instant, not null
1327      */
1328     @Override
1329     public String toString() {
1330         return DateTimeFormatter.ISO_INSTANT.format(this);
1331     }
1332 
1333     // -----------------------------------------------------------------------
1334     /**
1335      * Writes the object using a
1336      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1337      * @serialData
1338      * <pre>
1339      *  out.writeByte(2);  // identifies an Instant
1340      *  out.writeLong(seconds);
1341      *  out.writeInt(nanos);
1342      * </pre>
1343      *
1344      * @return the instance of {@code Ser}, not null
1345      */
1346     @java.io.Serial
1347     private Object writeReplace() {
1348         return new Ser(Ser.INSTANT_TYPE, this);
1349     }
1350 
1351     /**
1352      * Defend against malicious streams.
1353      *
1354      * @param s the stream to read
1355      * @throws InvalidObjectException always
1356      */
1357     @java.io.Serial
1358     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1359         throw new InvalidObjectException("Deserialization via serialization delegate");
1360     }
1361 
1362     void writeExternal(DataOutput out) throws IOException {
1363         out.writeLong(seconds);
1364         out.writeInt(nanos);
1365     }
1366 
1367     static Instant readExternal(DataInput in) throws IOException {
1368         long seconds = in.readLong();
1369         int nanos = in.readInt();
1370         return Instant.ofEpochSecond(seconds, nanos);
1371     }
1372 
1373 }
< prev index next >