< prev index next >

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

Print this page

   1 /*
   2  * Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any

 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.
 158      */

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 }

   1 /*
   2  * Copyright (c) 2012, 2026, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any

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

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