< prev index next >

src/java.base/share/classes/java/util/Date.java

Print this page




 137                                 CalendarSystem.getGregorianCalendar();
 138     private static BaseCalendar jcal;
 139 
 140     private transient long fastTime;
 141 
 142     /*
 143      * If cdate is null, then fastTime indicates the time in millis.
 144      * If cdate.isNormalized() is true, then fastTime and cdate are in
 145      * synch. Otherwise, fastTime is ignored, and cdate indicates the
 146      * time.
 147      */
 148     private transient BaseCalendar.Date cdate;
 149 
 150     // Initialized just before the value is used. See parse().
 151     private static int defaultCenturyStart;
 152 
 153     /* use serialVersionUID from modified java.util.Date for
 154      * interoperability with JDK1.1. The Date was modified to write
 155      * and read only the UTC time.
 156      */

 157     private static final long serialVersionUID = 7523967970034938905L;
 158 
 159     /**
 160      * Allocates a {@code Date} object and initializes it so that
 161      * it represents the time at which it was allocated, measured to the
 162      * nearest millisecond.
 163      *
 164      * @see     java.lang.System#currentTimeMillis()
 165      */
 166     public Date() {
 167         this(System.currentTimeMillis());
 168     }
 169 
 170     /**
 171      * Allocates a {@code Date} object and initializes it to
 172      * represent the specified number of milliseconds since the
 173      * standard base time known as "the epoch", namely January 1,
 174      * 1970, 00:00:00 GMT.
 175      *
 176      * @param   date   the milliseconds since January 1, 1970, 00:00:00 GMT.


1301         if (cdate.getEra() != null) {
1302             return jcal;
1303         }
1304         return gcal;
1305     }
1306 
1307     private static final synchronized BaseCalendar getJulianCalendar() {
1308         if (jcal == null) {
1309             jcal = (BaseCalendar) CalendarSystem.forName("julian");
1310         }
1311         return jcal;
1312     }
1313 
1314     /**
1315      * Save the state of this object to a stream (i.e., serialize it).
1316      *
1317      * @serialData The value returned by {@code getTime()}
1318      *             is emitted (long).  This represents the offset from
1319      *             January 1, 1970, 00:00:00 GMT in milliseconds.
1320      */

1321     private void writeObject(ObjectOutputStream s)
1322          throws IOException
1323     {
1324         s.defaultWriteObject();
1325         s.writeLong(getTimeImpl());
1326     }
1327 
1328     /**
1329      * Reconstitute this object from a stream (i.e., deserialize it).
1330      */

1331     private void readObject(ObjectInputStream s)
1332          throws IOException, ClassNotFoundException
1333     {
1334         s.defaultReadObject();
1335         fastTime = s.readLong();
1336     }
1337 
1338     /**
1339      * Obtains an instance of {@code Date} from an {@code Instant} object.
1340      * <p>
1341      * {@code Instant} uses a precision of nanoseconds, whereas {@code Date}
1342      * uses a precision of milliseconds.  The conversion will truncate any
1343      * excess precision information as though the amount in nanoseconds was
1344      * subject to integer division by one million.
1345      * <p>
1346      * {@code Instant} can store points on the time-line further in the future
1347      * and further in the past than {@code Date}. In this scenario, this method
1348      * will throw an exception.
1349      *
1350      * @param instant  the instant to convert




 137                                 CalendarSystem.getGregorianCalendar();
 138     private static BaseCalendar jcal;
 139 
 140     private transient long fastTime;
 141 
 142     /*
 143      * If cdate is null, then fastTime indicates the time in millis.
 144      * If cdate.isNormalized() is true, then fastTime and cdate are in
 145      * synch. Otherwise, fastTime is ignored, and cdate indicates the
 146      * time.
 147      */
 148     private transient BaseCalendar.Date cdate;
 149 
 150     // Initialized just before the value is used. See parse().
 151     private static int defaultCenturyStart;
 152 
 153     /* use serialVersionUID from modified java.util.Date for
 154      * interoperability with JDK1.1. The Date was modified to write
 155      * and read only the UTC time.
 156      */
 157     @java.io.Serial
 158     private static final long serialVersionUID = 7523967970034938905L;
 159 
 160     /**
 161      * Allocates a {@code Date} object and initializes it so that
 162      * it represents the time at which it was allocated, measured to the
 163      * nearest millisecond.
 164      *
 165      * @see     java.lang.System#currentTimeMillis()
 166      */
 167     public Date() {
 168         this(System.currentTimeMillis());
 169     }
 170 
 171     /**
 172      * Allocates a {@code Date} object and initializes it to
 173      * represent the specified number of milliseconds since the
 174      * standard base time known as "the epoch", namely January 1,
 175      * 1970, 00:00:00 GMT.
 176      *
 177      * @param   date   the milliseconds since January 1, 1970, 00:00:00 GMT.


1302         if (cdate.getEra() != null) {
1303             return jcal;
1304         }
1305         return gcal;
1306     }
1307 
1308     private static final synchronized BaseCalendar getJulianCalendar() {
1309         if (jcal == null) {
1310             jcal = (BaseCalendar) CalendarSystem.forName("julian");
1311         }
1312         return jcal;
1313     }
1314 
1315     /**
1316      * Save the state of this object to a stream (i.e., serialize it).
1317      *
1318      * @serialData The value returned by {@code getTime()}
1319      *             is emitted (long).  This represents the offset from
1320      *             January 1, 1970, 00:00:00 GMT in milliseconds.
1321      */
1322     @java.io.Serial
1323     private void writeObject(ObjectOutputStream s)
1324          throws IOException
1325     {
1326         s.defaultWriteObject();
1327         s.writeLong(getTimeImpl());
1328     }
1329 
1330     /**
1331      * Reconstitute this object from a stream (i.e., deserialize it).
1332      */
1333     @java.io.Serial
1334     private void readObject(ObjectInputStream s)
1335          throws IOException, ClassNotFoundException
1336     {
1337         s.defaultReadObject();
1338         fastTime = s.readLong();
1339     }
1340 
1341     /**
1342      * Obtains an instance of {@code Date} from an {@code Instant} object.
1343      * <p>
1344      * {@code Instant} uses a precision of nanoseconds, whereas {@code Date}
1345      * uses a precision of milliseconds.  The conversion will truncate any
1346      * excess precision information as though the amount in nanoseconds was
1347      * subject to integer division by one million.
1348      * <p>
1349      * {@code Instant} can store points on the time-line further in the future
1350      * and further in the past than {@code Date}. In this scenario, this method
1351      * will throw an exception.
1352      *
1353      * @param instant  the instant to convert


< prev index next >