150 * {@code ZonedDateTime} may have unpredictable results and should be avoided.
151 * The {@code equals} method should be used for comparisons.
152 *
153 * @implSpec
154 * A {@code ZonedDateTime} holds state equivalent to three separate objects,
155 * a {@code LocalDateTime}, a {@code ZoneId} and the resolved {@code ZoneOffset}.
156 * The offset and local date-time are used to define an instant when necessary.
157 * The zone ID is used to obtain the rules for how and when the offset changes.
158 * The offset cannot be freely set, as the zone controls which offsets are valid.
159 * <p>
160 * This class is immutable and thread-safe.
161 *
162 * @since 1.8
163 */
164 public final class ZonedDateTime
165 implements Temporal, ChronoZonedDateTime<LocalDate>, Serializable {
166
167 /**
168 * Serialization version.
169 */
170 private static final long serialVersionUID = -6260982410461394882L;
171
172 /**
173 * The local date-time.
174 */
175 private final LocalDateTime dateTime;
176 /**
177 * The offset from UTC/Greenwich.
178 */
179 private final ZoneOffset offset;
180 /**
181 * The time-zone.
182 */
183 private final ZoneId zone;
184
185 //-----------------------------------------------------------------------
186 /**
187 * Obtains the current date-time from the system clock in the default time-zone.
188 * <p>
189 * This will query the {@link Clock#systemDefaultZone() system clock} in the default
2224 if (offset != zone) {
2225 str += '[' + zone.toString() + ']';
2226 }
2227 return str;
2228 }
2229
2230 //-----------------------------------------------------------------------
2231 /**
2232 * Writes the object using a
2233 * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
2234 * @serialData
2235 * <pre>
2236 * out.writeByte(6); // identifies a ZonedDateTime
2237 * // the <a href="{@docRoot}/serialized-form.html#java.time.LocalDateTime">dateTime</a> excluding the one byte header
2238 * // the <a href="{@docRoot}/serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header
2239 * // the <a href="{@docRoot}/serialized-form.html#java.time.ZoneId">zone ID</a> excluding the one byte header
2240 * </pre>
2241 *
2242 * @return the instance of {@code Ser}, not null
2243 */
2244 private Object writeReplace() {
2245 return new Ser(Ser.ZONE_DATE_TIME_TYPE, this);
2246 }
2247
2248 /**
2249 * Defend against malicious streams.
2250 *
2251 * @param s the stream to read
2252 * @throws InvalidObjectException always
2253 */
2254 private void readObject(ObjectInputStream s) throws InvalidObjectException {
2255 throw new InvalidObjectException("Deserialization via serialization delegate");
2256 }
2257
2258 void writeExternal(DataOutput out) throws IOException {
2259 dateTime.writeExternal(out);
2260 offset.writeExternal(out);
2261 zone.write(out);
2262 }
2263
2264 static ZonedDateTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
2265 LocalDateTime dateTime = LocalDateTime.readExternal(in);
2266 ZoneOffset offset = ZoneOffset.readExternal(in);
2267 ZoneId zone = (ZoneId) Ser.read(in);
2268 return ZonedDateTime.ofLenient(dateTime, offset, zone);
2269 }
2270
2271 }
|
150 * {@code ZonedDateTime} may have unpredictable results and should be avoided.
151 * The {@code equals} method should be used for comparisons.
152 *
153 * @implSpec
154 * A {@code ZonedDateTime} holds state equivalent to three separate objects,
155 * a {@code LocalDateTime}, a {@code ZoneId} and the resolved {@code ZoneOffset}.
156 * The offset and local date-time are used to define an instant when necessary.
157 * The zone ID is used to obtain the rules for how and when the offset changes.
158 * The offset cannot be freely set, as the zone controls which offsets are valid.
159 * <p>
160 * This class is immutable and thread-safe.
161 *
162 * @since 1.8
163 */
164 public final class ZonedDateTime
165 implements Temporal, ChronoZonedDateTime<LocalDate>, Serializable {
166
167 /**
168 * Serialization version.
169 */
170 @java.io.Serial
171 private static final long serialVersionUID = -6260982410461394882L;
172
173 /**
174 * The local date-time.
175 */
176 private final LocalDateTime dateTime;
177 /**
178 * The offset from UTC/Greenwich.
179 */
180 private final ZoneOffset offset;
181 /**
182 * The time-zone.
183 */
184 private final ZoneId zone;
185
186 //-----------------------------------------------------------------------
187 /**
188 * Obtains the current date-time from the system clock in the default time-zone.
189 * <p>
190 * This will query the {@link Clock#systemDefaultZone() system clock} in the default
2225 if (offset != zone) {
2226 str += '[' + zone.toString() + ']';
2227 }
2228 return str;
2229 }
2230
2231 //-----------------------------------------------------------------------
2232 /**
2233 * Writes the object using a
2234 * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
2235 * @serialData
2236 * <pre>
2237 * out.writeByte(6); // identifies a ZonedDateTime
2238 * // the <a href="{@docRoot}/serialized-form.html#java.time.LocalDateTime">dateTime</a> excluding the one byte header
2239 * // the <a href="{@docRoot}/serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header
2240 * // the <a href="{@docRoot}/serialized-form.html#java.time.ZoneId">zone ID</a> excluding the one byte header
2241 * </pre>
2242 *
2243 * @return the instance of {@code Ser}, not null
2244 */
2245 @java.io.Serial
2246 private Object writeReplace() {
2247 return new Ser(Ser.ZONE_DATE_TIME_TYPE, this);
2248 }
2249
2250 /**
2251 * Defend against malicious streams.
2252 *
2253 * @param s the stream to read
2254 * @throws InvalidObjectException always
2255 */
2256 @java.io.Serial
2257 private void readObject(ObjectInputStream s) throws InvalidObjectException {
2258 throw new InvalidObjectException("Deserialization via serialization delegate");
2259 }
2260
2261 void writeExternal(DataOutput out) throws IOException {
2262 dateTime.writeExternal(out);
2263 offset.writeExternal(out);
2264 zone.write(out);
2265 }
2266
2267 static ZonedDateTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
2268 LocalDateTime dateTime = LocalDateTime.readExternal(in);
2269 ZoneOffset offset = ZoneOffset.readExternal(in);
2270 ZoneId zone = (ZoneId) Ser.read(in);
2271 return ZonedDateTime.ofLenient(dateTime, offset, zone);
2272 }
2273
2274 }
|