Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/util/Date.java
38829 views
/*1* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.util;2627import java.text.DateFormat;28import java.time.LocalDate;29import java.io.IOException;30import java.io.ObjectOutputStream;31import java.io.ObjectInputStream;32import java.lang.ref.SoftReference;33import java.time.Instant;34import sun.util.calendar.BaseCalendar;35import sun.util.calendar.CalendarDate;36import sun.util.calendar.CalendarSystem;37import sun.util.calendar.CalendarUtils;38import sun.util.calendar.Era;39import sun.util.calendar.Gregorian;40import sun.util.calendar.ZoneInfo;4142/**43* The class <code>Date</code> represents a specific instant44* in time, with millisecond precision.45* <p>46* Prior to JDK 1.1, the class <code>Date</code> had two additional47* functions. It allowed the interpretation of dates as year, month, day, hour,48* minute, and second values. It also allowed the formatting and parsing49* of date strings. Unfortunately, the API for these functions was not50* amenable to internationalization. As of JDK 1.1, the51* <code>Calendar</code> class should be used to convert between dates and time52* fields and the <code>DateFormat</code> class should be used to format and53* parse date strings.54* The corresponding methods in <code>Date</code> are deprecated.55* <p>56* Although the <code>Date</code> class is intended to reflect57* coordinated universal time (UTC), it may not do so exactly,58* depending on the host environment of the Java Virtual Machine.59* Nearly all modern operating systems assume that 1 day =60* 24 × 60 × 60 = 86400 seconds61* in all cases. In UTC, however, about once every year or two there62* is an extra second, called a "leap second." The leap63* second is always added as the last second of the day, and always64* on December 31 or June 30. For example, the last minute of the65* year 1995 was 61 seconds long, thanks to an added leap second.66* Most computer clocks are not accurate enough to be able to reflect67* the leap-second distinction.68* <p>69* Some computer standards are defined in terms of Greenwich mean70* time (GMT), which is equivalent to universal time (UT). GMT is71* the "civil" name for the standard; UT is the72* "scientific" name for the same standard. The73* distinction between UTC and UT is that UTC is based on an atomic74* clock and UT is based on astronomical observations, which for all75* practical purposes is an invisibly fine hair to split. Because the76* earth's rotation is not uniform (it slows down and speeds up77* in complicated ways), UT does not always flow uniformly. Leap78* seconds are introduced as needed into UTC so as to keep UTC within79* 0.9 seconds of UT1, which is a version of UT with certain80* corrections applied. There are other time and date systems as81* well; for example, the time scale used by the satellite-based82* global positioning system (GPS) is synchronized to UTC but is83* <i>not</i> adjusted for leap seconds. An interesting source of84* further information is the U.S. Naval Observatory, particularly85* the Directorate of Time at:86* <blockquote><pre>87* <a href=http://tycho.usno.navy.mil>http://tycho.usno.navy.mil</a>88* </pre></blockquote>89* <p>90* and their definitions of "Systems of Time" at:91* <blockquote><pre>92* <a href=http://tycho.usno.navy.mil/systime.html>http://tycho.usno.navy.mil/systime.html</a>93* </pre></blockquote>94* <p>95* In all methods of class <code>Date</code> that accept or return96* year, month, date, hours, minutes, and seconds values, the97* following representations are used:98* <ul>99* <li>A year <i>y</i> is represented by the integer100* <i>y</i> <code>- 1900</code>.101* <li>A month is represented by an integer from 0 to 11; 0 is January,102* 1 is February, and so forth; thus 11 is December.103* <li>A date (day of month) is represented by an integer from 1 to 31104* in the usual manner.105* <li>An hour is represented by an integer from 0 to 23. Thus, the hour106* from midnight to 1 a.m. is hour 0, and the hour from noon to 1107* p.m. is hour 12.108* <li>A minute is represented by an integer from 0 to 59 in the usual manner.109* <li>A second is represented by an integer from 0 to 61; the values 60 and110* 61 occur only for leap seconds and even then only in Java111* implementations that actually track leap seconds correctly. Because112* of the manner in which leap seconds are currently introduced, it is113* extremely unlikely that two leap seconds will occur in the same114* minute, but this specification follows the date and time conventions115* for ISO C.116* </ul>117* <p>118* In all cases, arguments given to methods for these purposes need119* not fall within the indicated ranges; for example, a date may be120* specified as January 32 and is interpreted as meaning February 1.121*122* @author James Gosling123* @author Arthur van Hoff124* @author Alan Liu125* @see java.text.DateFormat126* @see java.util.Calendar127* @see java.util.TimeZone128* @since JDK1.0129*/130public class Date131implements java.io.Serializable, Cloneable, Comparable<Date>132{133private static final BaseCalendar gcal =134CalendarSystem.getGregorianCalendar();135private static BaseCalendar jcal;136137private transient long fastTime;138139/*140* If cdate is null, then fastTime indicates the time in millis.141* If cdate.isNormalized() is true, then fastTime and cdate are in142* synch. Otherwise, fastTime is ignored, and cdate indicates the143* time.144*/145private transient BaseCalendar.Date cdate;146147// Initialized just before the value is used. See parse().148private static int defaultCenturyStart;149150/* use serialVersionUID from modified java.util.Date for151* interoperability with JDK1.1. The Date was modified to write152* and read only the UTC time.153*/154private static final long serialVersionUID = 7523967970034938905L;155156/**157* Allocates a <code>Date</code> object and initializes it so that158* it represents the time at which it was allocated, measured to the159* nearest millisecond.160*161* @see java.lang.System#currentTimeMillis()162*/163public Date() {164this(System.currentTimeMillis());165}166167/**168* Allocates a <code>Date</code> object and initializes it to169* represent the specified number of milliseconds since the170* standard base time known as "the epoch", namely January 1,171* 1970, 00:00:00 GMT.172*173* @param date the milliseconds since January 1, 1970, 00:00:00 GMT.174* @see java.lang.System#currentTimeMillis()175*/176public Date(long date) {177fastTime = date;178}179180/**181* Allocates a <code>Date</code> object and initializes it so that182* it represents midnight, local time, at the beginning of the day183* specified by the <code>year</code>, <code>month</code>, and184* <code>date</code> arguments.185*186* @param year the year minus 1900.187* @param month the month between 0-11.188* @param date the day of the month between 1-31.189* @see java.util.Calendar190* @deprecated As of JDK version 1.1,191* replaced by <code>Calendar.set(year + 1900, month, date)</code>192* or <code>GregorianCalendar(year + 1900, month, date)</code>.193*/194@Deprecated195public Date(int year, int month, int date) {196this(year, month, date, 0, 0, 0);197}198199/**200* Allocates a <code>Date</code> object and initializes it so that201* it represents the instant at the start of the minute specified by202* the <code>year</code>, <code>month</code>, <code>date</code>,203* <code>hrs</code>, and <code>min</code> arguments, in the local204* time zone.205*206* @param year the year minus 1900.207* @param month the month between 0-11.208* @param date the day of the month between 1-31.209* @param hrs the hours between 0-23.210* @param min the minutes between 0-59.211* @see java.util.Calendar212* @deprecated As of JDK version 1.1,213* replaced by <code>Calendar.set(year + 1900, month, date,214* hrs, min)</code> or <code>GregorianCalendar(year + 1900,215* month, date, hrs, min)</code>.216*/217@Deprecated218public Date(int year, int month, int date, int hrs, int min) {219this(year, month, date, hrs, min, 0);220}221222/**223* Allocates a <code>Date</code> object and initializes it so that224* it represents the instant at the start of the second specified225* by the <code>year</code>, <code>month</code>, <code>date</code>,226* <code>hrs</code>, <code>min</code>, and <code>sec</code> arguments,227* in the local time zone.228*229* @param year the year minus 1900.230* @param month the month between 0-11.231* @param date the day of the month between 1-31.232* @param hrs the hours between 0-23.233* @param min the minutes between 0-59.234* @param sec the seconds between 0-59.235* @see java.util.Calendar236* @deprecated As of JDK version 1.1,237* replaced by <code>Calendar.set(year + 1900, month, date,238* hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,239* month, date, hrs, min, sec)</code>.240*/241@Deprecated242public Date(int year, int month, int date, int hrs, int min, int sec) {243int y = year + 1900;244// month is 0-based. So we have to normalize month to support Long.MAX_VALUE.245if (month >= 12) {246y += month / 12;247month %= 12;248} else if (month < 0) {249y += CalendarUtils.floorDivide(month, 12);250month = CalendarUtils.mod(month, 12);251}252BaseCalendar cal = getCalendarSystem(y);253cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef());254cdate.setNormalizedDate(y, month + 1, date).setTimeOfDay(hrs, min, sec, 0);255getTimeImpl();256cdate = null;257}258259/**260* Allocates a <code>Date</code> object and initializes it so that261* it represents the date and time indicated by the string262* <code>s</code>, which is interpreted as if by the263* {@link Date#parse} method.264*265* @param s a string representation of the date.266* @see java.text.DateFormat267* @see java.util.Date#parse(java.lang.String)268* @deprecated As of JDK version 1.1,269* replaced by <code>DateFormat.parse(String s)</code>.270*/271@Deprecated272public Date(String s) {273this(parse(s));274}275276/**277* Return a copy of this object.278*/279public Object clone() {280Date d = null;281try {282d = (Date)super.clone();283if (cdate != null) {284d.cdate = (BaseCalendar.Date) cdate.clone();285}286} catch (CloneNotSupportedException e) {} // Won't happen287return d;288}289290/**291* Determines the date and time based on the arguments. The292* arguments are interpreted as a year, month, day of the month,293* hour of the day, minute within the hour, and second within the294* minute, exactly as for the <tt>Date</tt> constructor with six295* arguments, except that the arguments are interpreted relative296* to UTC rather than to the local time zone. The time indicated is297* returned represented as the distance, measured in milliseconds,298* of that time from the epoch (00:00:00 GMT on January 1, 1970).299*300* @param year the year minus 1900.301* @param month the month between 0-11.302* @param date the day of the month between 1-31.303* @param hrs the hours between 0-23.304* @param min the minutes between 0-59.305* @param sec the seconds between 0-59.306* @return the number of milliseconds since January 1, 1970, 00:00:00 GMT for307* the date and time specified by the arguments.308* @see java.util.Calendar309* @deprecated As of JDK version 1.1,310* replaced by <code>Calendar.set(year + 1900, month, date,311* hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,312* month, date, hrs, min, sec)</code>, using a UTC313* <code>TimeZone</code>, followed by <code>Calendar.getTime().getTime()</code>.314*/315@Deprecated316public static long UTC(int year, int month, int date,317int hrs, int min, int sec) {318int y = year + 1900;319// month is 0-based. So we have to normalize month to support Long.MAX_VALUE.320if (month >= 12) {321y += month / 12;322month %= 12;323} else if (month < 0) {324y += CalendarUtils.floorDivide(month, 12);325month = CalendarUtils.mod(month, 12);326}327int m = month + 1;328BaseCalendar cal = getCalendarSystem(y);329BaseCalendar.Date udate = (BaseCalendar.Date) cal.newCalendarDate(null);330udate.setNormalizedDate(y, m, date).setTimeOfDay(hrs, min, sec, 0);331332// Use a Date instance to perform normalization. Its fastTime333// is the UTC value after the normalization.334Date d = new Date(0);335d.normalize(udate);336return d.fastTime;337}338339/**340* Attempts to interpret the string <tt>s</tt> as a representation341* of a date and time. If the attempt is successful, the time342* indicated is returned represented as the distance, measured in343* milliseconds, of that time from the epoch (00:00:00 GMT on344* January 1, 1970). If the attempt fails, an345* <tt>IllegalArgumentException</tt> is thrown.346* <p>347* It accepts many syntaxes; in particular, it recognizes the IETF348* standard date syntax: "Sat, 12 Aug 1995 13:30:00 GMT". It also349* understands the continental U.S. time-zone abbreviations, but for350* general use, a time-zone offset should be used: "Sat, 12 Aug 1995351* 13:30:00 GMT+0430" (4 hours, 30 minutes west of the Greenwich352* meridian). If no time zone is specified, the local time zone is353* assumed. GMT and UTC are considered equivalent.354* <p>355* The string <tt>s</tt> is processed from left to right, looking for356* data of interest. Any material in <tt>s</tt> that is within the357* ASCII parenthesis characters <tt>(</tt> and <tt>)</tt> is ignored.358* Parentheses may be nested. Otherwise, the only characters permitted359* within <tt>s</tt> are these ASCII characters:360* <blockquote><pre>361* abcdefghijklmnopqrstuvwxyz362* ABCDEFGHIJKLMNOPQRSTUVWXYZ363* 0123456789,+-:/</pre></blockquote>364* and whitespace characters.<p>365* A consecutive sequence of decimal digits is treated as a decimal366* number:<ul>367* <li>If a number is preceded by <tt>+</tt> or <tt>-</tt> and a year368* has already been recognized, then the number is a time-zone369* offset. If the number is less than 24, it is an offset measured370* in hours. Otherwise, it is regarded as an offset in minutes,371* expressed in 24-hour time format without punctuation. A372* preceding <tt>-</tt> means a westward offset. Time zone offsets373* are always relative to UTC (Greenwich). Thus, for example,374* <tt>-5</tt> occurring in the string would mean "five hours west375* of Greenwich" and <tt>+0430</tt> would mean "four hours and376* thirty minutes east of Greenwich." It is permitted for the377* string to specify <tt>GMT</tt>, <tt>UT</tt>, or <tt>UTC</tt>378* redundantly-for example, <tt>GMT-5</tt> or <tt>utc+0430</tt>.379* <li>The number is regarded as a year number if one of the380* following conditions is true:381* <ul>382* <li>The number is equal to or greater than 70 and followed by a383* space, comma, slash, or end of string384* <li>The number is less than 70, and both a month and a day of385* the month have already been recognized</li>386* </ul>387* If the recognized year number is less than 100, it is388* interpreted as an abbreviated year relative to a century of389* which dates are within 80 years before and 19 years after390* the time when the Date class is initialized.391* After adjusting the year number, 1900 is subtracted from392* it. For example, if the current year is 1999 then years in393* the range 19 to 99 are assumed to mean 1919 to 1999, while394* years from 0 to 18 are assumed to mean 2000 to 2018. Note395* that this is slightly different from the interpretation of396* years less than 100 that is used in {@link java.text.SimpleDateFormat}.397* <li>If the number is followed by a colon, it is regarded as an hour,398* unless an hour has already been recognized, in which case it is399* regarded as a minute.400* <li>If the number is followed by a slash, it is regarded as a month401* (it is decreased by 1 to produce a number in the range <tt>0</tt>402* to <tt>11</tt>), unless a month has already been recognized, in403* which case it is regarded as a day of the month.404* <li>If the number is followed by whitespace, a comma, a hyphen, or405* end of string, then if an hour has been recognized but not a406* minute, it is regarded as a minute; otherwise, if a minute has407* been recognized but not a second, it is regarded as a second;408* otherwise, it is regarded as a day of the month. </ul><p>409* A consecutive sequence of letters is regarded as a word and treated410* as follows:<ul>411* <li>A word that matches <tt>AM</tt>, ignoring case, is ignored (but412* the parse fails if an hour has not been recognized or is less413* than <tt>1</tt> or greater than <tt>12</tt>).414* <li>A word that matches <tt>PM</tt>, ignoring case, adds <tt>12</tt>415* to the hour (but the parse fails if an hour has not been416* recognized or is less than <tt>1</tt> or greater than <tt>12</tt>).417* <li>Any word that matches any prefix of <tt>SUNDAY, MONDAY, TUESDAY,418* WEDNESDAY, THURSDAY, FRIDAY</tt>, or <tt>SATURDAY</tt>, ignoring419* case, is ignored. For example, <tt>sat, Friday, TUE</tt>, and420* <tt>Thurs</tt> are ignored.421* <li>Otherwise, any word that matches any prefix of <tt>JANUARY,422* FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER,423* OCTOBER, NOVEMBER</tt>, or <tt>DECEMBER</tt>, ignoring case, and424* considering them in the order given here, is recognized as425* specifying a month and is converted to a number (<tt>0</tt> to426* <tt>11</tt>). For example, <tt>aug, Sept, april</tt>, and427* <tt>NOV</tt> are recognized as months. So is <tt>Ma</tt>, which428* is recognized as <tt>MARCH</tt>, not <tt>MAY</tt>.429* <li>Any word that matches <tt>GMT, UT</tt>, or <tt>UTC</tt>, ignoring430* case, is treated as referring to UTC.431* <li>Any word that matches <tt>EST, CST, MST</tt>, or <tt>PST</tt>,432* ignoring case, is recognized as referring to the time zone in433* North America that is five, six, seven, or eight hours west of434* Greenwich, respectively. Any word that matches <tt>EDT, CDT,435* MDT</tt>, or <tt>PDT</tt>, ignoring case, is recognized as436* referring to the same time zone, respectively, during daylight437* saving time.</ul><p>438* Once the entire string s has been scanned, it is converted to a time439* result in one of two ways. If a time zone or time-zone offset has been440* recognized, then the year, month, day of month, hour, minute, and441* second are interpreted in UTC and then the time-zone offset is442* applied. Otherwise, the year, month, day of month, hour, minute, and443* second are interpreted in the local time zone.444*445* @param s a string to be parsed as a date.446* @return the number of milliseconds since January 1, 1970, 00:00:00 GMT447* represented by the string argument.448* @see java.text.DateFormat449* @deprecated As of JDK version 1.1,450* replaced by <code>DateFormat.parse(String s)</code>.451*/452@Deprecated453public static long parse(String s) {454int year = Integer.MIN_VALUE;455int mon = -1;456int mday = -1;457int hour = -1;458int min = -1;459int sec = -1;460int millis = -1;461int c = -1;462int i = 0;463int n = -1;464int wst = -1;465int tzoffset = -1;466int prevc = 0;467syntax:468{469if (s == null)470break syntax;471int limit = s.length();472while (i < limit) {473c = s.charAt(i);474i++;475if (c <= ' ' || c == ',')476continue;477if (c == '(') { // skip comments478int depth = 1;479while (i < limit) {480c = s.charAt(i);481i++;482if (c == '(') depth++;483else if (c == ')')484if (--depth <= 0)485break;486}487continue;488}489if ('0' <= c && c <= '9') {490n = c - '0';491while (i < limit && '0' <= (c = s.charAt(i)) && c <= '9') {492n = n * 10 + c - '0';493i++;494}495if (prevc == '+' || prevc == '-' && year != Integer.MIN_VALUE) {496// timezone offset497if (n < 24)498n = n * 60; // EG. "GMT-3"499else500n = n % 100 + n / 100 * 60; // eg "GMT-0430"501if (prevc == '+') // plus means east of GMT502n = -n;503if (tzoffset != 0 && tzoffset != -1)504break syntax;505tzoffset = n;506} else if (n >= 70)507if (year != Integer.MIN_VALUE)508break syntax;509else if (c <= ' ' || c == ',' || c == '/' || i >= limit)510// year = n < 1900 ? n : n - 1900;511year = n;512else513break syntax;514else if (c == ':')515if (hour < 0)516hour = (byte) n;517else if (min < 0)518min = (byte) n;519else520break syntax;521else if (c == '/')522if (mon < 0)523mon = (byte) (n - 1);524else if (mday < 0)525mday = (byte) n;526else527break syntax;528else if (i < limit && c != ',' && c > ' ' && c != '-')529break syntax;530else if (hour >= 0 && min < 0)531min = (byte) n;532else if (min >= 0 && sec < 0)533sec = (byte) n;534else if (mday < 0)535mday = (byte) n;536// Handle two-digit years < 70 (70-99 handled above).537else if (year == Integer.MIN_VALUE && mon >= 0 && mday >= 0)538year = n;539else540break syntax;541prevc = 0;542} else if (c == '/' || c == ':' || c == '+' || c == '-')543prevc = c;544else {545int st = i - 1;546while (i < limit) {547c = s.charAt(i);548if (!('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'))549break;550i++;551}552if (i <= st + 1)553break syntax;554int k;555for (k = wtb.length; --k >= 0;)556if (wtb[k].regionMatches(true, 0, s, st, i - st)) {557int action = ttb[k];558if (action != 0) {559if (action == 1) { // pm560if (hour > 12 || hour < 1)561break syntax;562else if (hour < 12)563hour += 12;564} else if (action == 14) { // am565if (hour > 12 || hour < 1)566break syntax;567else if (hour == 12)568hour = 0;569} else if (action <= 13) { // month!570if (mon < 0)571mon = (byte) (action - 2);572else573break syntax;574} else {575tzoffset = action - 10000;576}577}578break;579}580if (k < 0)581break syntax;582prevc = 0;583}584}585if (year == Integer.MIN_VALUE || mon < 0 || mday < 0)586break syntax;587// Parse 2-digit years within the correct default century.588if (year < 100) {589synchronized (Date.class) {590if (defaultCenturyStart == 0) {591defaultCenturyStart = gcal.getCalendarDate().getYear() - 80;592}593}594year += (defaultCenturyStart / 100) * 100;595if (year < defaultCenturyStart) year += 100;596}597if (sec < 0)598sec = 0;599if (min < 0)600min = 0;601if (hour < 0)602hour = 0;603BaseCalendar cal = getCalendarSystem(year);604if (tzoffset == -1) { // no time zone specified, have to use local605BaseCalendar.Date ldate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef());606ldate.setDate(year, mon + 1, mday);607ldate.setTimeOfDay(hour, min, sec, 0);608return cal.getTime(ldate);609}610BaseCalendar.Date udate = (BaseCalendar.Date) cal.newCalendarDate(null); // no time zone611udate.setDate(year, mon + 1, mday);612udate.setTimeOfDay(hour, min, sec, 0);613return cal.getTime(udate) + tzoffset * (60 * 1000);614}615// syntax error616throw new IllegalArgumentException();617}618private final static String wtb[] = {619"am", "pm",620"monday", "tuesday", "wednesday", "thursday", "friday",621"saturday", "sunday",622"january", "february", "march", "april", "may", "june",623"july", "august", "september", "october", "november", "december",624"gmt", "ut", "utc", "est", "edt", "cst", "cdt",625"mst", "mdt", "pst", "pdt"626};627private final static int ttb[] = {62814, 1, 0, 0, 0, 0, 0, 0, 0,6292, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,63010000 + 0, 10000 + 0, 10000 + 0, // GMT/UT/UTC63110000 + 5 * 60, 10000 + 4 * 60, // EST/EDT63210000 + 6 * 60, 10000 + 5 * 60, // CST/CDT63310000 + 7 * 60, 10000 + 6 * 60, // MST/MDT63410000 + 8 * 60, 10000 + 7 * 60 // PST/PDT635};636637/**638* Returns a value that is the result of subtracting 1900 from the639* year that contains or begins with the instant in time represented640* by this <code>Date</code> object, as interpreted in the local641* time zone.642*643* @return the year represented by this date, minus 1900.644* @see java.util.Calendar645* @deprecated As of JDK version 1.1,646* replaced by <code>Calendar.get(Calendar.YEAR) - 1900</code>.647*/648@Deprecated649public int getYear() {650return normalize().getYear() - 1900;651}652653/**654* Sets the year of this <tt>Date</tt> object to be the specified655* value plus 1900. This <code>Date</code> object is modified so656* that it represents a point in time within the specified year,657* with the month, date, hour, minute, and second the same as658* before, as interpreted in the local time zone. (Of course, if659* the date was February 29, for example, and the year is set to a660* non-leap year, then the new date will be treated as if it were661* on March 1.)662*663* @param year the year value.664* @see java.util.Calendar665* @deprecated As of JDK version 1.1,666* replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.667*/668@Deprecated669public void setYear(int year) {670getCalendarDate().setNormalizedYear(year + 1900);671}672673/**674* Returns a number representing the month that contains or begins675* with the instant in time represented by this <tt>Date</tt> object.676* The value returned is between <code>0</code> and <code>11</code>,677* with the value <code>0</code> representing January.678*679* @return the month represented by this date.680* @see java.util.Calendar681* @deprecated As of JDK version 1.1,682* replaced by <code>Calendar.get(Calendar.MONTH)</code>.683*/684@Deprecated685public int getMonth() {686return normalize().getMonth() - 1; // adjust 1-based to 0-based687}688689/**690* Sets the month of this date to the specified value. This691* <tt>Date</tt> object is modified so that it represents a point692* in time within the specified month, with the year, date, hour,693* minute, and second the same as before, as interpreted in the694* local time zone. If the date was October 31, for example, and695* the month is set to June, then the new date will be treated as696* if it were on July 1, because June has only 30 days.697*698* @param month the month value between 0-11.699* @see java.util.Calendar700* @deprecated As of JDK version 1.1,701* replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.702*/703@Deprecated704public void setMonth(int month) {705int y = 0;706if (month >= 12) {707y = month / 12;708month %= 12;709} else if (month < 0) {710y = CalendarUtils.floorDivide(month, 12);711month = CalendarUtils.mod(month, 12);712}713BaseCalendar.Date d = getCalendarDate();714if (y != 0) {715d.setNormalizedYear(d.getNormalizedYear() + y);716}717d.setMonth(month + 1); // adjust 0-based to 1-based month numbering718}719720/**721* Returns the day of the month represented by this <tt>Date</tt> object.722* The value returned is between <code>1</code> and <code>31</code>723* representing the day of the month that contains or begins with the724* instant in time represented by this <tt>Date</tt> object, as725* interpreted in the local time zone.726*727* @return the day of the month represented by this date.728* @see java.util.Calendar729* @deprecated As of JDK version 1.1,730* replaced by <code>Calendar.get(Calendar.DAY_OF_MONTH)</code>.731* @deprecated732*/733@Deprecated734public int getDate() {735return normalize().getDayOfMonth();736}737738/**739* Sets the day of the month of this <tt>Date</tt> object to the740* specified value. This <tt>Date</tt> object is modified so that741* it represents a point in time within the specified day of the742* month, with the year, month, hour, minute, and second the same743* as before, as interpreted in the local time zone. If the date744* was April 30, for example, and the date is set to 31, then it745* will be treated as if it were on May 1, because April has only746* 30 days.747*748* @param date the day of the month value between 1-31.749* @see java.util.Calendar750* @deprecated As of JDK version 1.1,751* replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.752*/753@Deprecated754public void setDate(int date) {755getCalendarDate().setDayOfMonth(date);756}757758/**759* Returns the day of the week represented by this date. The760* returned value (<tt>0</tt> = Sunday, <tt>1</tt> = Monday,761* <tt>2</tt> = Tuesday, <tt>3</tt> = Wednesday, <tt>4</tt> =762* Thursday, <tt>5</tt> = Friday, <tt>6</tt> = Saturday)763* represents the day of the week that contains or begins with764* the instant in time represented by this <tt>Date</tt> object,765* as interpreted in the local time zone.766*767* @return the day of the week represented by this date.768* @see java.util.Calendar769* @deprecated As of JDK version 1.1,770* replaced by <code>Calendar.get(Calendar.DAY_OF_WEEK)</code>.771*/772@Deprecated773public int getDay() {774return normalize().getDayOfWeek() - BaseCalendar.SUNDAY;775}776777/**778* Returns the hour represented by this <tt>Date</tt> object. The779* returned value is a number (<tt>0</tt> through <tt>23</tt>)780* representing the hour within the day that contains or begins781* with the instant in time represented by this <tt>Date</tt>782* object, as interpreted in the local time zone.783*784* @return the hour represented by this date.785* @see java.util.Calendar786* @deprecated As of JDK version 1.1,787* replaced by <code>Calendar.get(Calendar.HOUR_OF_DAY)</code>.788*/789@Deprecated790public int getHours() {791return normalize().getHours();792}793794/**795* Sets the hour of this <tt>Date</tt> object to the specified value.796* This <tt>Date</tt> object is modified so that it represents a point797* in time within the specified hour of the day, with the year, month,798* date, minute, and second the same as before, as interpreted in the799* local time zone.800*801* @param hours the hour value.802* @see java.util.Calendar803* @deprecated As of JDK version 1.1,804* replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.805*/806@Deprecated807public void setHours(int hours) {808getCalendarDate().setHours(hours);809}810811/**812* Returns the number of minutes past the hour represented by this date,813* as interpreted in the local time zone.814* The value returned is between <code>0</code> and <code>59</code>.815*816* @return the number of minutes past the hour represented by this date.817* @see java.util.Calendar818* @deprecated As of JDK version 1.1,819* replaced by <code>Calendar.get(Calendar.MINUTE)</code>.820*/821@Deprecated822public int getMinutes() {823return normalize().getMinutes();824}825826/**827* Sets the minutes of this <tt>Date</tt> object to the specified value.828* This <tt>Date</tt> object is modified so that it represents a point829* in time within the specified minute of the hour, with the year, month,830* date, hour, and second the same as before, as interpreted in the831* local time zone.832*833* @param minutes the value of the minutes.834* @see java.util.Calendar835* @deprecated As of JDK version 1.1,836* replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.837*/838@Deprecated839public void setMinutes(int minutes) {840getCalendarDate().setMinutes(minutes);841}842843/**844* Returns the number of seconds past the minute represented by this date.845* The value returned is between <code>0</code> and <code>61</code>. The846* values <code>60</code> and <code>61</code> can only occur on those847* Java Virtual Machines that take leap seconds into account.848*849* @return the number of seconds past the minute represented by this date.850* @see java.util.Calendar851* @deprecated As of JDK version 1.1,852* replaced by <code>Calendar.get(Calendar.SECOND)</code>.853*/854@Deprecated855public int getSeconds() {856return normalize().getSeconds();857}858859/**860* Sets the seconds of this <tt>Date</tt> to the specified value.861* This <tt>Date</tt> object is modified so that it represents a862* point in time within the specified second of the minute, with863* the year, month, date, hour, and minute the same as before, as864* interpreted in the local time zone.865*866* @param seconds the seconds value.867* @see java.util.Calendar868* @deprecated As of JDK version 1.1,869* replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.870*/871@Deprecated872public void setSeconds(int seconds) {873getCalendarDate().setSeconds(seconds);874}875876/**877* Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT878* represented by this <tt>Date</tt> object.879*880* @return the number of milliseconds since January 1, 1970, 00:00:00 GMT881* represented by this date.882*/883public long getTime() {884return getTimeImpl();885}886887private final long getTimeImpl() {888if (cdate != null && !cdate.isNormalized()) {889normalize();890}891return fastTime;892}893894/**895* Sets this <code>Date</code> object to represent a point in time that is896* <code>time</code> milliseconds after January 1, 1970 00:00:00 GMT.897*898* @param time the number of milliseconds.899*/900public void setTime(long time) {901fastTime = time;902cdate = null;903}904905/**906* Tests if this date is before the specified date.907*908* @param when a date.909* @return <code>true</code> if and only if the instant of time910* represented by this <tt>Date</tt> object is strictly911* earlier than the instant represented by <tt>when</tt>;912* <code>false</code> otherwise.913* @exception NullPointerException if <code>when</code> is null.914*/915public boolean before(Date when) {916return getMillisOf(this) < getMillisOf(when);917}918919/**920* Tests if this date is after the specified date.921*922* @param when a date.923* @return <code>true</code> if and only if the instant represented924* by this <tt>Date</tt> object is strictly later than the925* instant represented by <tt>when</tt>;926* <code>false</code> otherwise.927* @exception NullPointerException if <code>when</code> is null.928*/929public boolean after(Date when) {930return getMillisOf(this) > getMillisOf(when);931}932933/**934* Compares two dates for equality.935* The result is <code>true</code> if and only if the argument is936* not <code>null</code> and is a <code>Date</code> object that937* represents the same point in time, to the millisecond, as this object.938* <p>939* Thus, two <code>Date</code> objects are equal if and only if the940* <code>getTime</code> method returns the same <code>long</code>941* value for both.942*943* @param obj the object to compare with.944* @return <code>true</code> if the objects are the same;945* <code>false</code> otherwise.946* @see java.util.Date#getTime()947*/948public boolean equals(Object obj) {949return obj instanceof Date && getTime() == ((Date) obj).getTime();950}951952/**953* Returns the millisecond value of this <code>Date</code> object954* without affecting its internal state.955*/956static final long getMillisOf(Date date) {957if (date.cdate == null || date.cdate.isNormalized()) {958return date.fastTime;959}960BaseCalendar.Date d = (BaseCalendar.Date) date.cdate.clone();961return gcal.getTime(d);962}963964/**965* Compares two Dates for ordering.966*967* @param anotherDate the <code>Date</code> to be compared.968* @return the value <code>0</code> if the argument Date is equal to969* this Date; a value less than <code>0</code> if this Date970* is before the Date argument; and a value greater than971* <code>0</code> if this Date is after the Date argument.972* @since 1.2973* @exception NullPointerException if <code>anotherDate</code> is null.974*/975public int compareTo(Date anotherDate) {976long thisTime = getMillisOf(this);977long anotherTime = getMillisOf(anotherDate);978return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));979}980981/**982* Returns a hash code value for this object. The result is the983* exclusive OR of the two halves of the primitive <tt>long</tt>984* value returned by the {@link Date#getTime}985* method. That is, the hash code is the value of the expression:986* <blockquote><pre>{@code987* (int)(this.getTime()^(this.getTime() >>> 32))988* }</pre></blockquote>989*990* @return a hash code value for this object.991*/992public int hashCode() {993long ht = this.getTime();994return (int) ht ^ (int) (ht >> 32);995}996997/**998* Converts this <code>Date</code> object to a <code>String</code>999* of the form:1000* <blockquote><pre>1001* dow mon dd hh:mm:ss zzz yyyy</pre></blockquote>1002* where:<ul>1003* <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed,1004* Thu, Fri, Sat</tt>).1005* <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun,1006* Jul, Aug, Sep, Oct, Nov, Dec</tt>).1007* <li><tt>dd</tt> is the day of the month (<tt>01</tt> through1008* <tt>31</tt>), as two decimal digits.1009* <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through1010* <tt>23</tt>), as two decimal digits.1011* <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through1012* <tt>59</tt>), as two decimal digits.1013* <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through1014* <tt>61</tt>, as two decimal digits.1015* <li><tt>zzz</tt> is the time zone (and may reflect daylight saving1016* time). Standard time zone abbreviations include those1017* recognized by the method <tt>parse</tt>. If time zone1018* information is not available, then <tt>zzz</tt> is empty -1019* that is, it consists of no characters at all.1020* <li><tt>yyyy</tt> is the year, as four decimal digits.1021* </ul>1022*1023* @return a string representation of this date.1024* @see java.util.Date#toLocaleString()1025* @see java.util.Date#toGMTString()1026*/1027public String toString() {1028// "EEE MMM dd HH:mm:ss zzz yyyy";1029BaseCalendar.Date date = normalize();1030StringBuilder sb = new StringBuilder(28);1031int index = date.getDayOfWeek();1032if (index == BaseCalendar.SUNDAY) {1033index = 8;1034}1035convertToAbbr(sb, wtb[index]).append(' '); // EEE1036convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); // MMM1037CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd10381039CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); // HH1040CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm1041CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss1042TimeZone zi = date.getZone();1043if (zi != null) {1044sb.append(zi.getDisplayName(date.isDaylightTime(), TimeZone.SHORT, Locale.US)); // zzz1045} else {1046sb.append("GMT");1047}1048sb.append(' ').append(date.getYear()); // yyyy1049return sb.toString();1050}10511052/**1053* Converts the given name to its 3-letter abbreviation (e.g.,1054* "monday" -> "Mon") and stored the abbreviation in the given1055* <code>StringBuilder</code>.1056*/1057private static final StringBuilder convertToAbbr(StringBuilder sb, String name) {1058sb.append(Character.toUpperCase(name.charAt(0)));1059sb.append(name.charAt(1)).append(name.charAt(2));1060return sb;1061}10621063/**1064* Creates a string representation of this <tt>Date</tt> object in an1065* implementation-dependent form. The intent is that the form should1066* be familiar to the user of the Java application, wherever it may1067* happen to be running. The intent is comparable to that of the1068* "<code>%c</code>" format supported by the <code>strftime()</code>1069* function of ISO C.1070*1071* @return a string representation of this date, using the locale1072* conventions.1073* @see java.text.DateFormat1074* @see java.util.Date#toString()1075* @see java.util.Date#toGMTString()1076* @deprecated As of JDK version 1.1,1077* replaced by <code>DateFormat.format(Date date)</code>.1078*/1079@Deprecated1080public String toLocaleString() {1081DateFormat formatter = DateFormat.getDateTimeInstance();1082return formatter.format(this);1083}10841085/**1086* Creates a string representation of this <tt>Date</tt> object of1087* the form:1088* <blockquote><pre>1089* d mon yyyy hh:mm:ss GMT</pre></blockquote>1090* where:<ul>1091* <li><i>d</i> is the day of the month (<tt>1</tt> through <tt>31</tt>),1092* as one or two decimal digits.1093* <li><i>mon</i> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun, Jul,1094* Aug, Sep, Oct, Nov, Dec</tt>).1095* <li><i>yyyy</i> is the year, as four decimal digits.1096* <li><i>hh</i> is the hour of the day (<tt>00</tt> through <tt>23</tt>),1097* as two decimal digits.1098* <li><i>mm</i> is the minute within the hour (<tt>00</tt> through1099* <tt>59</tt>), as two decimal digits.1100* <li><i>ss</i> is the second within the minute (<tt>00</tt> through1101* <tt>61</tt>), as two decimal digits.1102* <li><i>GMT</i> is exactly the ASCII letters "<tt>GMT</tt>" to indicate1103* Greenwich Mean Time.1104* </ul><p>1105* The result does not depend on the local time zone.1106*1107* @return a string representation of this date, using the Internet GMT1108* conventions.1109* @see java.text.DateFormat1110* @see java.util.Date#toString()1111* @see java.util.Date#toLocaleString()1112* @deprecated As of JDK version 1.1,1113* replaced by <code>DateFormat.format(Date date)</code>, using a1114* GMT <code>TimeZone</code>.1115*/1116@Deprecated1117public String toGMTString() {1118// d MMM yyyy HH:mm:ss 'GMT'1119long t = getTime();1120BaseCalendar cal = getCalendarSystem(t);1121BaseCalendar.Date date =1122(BaseCalendar.Date) cal.getCalendarDate(getTime(), (TimeZone)null);1123StringBuilder sb = new StringBuilder(32);1124CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 1).append(' '); // d1125convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); // MMM1126sb.append(date.getYear()).append(' '); // yyyy1127CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); // HH1128CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm1129CalendarUtils.sprintf0d(sb, date.getSeconds(), 2); // ss1130sb.append(" GMT"); // ' GMT'1131return sb.toString();1132}11331134/**1135* Returns the offset, measured in minutes, for the local time zone1136* relative to UTC that is appropriate for the time represented by1137* this <code>Date</code> object.1138* <p>1139* For example, in Massachusetts, five time zones west of Greenwich:1140* <blockquote><pre>1141* new Date(96, 1, 14).getTimezoneOffset() returns 300</pre></blockquote>1142* because on February 14, 1996, standard time (Eastern Standard Time)1143* is in use, which is offset five hours from UTC; but:1144* <blockquote><pre>1145* new Date(96, 5, 1).getTimezoneOffset() returns 240</pre></blockquote>1146* because on June 1, 1996, daylight saving time (Eastern Daylight Time)1147* is in use, which is offset only four hours from UTC.<p>1148* This method produces the same result as if it computed:1149* <blockquote><pre>1150* (this.getTime() - UTC(this.getYear(),1151* this.getMonth(),1152* this.getDate(),1153* this.getHours(),1154* this.getMinutes(),1155* this.getSeconds())) / (60 * 1000)1156* </pre></blockquote>1157*1158* @return the time-zone offset, in minutes, for the current time zone.1159* @see java.util.Calendar#ZONE_OFFSET1160* @see java.util.Calendar#DST_OFFSET1161* @see java.util.TimeZone#getDefault1162* @deprecated As of JDK version 1.1,1163* replaced by <code>-(Calendar.get(Calendar.ZONE_OFFSET) +1164* Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000)</code>.1165*/1166@Deprecated1167public int getTimezoneOffset() {1168int zoneOffset;1169if (cdate == null) {1170TimeZone tz = TimeZone.getDefaultRef();1171if (tz instanceof ZoneInfo) {1172zoneOffset = ((ZoneInfo)tz).getOffsets(fastTime, null);1173} else {1174zoneOffset = tz.getOffset(fastTime);1175}1176} else {1177normalize();1178zoneOffset = cdate.getZoneOffset();1179}1180return -zoneOffset/60000; // convert to minutes1181}11821183private final BaseCalendar.Date getCalendarDate() {1184if (cdate == null) {1185BaseCalendar cal = getCalendarSystem(fastTime);1186cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime,1187TimeZone.getDefaultRef());1188}1189return cdate;1190}11911192private final BaseCalendar.Date normalize() {1193if (cdate == null) {1194BaseCalendar cal = getCalendarSystem(fastTime);1195cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime,1196TimeZone.getDefaultRef());1197return cdate;1198}11991200// Normalize cdate with the TimeZone in cdate first. This is1201// required for the compatible behavior.1202if (!cdate.isNormalized()) {1203cdate = normalize(cdate);1204}12051206// If the default TimeZone has changed, then recalculate the1207// fields with the new TimeZone.1208TimeZone tz = TimeZone.getDefaultRef();1209if (tz != cdate.getZone()) {1210cdate.setZone(tz);1211CalendarSystem cal = getCalendarSystem(cdate);1212cal.getCalendarDate(fastTime, cdate);1213}1214return cdate;1215}12161217// fastTime and the returned data are in sync upon return.1218private final BaseCalendar.Date normalize(BaseCalendar.Date date) {1219int y = date.getNormalizedYear();1220int m = date.getMonth();1221int d = date.getDayOfMonth();1222int hh = date.getHours();1223int mm = date.getMinutes();1224int ss = date.getSeconds();1225int ms = date.getMillis();1226TimeZone tz = date.getZone();12271228// If the specified year can't be handled using a long value1229// in milliseconds, GregorianCalendar is used for full1230// compatibility with underflow and overflow. This is required1231// by some JCK tests. The limits are based max year values -1232// years that can be represented by max values of d, hh, mm,1233// ss and ms. Also, let GregorianCalendar handle the default1234// cutover year so that we don't need to worry about the1235// transition here.1236if (y == 1582 || y > 280000000 || y < -280000000) {1237if (tz == null) {1238tz = TimeZone.getTimeZone("GMT");1239}1240GregorianCalendar gc = new GregorianCalendar(tz);1241gc.clear();1242gc.set(GregorianCalendar.MILLISECOND, ms);1243gc.set(y, m-1, d, hh, mm, ss);1244fastTime = gc.getTimeInMillis();1245BaseCalendar cal = getCalendarSystem(fastTime);1246date = (BaseCalendar.Date) cal.getCalendarDate(fastTime, tz);1247return date;1248}12491250BaseCalendar cal = getCalendarSystem(y);1251if (cal != getCalendarSystem(date)) {1252date = (BaseCalendar.Date) cal.newCalendarDate(tz);1253date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);1254}1255// Perform the GregorianCalendar-style normalization.1256fastTime = cal.getTime(date);12571258// In case the normalized date requires the other calendar1259// system, we need to recalculate it using the other one.1260BaseCalendar ncal = getCalendarSystem(fastTime);1261if (ncal != cal) {1262date = (BaseCalendar.Date) ncal.newCalendarDate(tz);1263date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);1264fastTime = ncal.getTime(date);1265}1266return date;1267}12681269/**1270* Returns the Gregorian or Julian calendar system to use with the1271* given date. Use Gregorian from October 15, 1582.1272*1273* @param year normalized calendar year (not -1900)1274* @return the CalendarSystem to use for the specified date1275*/1276private static final BaseCalendar getCalendarSystem(int year) {1277if (year >= 1582) {1278return gcal;1279}1280return getJulianCalendar();1281}12821283private static final BaseCalendar getCalendarSystem(long utc) {1284// Quickly check if the time stamp given by `utc' is the Epoch1285// or later. If it's before 1970, we convert the cutover to1286// local time to compare.1287if (utc >= 01288|| utc >= GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER1289- TimeZone.getDefaultRef().getOffset(utc)) {1290return gcal;1291}1292return getJulianCalendar();1293}12941295private static final BaseCalendar getCalendarSystem(BaseCalendar.Date cdate) {1296if (jcal == null) {1297return gcal;1298}1299if (cdate.getEra() != null) {1300return jcal;1301}1302return gcal;1303}13041305synchronized private static final BaseCalendar getJulianCalendar() {1306if (jcal == null) {1307jcal = (BaseCalendar) CalendarSystem.forName("julian");1308}1309return jcal;1310}13111312/**1313* Save the state of this object to a stream (i.e., serialize it).1314*1315* @serialData The value returned by <code>getTime()</code>1316* is emitted (long). This represents the offset from1317* January 1, 1970, 00:00:00 GMT in milliseconds.1318*/1319private void writeObject(ObjectOutputStream s)1320throws IOException1321{1322s.writeLong(getTimeImpl());1323}13241325/**1326* Reconstitute this object from a stream (i.e., deserialize it).1327*/1328private void readObject(ObjectInputStream s)1329throws IOException, ClassNotFoundException1330{1331fastTime = s.readLong();1332}13331334/**1335* Obtains an instance of {@code Date} from an {@code Instant} object.1336* <p>1337* {@code Instant} uses a precision of nanoseconds, whereas {@code Date}1338* uses a precision of milliseconds. The conversion will trancate any1339* excess precision information as though the amount in nanoseconds was1340* subject to integer division by one million.1341* <p>1342* {@code Instant} can store points on the time-line further in the future1343* and further in the past than {@code Date}. In this scenario, this method1344* will throw an exception.1345*1346* @param instant the instant to convert1347* @return a {@code Date} representing the same point on the time-line as1348* the provided instant1349* @exception NullPointerException if {@code instant} is null.1350* @exception IllegalArgumentException if the instant is too large to1351* represent as a {@code Date}1352* @since 1.81353*/1354public static Date from(Instant instant) {1355try {1356return new Date(instant.toEpochMilli());1357} catch (ArithmeticException ex) {1358throw new IllegalArgumentException(ex);1359}1360}13611362/**1363* Converts this {@code Date} object to an {@code Instant}.1364* <p>1365* The conversion creates an {@code Instant} that represents the same1366* point on the time-line as this {@code Date}.1367*1368* @return an instant representing the same point on the time-line as1369* this {@code Date} object1370* @since 1.81371*/1372public Instant toInstant() {1373return Instant.ofEpochMilli(getTime());1374}1375}137613771378