|
JavaTM 2 Platform Standard Ed. 6 |
|||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
java.lang.Objectjava.util.Date
public class Date
類別 Date 表示特定的瞬間,精確到毫秒。
在 JDK 1.1 之前,類別 Date 有兩個其他的函數。它允許把日期解釋為年、月、日、小時、分鐘和秒值。它也允許格式化和解析日期字元串。不過,這些函數的 API 不易於實作國際化。從 JDK 1.1 開始,應該使用 Calendar 類別實作日期和時間欄位之間轉換,使用 DateFormat 類別來格式化和解析日期字元串。Date 中的相應方法已廢棄。
儘管 Date 類別打算反映協調世界時 (UTC),但無法做到如此準確,這取決於 Java 虛擬機器的主機環境。當前幾乎所有作業系統都假定 1 天 = 24 × 60 × 60 = 86400 秒。但對於 UTC,大約每一兩年出現一次額外的一秒,稱為“閏秒”。閏秒始終作為當天的最後一秒增加,並且始終在 12 月 31 日或 6 月 30 日增加。例如,1995 年的最後一分鐘是 61 秒,因為增加了閏秒。大多數電腦時鐘不是特別的準確,因此不能反映閏秒的差別。
一些電腦標準是按照格林威治標準時 (GMT) 定義的,格林威治標準時和世界時 (UT) 是相等的。GMT 是標準的“民間”名稱;UT 是相同標準的“科學”名稱。UTC 和 UT 的區別是:UTC 是基於原子時鐘的,UT 是基於天體觀察的,兩者在實際應用中難分軒輊。因為地球的旋轉不是均勻的(它以複雜的方式減速和加速),所以 UT 始終不是均勻地串流過。閏秒是根據需要引入 UTC 的,以便把 UTC 保持在 UT1 的 0.9 秒之內,UT1 是應用了某些更正的 UT 版本。還有其他的時間和日期系統;例如,基於衛星的全球定位系統 (GPS) 使用的時間刻度與 UTC 同步,但沒有 針對閏秒進行調整。有關更多資訊的一個有趣來源是美國海軍天文台,特別是 Directorate of Time 的網址:
http://tycho.usno.navy.mil
還有它們對 "Systems of Time" 的定義,網址為:
http://tycho.usno.navy.mil/systime.html
在類別 Date 所有可以接受或返回年、月、日期、小時、分鐘和秒值的方法中,將使用下面的表示形式:
- 1900 表示。
在所有情形中,針對這些目的賦予方法的參數不需要在指定的範圍內;例如,可以把日期指定為 1 月 32 日,並把它解釋為 2 月 1 日的相同含義。
DateFormat,
Calendar,
TimeZone,
序列化表格| 建構子摘要 | |
|---|---|
Date()
分派 Date 物件並初始化此物件,以表示分派它的時間(精確到毫秒)。 |
|
Date(int year,
int month,
int date)
已過時。 從 JDK 1.1 開始,由 Calendar.set(year + 1900, month, date) 或 GregorianCalendar(year + 1900, month, date) 取代。 |
|
Date(int year,
int month,
int date,
int hrs,
int min)
已過時。 從 JDK 1.1 開始,由 Calendar.set(year + 1900, month, date, hrs, min) 或 GregorianCalendar(year + 1900, month, date, hrs, min) 取代。 |
|
Date(int year,
int month,
int date,
int hrs,
int min,
int sec)
已過時。 從 JDK 1.1 開始,由 Calendar.set(year + 1900, month, date, hrs, min, sec) 或 GregorianCalendar(year + 1900, month, date, hrs, min, sec) 取代。 |
|
Date(long date)
分派 Date 物件並初始化此物件,以表示自從標準基準時間(稱為“歷元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以來的指定毫秒數。 |
|
Date(String s)
已過時。 從 JDK 1.1 開始,由 DateFormat.parse(String s) 取代。 |
|
| 方法摘要 | |
|---|---|
boolean |
after(Date when)
測試此日期是否在指定日期之後。 |
boolean |
before(Date when)
測試此日期是否在指定日期之前。 |
Object |
clone()
返回此物件的副本。 |
int |
compareTo(Date anotherDate)
比較兩個日期的順序。 |
boolean |
equals(Object obj)
比較兩個日期的相等性。 |
int |
getDate()
已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.DAY_OF_MONTH) 取代。 |
int |
getDay()
已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.DAY_OF_WEEK) 取代。 |
int |
getHours()
已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.HOUR_OF_DAY) 取代。 |
int |
getMinutes()
已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.MINUTE) 取代。 |
int |
getMonth()
已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.MONTH) 取代。 |
int |
getSeconds()
已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.SECOND) 取代。 |
long |
getTime()
返回自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 物件表示的毫秒數。 |
int |
getTimezoneOffset()
已過時。 從 JDK 1.1 開始,由 -(Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000) 取代。 |
int |
getYear()
已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.YEAR) - 1900 取代。 |
int |
hashCode()
返回此物件的雜湊碼值。 |
static long |
parse(String s)
已過時。 從 JDK 1.1 開始,由 DateFormat.parse(String s) 取代。 |
void |
setDate(int date)
已過時。 從 JDK 1.1 開始,由 Calendar.set(Calendar.DAY_OF_MONTH, int date) 取代。 |
void |
setHours(int hours)
已過時。 從 JDK 1.1 開始,由 Calendar.set(Calendar.HOUR_OF_DAY, int hours) 取代。 |
void |
setMinutes(int minutes)
已過時。 從 JDK 1.1 開始,由 Calendar.set(Calendar.MINUTE, int minutes) 取代。 |
void |
setMonth(int month)
已過時。 從 JDK 1.1 開始,由 Calendar.set(Calendar.MONTH, int month) 取代。 |
void |
setSeconds(int seconds)
已過時。 從 JDK 1.1 開始,由 Calendar.set(Calendar.SECOND, int seconds) 取代。 |
void |
setTime(long time)
設置此 Date 物件,以表示 1970 年 1 月 1 日 00:00:00 GMT 以後 time 毫秒的時間點。 |
void |
setYear(int year)
已過時。 從 JDK 1.1 開始,由 Calendar.set(Calendar.YEAR, year + 1900) 取代。 |
String |
toGMTString()
已過時。 從 JDK 1.1 開始,由 DateFormat.format(Date date) 取代,使用 GMT TimeZone。 |
String |
toLocaleString()
已過時。 從 JDK 1.1 開始,由 DateFormat.format(Date date) 取代。 |
String |
toString()
把此 Date 物件轉換為以下形式的 String:
dow mon dd hh:mm:ss zzz yyyy
其中:
dow 是一周中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat)。 |
static long |
UTC(int year,
int month,
int date,
int hrs,
int min,
int sec)
已過時。 從 JDK 1.1 開始,由 Calendar.set(year + 1900, month, date, hrs, min, sec) 或 GregorianCalendar(year + 1900, month, date, hrs, min, sec) 取代,使用 UTC TimeZone,後跟 Calendar.getTime().getTime()。 |
| 從類別 java.lang.Object 繼承的方法 |
|---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
| 建構子詳細資訊 |
|---|
public Date()
Date 物件並初始化此物件,以表示分派它的時間(精確到毫秒)。
System.currentTimeMillis()public Date(long date)
Date 物件並初始化此物件,以表示自從標準基準時間(稱為“歷元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以來的指定毫秒數。
date - 自 1970 年 1 月 1 日 00:00:00 GMT 以來的毫秒數。System.currentTimeMillis()
@Deprecated
public Date(int year,
int month,
int date)
Calendar.set(year + 1900, month, date) 或 GregorianCalendar(year + 1900, month, date) 取代。
Date 物件並初始化此物件,以表示由 year、month 和 date 參數指定的一天的開始時間(本地時間晚上 12 時)。
year - 減 1900 的年份。month - 0-11 的月份。date - 一月中 1-31 之間的某一天。Calendar
@Deprecated
public Date(int year,
int month,
int date,
int hrs,
int min)
Calendar.set(year + 1900, month, date, hrs, min) 或 GregorianCalendar(year + 1900, month, date, hrs, min) 取代。
Date 物件並初始化此物件,以表示本地時區中由 year、month、date、hrs 和 min 參數指定的分鐘的開始瞬間。
year - 減 1900 的年份。month - 0-11 之間的月份。date - 一月中 1-31 之間的某一天。hrs - 0-23 之間的小時數。min - 0-59 之間的分鐘數。Calendar
@Deprecated
public Date(int year,
int month,
int date,
int hrs,
int min,
int sec)
Calendar.set(year + 1900, month, date, hrs, min, sec) 或 GregorianCalendar(year + 1900, month, date, hrs, min, sec) 取代。
Date 物件,並初始化此物件,以表示本地時區中由 year、month、date、hrs、min 和 sec 參數指定的秒的開始瞬間。
year - 減 1900 的年份。month - 0-11 之間的月份。date - 一月中 1-31 之間的某一天。hrs - 0-23 之間的小時數。min - 0-59 之間的分鐘數。sec - 0-59 之間的秒數。Calendar@Deprecated public Date(String s)
DateFormat.parse(String s) 取代。
Date 物件並初始化此物件,以表示字元串 s 指出的日期和時間,就像通過 parse(java.lang.String) 方法解釋一樣。
s - 日期的字元串表示形式。DateFormat,
parse(java.lang.String)| 方法詳細資訊 |
|---|
public Object clone()
Object 中的 cloneCloneable
@Deprecated
public static long UTC(int year,
int month,
int date,
int hrs,
int min,
int sec)
Calendar.set(year + 1900, month, date, hrs, min, sec) 或 GregorianCalendar(year + 1900, month, date, hrs, min, sec) 取代,使用 UTC TimeZone,後跟 Calendar.getTime().getTime()。
year - 減 1900 的年份。month - 0-11 之間的月份。date - 一月中 1-31 之間的某一天。hrs - 0-23 之間的小時數。min - 0-59 之間的分鐘數。sec - 0-59 之間的秒數。
Calendar@Deprecated public static long parse(String s)
DateFormat.parse(String s) 取代。
它接受許多語法;特別是它識別 IETF 標準日期語法:"Sat, 12 Aug 1995 13:30:00 GMT"。它也瞭解美國大陸時區縮寫,但對於一般用途,應該使用時區偏移量:"Sat, 12 Aug 1995 13:30:00 GMT+0430"(比格林威治子午線晚 4 小時 30 分)。如果沒有指定時區,則假定用本地時區。GMT 和 UTC 被認為是相同的。
系統將從左到右處理字元串 s,從中尋找相應的資料。s 中包括在 ASCII 括號字元 ( 和 ) 中的任何內容都將被忽略。括號可以進行巢狀。另外,s 中只允許使用以下 ASCII 字元:
以及空白字元。abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789,+-:/
連續的十進制位序列被當成十進制數:
SimpleDateFormat 中使用的小於 100 的年份具有稍微不同的解釋。
連續的字母序列被認為是單詞,並按以下方法進行處理:
一旦掃瞄了整個字元串,就以兩種方式之一把它轉換成時間結果。如果已經識別時區或時區偏移量,那麼年、月、月中某一天、小時、分鐘和秒以 UTC 進行解釋,然後應用時區偏移量。否則,年、月、月中某一天、小時、分鐘和秒用本地時區進行解釋。
s - 要解析為日期的字元串。
DateFormat@Deprecated public int getYear()
Calendar.get(Calendar.YEAR) - 1900 取代。
Date 物件表示的瞬間的年份減去 1900 的結果(用本地時區進行解釋)。
Calendar@Deprecated public void setYear(int year)
Calendar.set(Calendar.YEAR, year + 1900) 取代。
Date 物件,以便它用與以前相同的月份、日期、小時、分鐘和秒表示指定年份中的時間點(用本地時區進行解釋)。(當然,例如,如果日期是 2 月 29 日,並且年份被設置到非閏年,那麼新日期將被看作 3 月 1 日。)
year - 年份值。Calendar@Deprecated public int getMonth()
Calendar.get(Calendar.MONTH) 取代。
0 和 11 之間,值 0 表示 1 月。
Calendar@Deprecated public void setMonth(int month)
Calendar.set(Calendar.MONTH, int month) 取代。
month - 0-11 之間的月份值。Calendar@Deprecated public int getDate()
Calendar.get(Calendar.DAY_OF_MONTH) 取代。
1 和 31 之間,表示包含或開始於此 Date 物件表示的時間的月份中的某一天(用本地時區進行解釋)。
Calendar@Deprecated public void setDate(int date)
Calendar.set(Calendar.DAY_OF_MONTH, int date) 取代。
date - 月份中 1-31 之間的某一天。Calendar@Deprecated public int getDay()
Calendar.get(Calendar.DAY_OF_WEEK) 取代。
Calendar@Deprecated public int getHours()
Calendar.get(Calendar.HOUR_OF_DAY) 取代。
Calendar@Deprecated public void setHours(int hours)
Calendar.set(Calendar.HOUR_OF_DAY, int hours) 取代。
hours - 小時值。Calendar@Deprecated public int getMinutes()
Calendar.get(Calendar.MINUTE) 取代。
0 和 59 之間。
Calendar@Deprecated public void setMinutes(int minutes)
Calendar.set(Calendar.MINUTE, int minutes) 取代。
minutes - 分鐘值。Calendar@Deprecated public int getSeconds()
Calendar.get(Calendar.SECOND) 取代。
0 和 61 之間。值 60 和 61 只可能發生在考慮了閏秒的 Java 虛擬機器上。
Calendar@Deprecated public void setSeconds(int seconds)
Calendar.set(Calendar.SECOND, int seconds) 取代。
seconds - 秒數值。Calendarpublic long getTime()
public void setTime(long time)
Date 物件,以表示 1970 年 1 月 1 日 00:00:00 GMT 以後 time 毫秒的時間點。
time - 毫秒數。public boolean before(Date when)
when - 日期。
true;否則返回 false。
NullPointerException - 如果 when 為 null。public boolean after(Date when)
when - 日期。
true;否則返回 false。
NullPointerException - 如果 when 為 null。public boolean equals(Object obj)
null,並且是一個表示與此物件相同的時間點(到毫秒)的 Date 物件時,結果才為 true。
因此,當且僅當 getTime 方法對於兩個 Date 物件返回相同的 long 值時,這兩個物件才是相等的。
Object 中的 equalsobj - 要與之比較的物件。
true;否則,返回 false。getTime()public int compareTo(Date anotherDate)
Comparable<Date> 中的 compareToanotherDate - 要比較的 Date。
0;如果此 Date 在 Date 參數之前,則返回小於 0 的值;如果此 Date 在 Date 參數之後,則返回大於 0 的值。
NullPointerException - 如果 anotherDate 為 null。public int hashCode()
getTime() 方法返回的基本 long 值的兩部分的異或。也就是說,雜湊碼就是以下表達式的值:
(int)(this.getTime()^(this.getTime() >>> 32))
Object 中的 hashCodeObject.equals(java.lang.Object),
Hashtablepublic String toString()
Date 物件轉換為以下形式的 String:
其中:dow mon dd hh:mm:ss zzz yyyy
Object 中的 toStringtoLocaleString(),
toGMTString()@Deprecated public String toLocaleString()
DateFormat.format(Date date) 取代。
strftime() 函數支持的 "%c" 格式。
DateFormat,
toString(),
toGMTString()@Deprecated public String toGMTString()
DateFormat.format(Date date) 取代,使用 GMT TimeZone。
d mon yyyy hh:mm:ss GMT其中:
結果不依賴於本地時區。
DateFormat,
toString(),
toLocaleString()@Deprecated public int getTimezoneOffset()
-(Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000) 取代。
Date 物件表示的時間)的本地時區的偏移量(以分鐘為單位)。
例如,在馬薩諸塞州,比格林威治時間晚 5 個時區:
因為在 1996 年 2 月 14 日,使用標準時間(東部標準時間),此時間和 UTC 有 5 小時的偏移量;但是:new Date(96, 1, 14).getTimezoneOffset() 返回 300
因為在 1996 年 6 月 1 日,使用夏令時(東部日光時間),此時間和 UTC 只有 4 小時的偏移量。new Date(96, 5, 1).getTimezoneOffset() 返回 240
此方法產生與以下計算方式相同的結果:
(this.getTime() - UTC(this.getYear(),
this.getMonth(),
this.getDate(),
this.getHours(),
this.getMinutes(),
this.getSeconds())) / (60 * 1000)
Calendar.ZONE_OFFSET,
Calendar.DST_OFFSET,
TimeZone.getDefault()
|
JavaTM 2 Platform Standard Ed. 6 |
|||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
版權所有 2008 Sun Microsystems, Inc. 保留所有權利。請遵守GNU General Public License, version 2 only。