< prev index next >

src/java.base/share/classes/java/time/chrono/JapaneseDate.java

Print this page

  1 /*
  2  * Copyright (c) 2012, 2023, 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

 95  * A date in the Japanese Imperial calendar system.
 96  * <p>
 97  * This date operates using the {@linkplain JapaneseChronology Japanese Imperial calendar}.
 98  * This calendar system is primarily used in Japan.
 99  * <p>
100  * The Japanese Imperial calendar system is the same as the ISO calendar system
101  * apart from the era-based year numbering. The proleptic-year is defined to be
102  * equal to the ISO proleptic-year.
103  * <p>
104  * Japan introduced the Gregorian calendar starting with Meiji 6.
105  * Only Meiji and later eras are supported;
106  * dates before Meiji 6, January 1 are not supported.
107  * <p>
108  * For example, the Japanese year "Heisei 24" corresponds to ISO year "2012".<br>
109  * Calling {@code japaneseDate.get(YEAR_OF_ERA)} will return 24.<br>
110  * Calling {@code japaneseDate.get(YEAR)} will return 2012.<br>
111  * Calling {@code japaneseDate.get(ERA)} will return 2, corresponding to
112  * {@code JapaneseChronology.ERA_HEISEI}.<br>
113  * <p>
114  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
115  * class; programmers should treat instances that are
116  * {@linkplain #equals(Object) equal} as interchangeable and should not
117  * use instances for synchronization, or unpredictable behavior may
118  * occur. For example, in a future release, synchronization may fail.
119  * The {@code equals} method should be used for comparisons.







120  *
121  * @implSpec
122  * This class is immutable and thread-safe.
123  *
124  * @since 1.8
125  */
126 @jdk.internal.ValueBased
127 public final class JapaneseDate
128         extends ChronoLocalDateImpl<JapaneseDate>
129         implements ChronoLocalDate, Serializable {
130 
131     /**
132      * Serialization version.
133      */
134     @java.io.Serial
135     private static final long serialVersionUID = -305327627230580483L;
136 
137     /**
138      * The underlying ISO local date.
139      */
140     private final transient LocalDate isoDate;
141     /**
142      * The JapaneseEra of this date.
143      */
144     private final transient JapaneseEra era;
145     /**
146      * The Japanese imperial calendar year of this date.
147      */

700     }
701 
702     /**
703      * A hash code for this date.
704      *
705      * @return a suitable hash code based only on the Chronology and the date
706      */
707     @Override  // override for performance
708     public int hashCode() {
709         return getChronology().getId().hashCode() ^ isoDate.hashCode();
710     }
711 
712     //-----------------------------------------------------------------------
713     /**
714      * Defend against malicious streams.
715      *
716      * @param s the stream to read
717      * @throws InvalidObjectException always
718      */
719     @java.io.Serial

720     private void readObject(ObjectInputStream s) throws InvalidObjectException {
721         throw new InvalidObjectException("Deserialization via serialization delegate");
722     }
723 
724     /**
725      * Writes the object using a
726      * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
727      * @serialData
728      * <pre>
729      *  out.writeByte(4);                 // identifies a JapaneseDate
730      *  out.writeInt(get(YEAR));
731      *  out.writeByte(get(MONTH_OF_YEAR));
732      *  out.writeByte(get(DAY_OF_MONTH));
733      * </pre>
734      *
735      * @return the instance of {@code Ser}, not null
736      */
737     @java.io.Serial
738     private Object writeReplace() {
739         return new Ser(Ser.JAPANESE_DATE_TYPE, this);

  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

 95  * A date in the Japanese Imperial calendar system.
 96  * <p>
 97  * This date operates using the {@linkplain JapaneseChronology Japanese Imperial calendar}.
 98  * This calendar system is primarily used in Japan.
 99  * <p>
100  * The Japanese Imperial calendar system is the same as the ISO calendar system
101  * apart from the era-based year numbering. The proleptic-year is defined to be
102  * equal to the ISO proleptic-year.
103  * <p>
104  * Japan introduced the Gregorian calendar starting with Meiji 6.
105  * Only Meiji and later eras are supported;
106  * dates before Meiji 6, January 1 are not supported.
107  * <p>
108  * For example, the Japanese year "Heisei 24" corresponds to ISO year "2012".<br>
109  * Calling {@code japaneseDate.get(YEAR_OF_ERA)} will return 24.<br>
110  * Calling {@code japaneseDate.get(YEAR)} will return 2012.<br>
111  * Calling {@code japaneseDate.get(ERA)} will return 2, corresponding to
112  * {@code JapaneseChronology.ERA_HEISEI}.<br>
113  * <p>
114  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
115  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
116  * as interchangeable and should not use instances for synchronization or
117  * with {@linkplain java.lang.ref.Reference object references}.
118  *
119  * <div class="preview-block">
120  *      <div class="preview-comment">
121  *          When preview features are enabled, {@code JapaneseDate} is a {@linkplain Class#isValue value class}.
122  *          Use of value class instances for synchronization or with
123  *          {@linkplain java.lang.ref.Reference object references} result in
124  *          {@link IdentityException}.
125  *      </div>
126  * </div>
127  *
128  * @implSpec
129  * This class is immutable and thread-safe.
130  *
131  * @since 1.8
132  */
133 @jdk.internal.ValueBased
134 public final /*value*/ class JapaneseDate
135         extends ChronoLocalDateImpl<JapaneseDate>
136         implements ChronoLocalDate, Serializable {
137 
138     /**
139      * Serialization version.
140      */
141     @java.io.Serial
142     private static final long serialVersionUID = -305327627230580483L;
143 
144     /**
145      * The underlying ISO local date.
146      */
147     private final transient LocalDate isoDate;
148     /**
149      * The JapaneseEra of this date.
150      */
151     private final transient JapaneseEra era;
152     /**
153      * The Japanese imperial calendar year of this date.
154      */

707     }
708 
709     /**
710      * A hash code for this date.
711      *
712      * @return a suitable hash code based only on the Chronology and the date
713      */
714     @Override  // override for performance
715     public int hashCode() {
716         return getChronology().getId().hashCode() ^ isoDate.hashCode();
717     }
718 
719     //-----------------------------------------------------------------------
720     /**
721      * Defend against malicious streams.
722      *
723      * @param s the stream to read
724      * @throws InvalidObjectException always
725      */
726     @java.io.Serial
727     @SuppressWarnings("serial") // this method is not invoked for value classes
728     private void readObject(ObjectInputStream s) throws InvalidObjectException {
729         throw new InvalidObjectException("Deserialization via serialization delegate");
730     }
731 
732     /**
733      * Writes the object using a
734      * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
735      * @serialData
736      * <pre>
737      *  out.writeByte(4);                 // identifies a JapaneseDate
738      *  out.writeInt(get(YEAR));
739      *  out.writeByte(get(MONTH_OF_YEAR));
740      *  out.writeByte(get(DAY_OF_MONTH));
741      * </pre>
742      *
743      * @return the instance of {@code Ser}, not null
744      */
745     @java.io.Serial
746     private Object writeReplace() {
747         return new Ser(Ser.JAPANESE_DATE_TYPE, this);
< prev index next >