< prev index next >

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

Print this page




 167      * The comparison is based on the instant.
 168      *
 169      * @param datetime1  the first date-time to compare, not null
 170      * @param datetime2  the other date-time to compare to, not null
 171      * @return the comparator value, negative if less, positive if greater
 172      */
 173     private static int compareInstant(OffsetDateTime datetime1, OffsetDateTime datetime2) {
 174         if (datetime1.getOffset().equals(datetime2.getOffset())) {
 175             return datetime1.toLocalDateTime().compareTo(datetime2.toLocalDateTime());
 176         }
 177         int cmp = Long.compare(datetime1.toEpochSecond(), datetime2.toEpochSecond());
 178         if (cmp == 0) {
 179             cmp = datetime1.toLocalTime().getNano() - datetime2.toLocalTime().getNano();
 180         }
 181         return cmp;
 182     }
 183 
 184     /**
 185      * Serialization version.
 186      */

 187     private static final long serialVersionUID = 2287754244819255394L;
 188 
 189     /**
 190      * The local date-time.
 191      */
 192     private final LocalDateTime dateTime;
 193     /**
 194      * The offset from UTC/Greenwich.
 195      */
 196     private final ZoneOffset offset;
 197 
 198     //-----------------------------------------------------------------------
 199     /**
 200      * Obtains the current date-time from the system clock in the default time-zone.
 201      * <p>
 202      * This will query the {@link Clock#systemDefaultZone() system clock} in the default
 203      * time-zone to obtain the current date-time.
 204      * The offset will be calculated from the time-zone in the clock.
 205      * <p>
 206      * Using this method will prevent the ability to use an alternate clock for testing


1914      * @return a string representation of this date-time, not null
1915      */
1916     @Override
1917     public String toString() {
1918         return dateTime.toString() + offset.toString();
1919     }
1920 
1921     //-----------------------------------------------------------------------
1922     /**
1923      * Writes the object using a
1924      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1925      * @serialData
1926      * <pre>
1927      *  out.writeByte(10);  // identifies an OffsetDateTime
1928      *  // the <a href="{@docRoot}/serialized-form.html#java.time.LocalDateTime">datetime</a> excluding the one byte header
1929      *  // the <a href="{@docRoot}/serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header
1930      * </pre>
1931      *
1932      * @return the instance of {@code Ser}, not null
1933      */

1934     private Object writeReplace() {
1935         return new Ser(Ser.OFFSET_DATE_TIME_TYPE, this);
1936     }
1937 
1938     /**
1939      * Defend against malicious streams.
1940      *
1941      * @param s the stream to read
1942      * @throws InvalidObjectException always
1943      */

1944     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1945         throw new InvalidObjectException("Deserialization via serialization delegate");
1946     }
1947 
1948     void writeExternal(ObjectOutput out) throws IOException {
1949         dateTime.writeExternal(out);
1950         offset.writeExternal(out);
1951     }
1952 
1953     static OffsetDateTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
1954         LocalDateTime dateTime = LocalDateTime.readExternal(in);
1955         ZoneOffset offset = ZoneOffset.readExternal(in);
1956         return OffsetDateTime.of(dateTime, offset);
1957     }
1958 
1959 }


 167      * The comparison is based on the instant.
 168      *
 169      * @param datetime1  the first date-time to compare, not null
 170      * @param datetime2  the other date-time to compare to, not null
 171      * @return the comparator value, negative if less, positive if greater
 172      */
 173     private static int compareInstant(OffsetDateTime datetime1, OffsetDateTime datetime2) {
 174         if (datetime1.getOffset().equals(datetime2.getOffset())) {
 175             return datetime1.toLocalDateTime().compareTo(datetime2.toLocalDateTime());
 176         }
 177         int cmp = Long.compare(datetime1.toEpochSecond(), datetime2.toEpochSecond());
 178         if (cmp == 0) {
 179             cmp = datetime1.toLocalTime().getNano() - datetime2.toLocalTime().getNano();
 180         }
 181         return cmp;
 182     }
 183 
 184     /**
 185      * Serialization version.
 186      */
 187     @java.io.Serial
 188     private static final long serialVersionUID = 2287754244819255394L;
 189 
 190     /**
 191      * The local date-time.
 192      */
 193     private final LocalDateTime dateTime;
 194     /**
 195      * The offset from UTC/Greenwich.
 196      */
 197     private final ZoneOffset offset;
 198 
 199     //-----------------------------------------------------------------------
 200     /**
 201      * Obtains the current date-time from the system clock in the default time-zone.
 202      * <p>
 203      * This will query the {@link Clock#systemDefaultZone() system clock} in the default
 204      * time-zone to obtain the current date-time.
 205      * The offset will be calculated from the time-zone in the clock.
 206      * <p>
 207      * Using this method will prevent the ability to use an alternate clock for testing


1915      * @return a string representation of this date-time, not null
1916      */
1917     @Override
1918     public String toString() {
1919         return dateTime.toString() + offset.toString();
1920     }
1921 
1922     //-----------------------------------------------------------------------
1923     /**
1924      * Writes the object using a
1925      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1926      * @serialData
1927      * <pre>
1928      *  out.writeByte(10);  // identifies an OffsetDateTime
1929      *  // the <a href="{@docRoot}/serialized-form.html#java.time.LocalDateTime">datetime</a> excluding the one byte header
1930      *  // the <a href="{@docRoot}/serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header
1931      * </pre>
1932      *
1933      * @return the instance of {@code Ser}, not null
1934      */
1935     @java.io.Serial
1936     private Object writeReplace() {
1937         return new Ser(Ser.OFFSET_DATE_TIME_TYPE, this);
1938     }
1939 
1940     /**
1941      * Defend against malicious streams.
1942      *
1943      * @param s the stream to read
1944      * @throws InvalidObjectException always
1945      */
1946     @java.io.Serial
1947     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1948         throw new InvalidObjectException("Deserialization via serialization delegate");
1949     }
1950 
1951     void writeExternal(ObjectOutput out) throws IOException {
1952         dateTime.writeExternal(out);
1953         offset.writeExternal(out);
1954     }
1955 
1956     static OffsetDateTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
1957         LocalDateTime dateTime = LocalDateTime.readExternal(in);
1958         ZoneOffset offset = ZoneOffset.readExternal(in);
1959         return OffsetDateTime.of(dateTime, offset);
1960     }
1961 
1962 }
< prev index next >