📄 javautil.doc2.html
字号:
<html>
<head>
<title>The Java Language Specification The Package java.util</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="javautil.doc1.html">Prev</a> | <a href="javautil.doc3.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="7433"></a>
<center><h1>21.3 The Class <code>java.util.Date</code></h1></center>
<a name="8613"></a>
The class <code>Date</code> provides a system-independent abstraction of dates and times, to a
millisecond precision. Dates may be constructed from a year, month, date (day of
month), hour, minute, and second; those six components and the day of the week,
may be extracted; and dates may be compared and converted to a readable string.
<p><pre><a name="7434"></a>public class <code><b>Date</b></code> {
<a name="7435"></a> public <code><b>Date</b></code>();
<a name="7436"></a> public <code><b>Date</b></code>(long time);
<a name="7437"></a> public <code><b>Date</b></code>(int year, int month, int date);
<a name="7438"></a> public <code><b>Date</b></code>(int year, int month, int date,
<a name="25071"></a> int hours, int minutes);
<a name="7439"></a> public <code><b>Date</b></code>(int year, int month, int date,
<a name="20368"></a> int hours, int minutes, int seconds);
<a name="7440"></a> public <code><b>Date</b></code>(String s) throws IllegalArgumentException;
<a name="8534"></a> public String <code><b>toString</b></code>();
<a name="8535"></a> public boolean <code><b>equals</b></code>(Object obj);
<a name="8536"></a> public int <code><b>hashCode</b></code>();
<a name="7443"></a> public int <code><b>getYear</b></code>();
<a name="7444"></a> public void <code><b>setYear</b></code>(int year);
<a name="7445"></a> public int <code><b>getMonth</b></code>();
<a name="7446"></a> public void <code><b>setMonth</b></code>(int month);
<a name="7447"></a> public int <code><b>getDate</b></code>();
<a name="7448"></a> public void <code><b>setDate</b></code>(int date);
<a name="7449"></a> public int <code><b>getDay</b></code>();
<a name="7450"></a> public int <code><b>getHours</b></code>();
<a name="7451"></a> public void <code><b>setHours</b></code>(int hours);
<a name="7452"></a> public int <code><b>getMinutes</b></code>();
<a name="7453"></a> public void <code><b>setMinutes</b></code>(int minutes);
<a name="7454"></a> public int <code><b>getSeconds</b></code>();
<a name="7455"></a> public void <code><b>setSeconds</b></code>(int seconds);
<a name="7456"></a> public long <code><b>getTime</b></code>();
<a name="7457"></a> public void <code><b>setTime</b></code>(long time);
<a name="7458"></a> public boolean <code><b>before</b></code>(Date when);
<a name="7459"></a> public boolean <code><b>after</b></code>(Date when);
<a name="7463"></a> public String <code><b>toLocaleString</b></code>();
<a name="7464"></a> public String <code><b>toGMTString</b></code>();
<a name="7465"></a> public int <code><b>getTimezoneOffset</b></code>();
<a name="8542"></a> public static long <code><b>UTC</b></code>(int year, int month, int date,
<a name="20367"></a> int hours, int minutes, int seconds);
<a name="25066"></a> public static long <code><b>parse</b></code>(String s)
<a name="25067"></a> throws IllegalArgumentException;
<a name="25068"></a>}
</pre><a name="8657"></a>
Examples:
<p><ul><a name="25556"></a>
<li>To print today's date:
<a name="8662"></a> System.out.println("today = " + new Date());
<a name="8648"></a>
<li>To find out the day of the week for some particular date, for example, January 16, 1963:
<a name="8650"></a> new Date(63, 0, 16).getDay()
</ul><a name="8735"></a>
While the <code>Date</code> class is intended to reflect UTC (Coordinated Universal Time), it may not do so exactly, depending on the host environment of the Java system. Nearly all modern operating systems assume that 1 day = <img src="javautil.doc.anc2.gif"> = 86400 seconds in all cases. In UTC, however, about once every year or two there is an extra second, called a "leap second." The leap second is always added as the last second of the day, and nearly always on December 31 or June 30. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second.<p>
<a name="23606"></a>
Most computer clocks are currently not accurate enough to be able to reflect the leap-second distinction. Some computer standards are defined in terms of GMT (Greenwich Mean Time), which is equivalent to UT (Universal Time). GMT is the "civil" name for the standard; UT is the "scientific" name for the same standard. The distinction between UTC and UT is that UTC is based on an atomic clock and UT is based on astronomical observations, which for all practical purposes is an invisibly fine hair to split. Because the earth's rotation is not uniform-it slows down and speeds up in complicated ways-UT does not always flow uniformly. Leap seconds are introduced as needed into UTC so as to keep UTC within 0.9 seconds of UT1, which is a version of UT with certain corrections applied. There are other time and date systems as well; for example, the time scale used by GPS (the satellite-based Global Positioning System) is synchronized to UTC but is <i>not</i> adjusted for leap seconds. An interesting source of further information is the U. S. Naval Observatory, particularly the Directorate of Time at:<p>
<pre><a name="8743"></a>http://tycho.usno.navy.mil
</pre><a name="8746"></a>
and their definitions of "Systems of Time" at:<p>
<pre><a name="8747"></a>http://tycho.usno.navy.mil/systime.html
</pre><a name="8643"></a>
In all methods of class <code>Date</code> that accept or return year, month, day of month, hours, minutes, and seconds values, the following representations are used:<p>
<ul><a name="8787"></a>
<li>A year <i>y</i> is represented by the integer <img src="javautil.doc.anc3.gif">.
<a name="8795"></a>
<li>A month is represented by an integer form 0 to 11; 0 is January, 1 is February, and so on; thus 11 is December.
<a name="8799"></a>
<li>A date (day of month) is represented by an integer from 1 to 31 in the usual manner.
<a name="8800"></a>
<li>An hour is represented by an integer from 0 to 23. Thus the hour from midnight to 1 AM is hour 0, and the hour from noon to 1 PM is hour 12.
<a name="8801"></a>
<li>A minute is represented by an integer from 0 to 59 in the usual manner.
<a name="8802"></a>
<li>A second is represented by an integer from 0 to 61. The values 60 and 61 will occur only for leap seconds, and even then only in Java implementations that actually track leap seconds correctly. Because of the manner in which leap seconds are currently introduced, it is extremely unlikely that two leap seconds will occur in the same minute, but this specification follows the date and time conventions for ISO C.
</ul><a name="8803"></a>
In all cases, arguments given to methods for these purposes need not fall within
the indicated ranges; for example, a date may be specified as January 32 and will
be interpreted as meaning February 1.
<p><a name="7467"></a>
<p><font size=+1><strong>21.3.1 </strong> <code>public <code><b>Date</b></code>()</code></font>
<p>
<a name="8754"></a>
This constructor initializes a newly created <code>Date</code> object so that it represents the
instant of time that it was created, measured to the nearest millisecond.
<p><a name="7468"></a>
<p><font size=+1><strong>21.3.2 </strong> <code>public <code><b>Date</b></code>(long time)</code></font>
<p>
<a name="8772"></a>
This constructor initializes a newly created <code>Date</code> object so that it represents the
instant of time that is <code>time</code> milliseconds after the standard base time known as
"the epoch," namely 00:00:00 GMT on January 1, 1970. See also the method
<code>currentTimeMillis</code> <a href="javalang.doc17.html#5984">(§20.18.6)</a> of class <code>System</code>.
<p><a name="7469"></a>
<p><font size=+1><strong>21.3.3 </strong> <code>public <code><b>Date</b></code>(int year, int month, int date)</code></font>
<p>
<a name="8804"></a>
This constructor initializes a newly created <code>Date</code> object so that it represents midnight
at the beginning of the day specified by the <code>year</code>, <code>month</code>, and <code>date</code> arguments,
in the local time zone. Thus, it has the same effect as the constructor call
<a href="javautil.doc2.html#7471">(§21.3.5)</a>:
<p><pre><a name="8878"></a><code>Date(year, month, date, 0, 0, 0)</code>
</pre><a name="7470"></a>
<p><font size=+1><strong>21.3.4 </strong> <code>public <code><b>Date</b></code>(int year, int month, int date,<br>      int hours, int minutes)</code></font>
<p>
<a name="8820"></a>
This constructor initializes a newly created <code>Date</code> object so that it represents the
instant at the start of the minute specified by the <code>year</code>, <code>month</code>, <code>date</code>, <code>hours</code>, and
<code>minutes</code> arguments, in the local time zone. Thus, it has the same effect as the constructor
call <a href="javautil.doc2.html#7471">(§21.3.5)</a>:
<p><pre><a name="8854"></a><code>Date(year, month, date, hours, minutes, 0)
</code></pre><a name="7471"></a>
<p><font size=+1><strong>21.3.5 </strong> <code>public <code><b>Date</b></code>(int year, int month, int date,<br>      int hours, int minutes, int seconds)</code></font>
<p>
<a name="8845"></a>
This constructor initializes a newly created <code>Date</code> object so that it represents the
instant at the start of the second specified by the <code>year</code>, <code>month</code>, <code>date</code>, <code>hours</code>,
<code>minutes</code>,  and <code>seconds</code> arguments, in the local time zone.
<p><a name="8901"></a>
<p><font size=+1><strong>21.3.6 </strong> <code>public <code><b>Date</b></code>(String s)<br>throws IllegalArgumentException</code></font>
<p>
<a name="8906"></a>
This constructor initializes a newly created <code>Date</code> object so that it represents the
date and time indicated by the string s, which is interpreted as if by the <code>parse</code>
method <a href="javautil.doc2.html#21075">(§21.3.31)</a>.
<p><a name="8902"></a>
<p><font size=+1><strong>21.3.7 </strong> <code>public String <code><b>toString</b></code>()</code></font>
<p>
<a name="8915"></a>
This <code>Date</code> object is converted to a <code>String</code> of the form:
<p><pre><a name="20387"></a>"dow mon dd hh:mm:ss zzz yyyy"
</pre><a name="20391"></a>
where:<p>
<ul><a name="20401"></a>
<li><code>dow</code> is the day of the week (<code>Sun</code>, <code>Mon</code>, <code>Tue</code>, <code>Wed</code>, <code>Thu</code>, <code>Fri</code>, <code>Sat</code>).
<a name="20405"></a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -