< prev index next >

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

Print this page

 106  * often viewed as year-month-day-hour-minute-second. Other date and time fields,
 107  * such as day-of-year, day-of-week and week-of-year, can also be accessed.
 108  * Time is represented to nanosecond precision.
 109  * For example, the value "2nd October 2007 at 13:45.30.123456789" can be
 110  * stored in a {@code LocalDateTime}.
 111  * <p>
 112  * This class does not store or represent a time-zone.
 113  * Instead, it is a description of the date, as used for birthdays, combined with
 114  * the local time as seen on a wall clock.
 115  * It cannot represent an instant on the time-line without additional information
 116  * such as an offset or time-zone.
 117  * <p>
 118  * The ISO-8601 calendar system is the modern civil calendar system used today
 119  * in most of the world. It is equivalent to the proleptic Gregorian calendar
 120  * system, in which today's rules for leap years are applied for all time.
 121  * For most applications written today, the ISO-8601 rules are entirely suitable.
 122  * However, any application that makes use of historical dates, and requires them
 123  * to be accurate will find the ISO-8601 approach unsuitable.
 124  * <p>
 125  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 126  * class; programmers should treat instances that are
 127  * {@linkplain #equals(Object) equal} as interchangeable and should not
 128  * use instances for synchronization, or unpredictable behavior may
 129  * occur. For example, in a future release, synchronization may fail.
 130  * The {@code equals} method should be used for comparisons.







 131  *
 132  * @implSpec
 133  * This class is immutable and thread-safe.
 134  *
 135  * @since 1.8
 136  */
 137 @jdk.internal.ValueBased

 138 public final class LocalDateTime
 139         implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable {
 140 
 141     /**
 142      * The minimum supported {@code LocalDateTime}, '-999999999-01-01T00:00:00'.
 143      * This is the local date-time of midnight at the start of the minimum date.
 144      * This combines {@link LocalDate#MIN} and {@link LocalTime#MIN}.
 145      * This could be used by an application as a "far past" date-time.
 146      */
 147     public static final LocalDateTime MIN = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN);
 148     /**
 149      * The maximum supported {@code LocalDateTime}, '+999999999-12-31T23:59:59.999999999'.
 150      * This is the local date-time just before midnight at the end of the maximum date.
 151      * This combines {@link LocalDate#MAX} and {@link LocalTime#MAX}.
 152      * This could be used by an application as a "far future" date-time.
 153      */
 154     public static final LocalDateTime MAX = LocalDateTime.of(LocalDate.MAX, LocalTime.MAX);
 155 
 156     /**
 157      * Serialization version.

1981      * <pre>
1982      *  out.writeByte(5);  // identifies a LocalDateTime
1983      *  // the <a href="{@docRoot}/serialized-form.html#java.time.LocalDate">date</a> excluding the one byte header
1984      *  // the <a href="{@docRoot}/serialized-form.html#java.time.LocalTime">time</a> excluding the one byte header
1985      * </pre>
1986      *
1987      * @return the instance of {@code Ser}, not null
1988      */
1989     @java.io.Serial
1990     private Object writeReplace() {
1991         return new Ser(Ser.LOCAL_DATE_TIME_TYPE, this);
1992     }
1993 
1994     /**
1995      * Defend against malicious streams.
1996      *
1997      * @param s the stream to read
1998      * @throws InvalidObjectException always
1999      */
2000     @java.io.Serial

2001     private void readObject(ObjectInputStream s) throws InvalidObjectException {
2002         throw new InvalidObjectException("Deserialization via serialization delegate");
2003     }
2004 
2005     void writeExternal(DataOutput out) throws IOException {
2006         date.writeExternal(out);
2007         time.writeExternal(out);
2008     }
2009 
2010     static LocalDateTime readExternal(DataInput in) throws IOException {
2011         LocalDate date = LocalDate.readExternal(in);
2012         LocalTime time = LocalTime.readExternal(in);
2013         return LocalDateTime.of(date, time);
2014     }
2015 }

 106  * often viewed as year-month-day-hour-minute-second. Other date and time fields,
 107  * such as day-of-year, day-of-week and week-of-year, can also be accessed.
 108  * Time is represented to nanosecond precision.
 109  * For example, the value "2nd October 2007 at 13:45.30.123456789" can be
 110  * stored in a {@code LocalDateTime}.
 111  * <p>
 112  * This class does not store or represent a time-zone.
 113  * Instead, it is a description of the date, as used for birthdays, combined with
 114  * the local time as seen on a wall clock.
 115  * It cannot represent an instant on the time-line without additional information
 116  * such as an offset or time-zone.
 117  * <p>
 118  * The ISO-8601 calendar system is the modern civil calendar system used today
 119  * in most of the world. It is equivalent to the proleptic Gregorian calendar
 120  * system, in which today's rules for leap years are applied for all time.
 121  * For most applications written today, the ISO-8601 rules are entirely suitable.
 122  * However, any application that makes use of historical dates, and requires them
 123  * to be accurate will find the ISO-8601 approach unsuitable.
 124  * <p>
 125  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 126  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
 127  * as interchangeable and should not use instances for synchronization, mutexes, or
 128  * with {@linkplain java.lang.ref.Reference object references}.
 129  *
 130  * <div class="preview-block">
 131  *      <div class="preview-comment">
 132  *          When preview features are enabled, {@code LocalDateTime} is a {@linkplain Class#isValue value class}.
 133  *          Use of value class instances for synchronization, mutexes, or with
 134  *          {@linkplain java.lang.ref.Reference object references} result in
 135  *          {@link IdentityException}.
 136  *      </div>
 137  * </div>
 138  *
 139  * @implSpec
 140  * This class is immutable and thread-safe.
 141  *
 142  * @since 1.8
 143  */
 144 @jdk.internal.ValueBased
 145 @jdk.internal.MigratedValueClass
 146 public final class LocalDateTime
 147         implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable {
 148 
 149     /**
 150      * The minimum supported {@code LocalDateTime}, '-999999999-01-01T00:00:00'.
 151      * This is the local date-time of midnight at the start of the minimum date.
 152      * This combines {@link LocalDate#MIN} and {@link LocalTime#MIN}.
 153      * This could be used by an application as a "far past" date-time.
 154      */
 155     public static final LocalDateTime MIN = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN);
 156     /**
 157      * The maximum supported {@code LocalDateTime}, '+999999999-12-31T23:59:59.999999999'.
 158      * This is the local date-time just before midnight at the end of the maximum date.
 159      * This combines {@link LocalDate#MAX} and {@link LocalTime#MAX}.
 160      * This could be used by an application as a "far future" date-time.
 161      */
 162     public static final LocalDateTime MAX = LocalDateTime.of(LocalDate.MAX, LocalTime.MAX);
 163 
 164     /**
 165      * Serialization version.

1989      * <pre>
1990      *  out.writeByte(5);  // identifies a LocalDateTime
1991      *  // the <a href="{@docRoot}/serialized-form.html#java.time.LocalDate">date</a> excluding the one byte header
1992      *  // the <a href="{@docRoot}/serialized-form.html#java.time.LocalTime">time</a> excluding the one byte header
1993      * </pre>
1994      *
1995      * @return the instance of {@code Ser}, not null
1996      */
1997     @java.io.Serial
1998     private Object writeReplace() {
1999         return new Ser(Ser.LOCAL_DATE_TIME_TYPE, this);
2000     }
2001 
2002     /**
2003      * Defend against malicious streams.
2004      *
2005      * @param s the stream to read
2006      * @throws InvalidObjectException always
2007      */
2008     @java.io.Serial
2009     @SuppressWarnings("serial") // this method is not invoked for value classes
2010     private void readObject(ObjectInputStream s) throws InvalidObjectException {
2011         throw new InvalidObjectException("Deserialization via serialization delegate");
2012     }
2013 
2014     void writeExternal(DataOutput out) throws IOException {
2015         date.writeExternal(out);
2016         time.writeExternal(out);
2017     }
2018 
2019     static LocalDateTime readExternal(DataInput in) throws IOException {
2020         LocalDate date = LocalDate.readExternal(in);
2021         LocalTime time = LocalTime.readExternal(in);
2022         return LocalDateTime.of(date, time);
2023     }
2024 }
< prev index next >