日期和時(shí)間的處理不僅在面試題中會(huì)考到,在實(shí)際項(xiàng)目開(kāi)發(fā)中也是我們經(jīng)常需要處理的問(wèn)題,似乎沒(méi)有哪個(gè)項(xiàng)目可以避開(kāi)它們,我們常常在處理用戶的出生年月日、注冊(cè)日期,訂單的創(chuàng)建時(shí)間等屬性時(shí)用到,由此可見(jiàn)其重要性。
java.util.Date類
提到日期和時(shí)間,我想大家最先想到應(yīng)該是java.util.Date類吧。Date類可以精確到毫秒數(shù),這個(gè)毫秒數(shù)是相對(duì)于格林威治標(biāo)準(zhǔn)時(shí)間“1970-01-01 00:00:00.000 GMT”的差值。那么,什么是格林威治標(biāo)準(zhǔn)時(shí)間呢?要回答這個(gè)問(wèn)題,我們需要先來(lái)了解一下世界時(shí)間標(biāo)準(zhǔn)方面的知識(shí)。
世界時(shí)間標(biāo)準(zhǔn)主要有UTC,即Coordinated Universal Time(中文名譯作世界協(xié)調(diào)時(shí)間、世界統(tǒng)一時(shí)間或世界標(biāo)準(zhǔn)時(shí)間),以及GMT,即Greenwich Mean Time(中文名譯作格林威治標(biāo)準(zhǔn)時(shí)間或格林威治平均時(shí)間)兩種。嚴(yán)格來(lái)講,UTC比GMT更加精確一些,不過(guò)它們的差值不會(huì)超過(guò)0.9秒,如果超過(guò)了,將會(huì)為UTC增加閏秒以與GMT,也就是地球自轉(zhuǎn)周期保持一致。所以在日常使用中,我們可以把UTC和GMT一樣看待。
日期和時(shí)間的表示是與我們所處的時(shí)區(qū)相關(guān)聯(lián)的,如果我們不指定時(shí)區(qū),那么它們將以系統(tǒng)默認(rèn)的時(shí)區(qū)來(lái)顯示。我們先來(lái)看看如何創(chuàng)建日期對(duì)象。Date類有很多個(gè)構(gòu)造器方法,大部分已經(jīng)不被贊成使用了(Deprecated),不過(guò)還剩下兩個(gè)可以使用的:
- public ?Date()?{ ??
- ???? this (System.currentTimeMillis()); ??
- } ??
- ??
- public ?Date( long ?date)?{ ??
- ???? //other?code ??
- }??
public Date() {
this(System.currentTimeMillis());
}
public Date(long date) {
//other code
}
第一個(gè)是無(wú)參構(gòu)造器,使用系統(tǒng)當(dāng)前時(shí)間的毫秒數(shù)來(lái)創(chuàng)建Date對(duì)象,它調(diào)用了java.lang.System類的currentTimeMillis()來(lái)取得系統(tǒng)的當(dāng)前時(shí)間的毫秒值。這是個(gè)本地方法,它的定義如下:
- public ? static ? native ? long ?currentTimeMillis();??
public static native long currentTimeMillis();
第二個(gè)構(gòu)造器是根據(jù)給定的毫秒數(shù)來(lái)創(chuàng)建一個(gè)與之對(duì)應(yīng)的Date對(duì)象,這個(gè)毫秒數(shù)決定了被創(chuàng)建對(duì)象的年、月、日、時(shí)、分、秒屬性的值。
我們來(lái)看看日期和時(shí)間在默認(rèn)時(shí)區(qū)下的顯示效果:
- import ?java.util.Date; ??
- ??
- public ? class ?DateTest?{ ??
- ???? public ? static ? void ?main(String[]?args)?{ ??
- ????????Date?d?=? new ?Date(); ??
- ???????? //?在默認(rèn)時(shí)區(qū)下輸出日期和時(shí)間值 ??
- ????????System.out.println(d); ??
- ????} ??
- }??
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
Date d = new Date();
// 在默認(rèn)時(shí)區(qū)下輸出日期和時(shí)間值
System.out.println(d);
}
}
運(yùn)行結(jié)果:
- Tue Jul 22 10:44:47 CST 2008
大家應(yīng)該注意到了年份前的“CST”標(biāo)識(shí),它是China Standard Time的縮寫(xiě),指的是中國(guó)標(biāo)準(zhǔn)時(shí)間,也就是我們常說(shuō)的北京時(shí)間。它與UTC的時(shí)差是UTC+8:00,就是說(shuō)北京時(shí)間比世界標(biāo)準(zhǔn)時(shí)間早8個(gè)小時(shí),如果世界標(biāo)準(zhǔn)時(shí)間是早上1點(diǎn),北京時(shí)間就是早上9點(diǎn)。一般情況下我們不需要關(guān)心時(shí)區(qū)問(wèn)題。
在創(chuàng)建完Date對(duì)象之后,我們可以通過(guò)調(diào)用getTime()方法來(lái)獲得該對(duì)象的毫秒數(shù)值,調(diào)用setTime(long time)方法來(lái)設(shè)置它的毫秒數(shù)值,從而影響年、月、日、時(shí)、分、秒這些屬性。這兩個(gè)方法的定義如下:
- public ? long ?getTime()?{ ??
- ???? //other?code ??
- } ??
- ??
- public ? void ?setTime( long ?time)?{ ??
- ???? //other?code ??
- }??
public long getTime() {
//other code
}
public void setTime(long time) {
//other code
}
既然Date對(duì)象可以表示盛相對(duì)于“1970-01-01 00:00:00.000 GMT”的毫秒數(shù),我們自然可以通過(guò)這個(gè)值來(lái)比較兩個(gè)日期的大小了,不過(guò)對(duì)于日期來(lái)講,前后的說(shuō)法應(yīng)該更為恰當(dāng)。而Date類已經(jīng)為我們提供了這樣的方法:
- public ? boolean ?before(Date?when)?{ ??
- ???? //other?code ??
- } ??
- ??
- public ? boolean ?after(Date?when)?{ ??
- ???? //other?code ??
- } ??
- ??
- public ? int ?compareTo(Date?anotherDate)?{ ??
- ???? //other?code ??
- }??
public boolean before(Date when) {
//other code
}
public boolean after(Date when) {
//other code
}
public int compareTo(Date anotherDate) {
//other code
}
before()是判斷當(dāng)前日期是否在參數(shù)日期之前,即當(dāng)前日期毫秒數(shù)小于參數(shù)日期毫秒數(shù);after()是判斷當(dāng)前日期是否在參數(shù)日期之后,即當(dāng)前日期毫秒數(shù)大于參數(shù)日期毫秒數(shù)。而compareTo()是將當(dāng)前日期與參數(shù)日期比較后,返回一個(gè)int型值,它的返回值有三種可能:-1、0和1。如果返回-1則表示當(dāng)前日期在參數(shù)日期之前;如果返回0則表示兩個(gè)日期是同一時(shí)刻;返回1則表示當(dāng)前日期在參數(shù)日期之后。雖然我們可以用compareTo()方法來(lái)比較兩個(gè)Date對(duì)象,但是它的設(shè)計(jì)實(shí)際是另有用途的,我們?cè)诤竺娴恼鹿?jié)將會(huì)講到。
下面我們就用一個(gè)示例來(lái)檢驗(yàn)一下以上方法的用法:
- import ?java.util.Date; ??
- ??
- public ? class ?DateTest?{ ??
- ???? public ? static ? void ?main(String[]?args)?{ ??
- ???????? //?2008-08-08?20:00:00對(duì)應(yīng)的毫秒數(shù) ??
- ???????? long ?t2008?=?1218196800000L; ??
- ???????? //?1900-01-01?20:00:00對(duì)應(yīng)的毫秒數(shù) ??
- ???????? long ?t1900?=?-2208945952000L; ??
- ??
- ???????? //?指定毫秒數(shù)創(chuàng)建Date對(duì)象 ??
- ????????Date?d2008?=? new ?Date(t2008); ??
- ???????? //?使用系統(tǒng)默認(rèn)時(shí)間創(chuàng)建Date對(duì)象 ??
- ????????Date?d1900?=? new ?Date(); ??
- ???????? //?通過(guò)設(shè)置毫秒數(shù)改變?nèi)掌诤蜁r(shí)間 ??
- ????????d1900.setTime(t1900); ??
- ??
- ????????System.out.println( "調(diào)用方法:d1900.before(d2008)" ); ??
- ????????System.out ??
- ????????????????.print( "比較結(jié)果:\"1900-01-01?20:00:00\"在\"2008-08-08?20:00:00\"" ); ??
- ???????? //?使用before()方法比較 ??
- ???????? if ?(d1900.before(d2008))?{ ??
- ????????????System.out.println( "之前" ); ??
- ????????}? else ?{ ??
- ????????????System.out.println( "之后" ); ??
- ????????} ??
- ???????? ??
- ????????System.out.println(); ??
- ???????? ??
- ????????System.out.println( "調(diào)用方法:d2008.after(d1900)" ); ??
- ????????System.out ??
- ????????????????.print( "比較結(jié)果:\"2008-08-08?20:00:00\"在\"1900-01-01?20:00:00\"" ); ??
- ???????? //?使用after()方法比較 ??
- ???????? if ?(d2008.after(d1900))?{ ??
- ????????????System.out.println( "之后" ); ??
- ????????}? else ?{ ??
- ????????????System.out.println( "之前" ); ??
- ????????} ??
- ???????? ??
- ????????System.out.println(); ??
- ???????? ??
- ????????System.out.println( "調(diào)用方法:d1900.compareTo(d2008)" ); ??
- ????????System.out ??
- ????????????????.print( "比較結(jié)果:\"1900-01-01?20:00:00\"在\"2008-08-08?20:00:00\"" ); ??
- ???????? //?使用compareTo()方法比較 ??
- ???????? int ?i?=?d1900.compareTo(d2008); ??
- ???????? if ?(i?==?- 1 )?{ ??
- ????????????System.out.println( "之前" ); ??
- ????????}? else ? if ?(i?==? 1 )?{ ??
- ????????????System.out.println( "之后" ); ??
- ????????}? else ? if ?(i?==? 0 )?{ ??
- ????????????System.out.println( "是同一時(shí)刻" ); ??
- ????????} ??
- ????} ??
- }??
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
// 2008-08-08 20:00:00對(duì)應(yīng)的毫秒數(shù)
long t2008 = 1218196800000L;
// 1900-01-01 20:00:00對(duì)應(yīng)的毫秒數(shù)
long t1900 = -2208945952000L;
// 指定毫秒數(shù)創(chuàng)建Date對(duì)象
Date d2008 = new Date(t2008);
// 使用系統(tǒng)默認(rèn)時(shí)間創(chuàng)建Date對(duì)象
Date d1900 = new Date();
// 通過(guò)設(shè)置毫秒數(shù)改變?nèi)掌诤蜁r(shí)間
d1900.setTime(t1900);
System.out.println("調(diào)用方法:d1900.before(d2008)");
System.out
.print("比較結(jié)果:\"1900-01-01 20:00:00\"在\"2008-08-08 20:00:00\"");
// 使用before()方法比較
if (d1900.before(d2008)) {
System.out.println("之前");
} else {
System.out.println("之后");
}
System.out.println();
System.out.println("調(diào)用方法:d2008.after(d1900)");
System.out
.print("比較結(jié)果:\"2008-08-08 20:00:00\"在\"1900-01-01 20:00:00\"");
// 使用after()方法比較
if (d2008.after(d1900)) {
System.out.println("之后");
} else {
System.out.println("之前");
}
System.out.println();
System.out.println("調(diào)用方法:d1900.compareTo(d2008)");
System.out
.print("比較結(jié)果:\"1900-01-01 20:00:00\"在\"2008-08-08 20:00:00\"");
// 使用compareTo()方法比較
int i = d1900.compareTo(d2008);
if (i == -1) {
System.out.println("之前");
} else if (i == 1) {
System.out.println("之后");
} else if (i == 0) {
System.out.println("是同一時(shí)刻");
}
}
}
運(yùn)行結(jié)果:
- 調(diào)用方法:d1900.before(d2008)
- 比較結(jié)果:"1900-01-01 20:00:00"在"2008-08-08 20:00:00"之前
- 調(diào)用方法:d2008.after(d1900)
- 比較結(jié)果:"2008-08-08 20:00:00"在"1900-01-01 20:00:00"之后
- 調(diào)用方法:d1900.compareTo(d2008)
- 比較結(jié)果:"1900-01-01 20:00:00"在"2008-08-08 20:00:00"之前
那么如果我們想直接獲取或者改變年、月、日、時(shí)、分、秒等等這些屬性的值時(shí)怎么辦呢?Date類當(dāng)然有完成這些操作的方法,不過(guò)遺憾的是它們也都已經(jīng)不被贊成使用了。我們必須換一個(gè)能夠提供這些操作的類,這個(gè)類就是java.util.Calendar。
公歷歷法java.util.GregorianCalendar
Calendar是一個(gè)抽象類,我們無(wú)法直接實(shí)例化它,它有一個(gè)具體子類實(shí)體類java.util.GregorianCalendar,這個(gè)類實(shí)現(xiàn)的就是我們?nèi)粘K玫墓珰v歷法,或者叫做陽(yáng)歷。我們可以直接使用new命令創(chuàng)建它的實(shí)例,或者使用Calendar類的這個(gè)方法來(lái)獲得它實(shí)例:
- public ? static ?Calendar?getInstance(){ ??
- ???? //other?code ??
- }??
public static Calendar getInstance(){
//other code
}
采用上面這個(gè)方法時(shí),我們創(chuàng)建的Calendar對(duì)象的日期和時(shí)間值是對(duì)象被創(chuàng)建時(shí)系統(tǒng)日期和時(shí)間值。當(dāng)使用new命令時(shí),我們有兩種選擇,一種是使用系統(tǒng)當(dāng)前的日期和時(shí)間值初始化GregorianCalendar對(duì)象;另一種是通過(guò)給定年、月、日、時(shí)、分、秒等屬性值來(lái)對(duì)其進(jìn)行初始化。請(qǐng)看下面的例子:
- import ?java.text.DateFormat; ??
- import ?java.text.SimpleDateFormat; ??
- import ?java.util.Calendar; ??
- import ?java.util.GregorianCalendar; ??
- ??
- public ? class ?DateTest?{ ??
- ???? /** ?
- ?????*?以一種較為友好的方式格式化日期時(shí)間值 ?
- ?????*? ?
- ?????*?@param?c ?
- ?????*????????????日期時(shí)間對(duì)象 ?
- ?????*?@return?格式化后的日期時(shí)間字符串 ?
- ?????*/ ??
- ???? public ? static ?String?toFriendlyString(Calendar?c)?{ ??
- ???????? if ?(c?!=? null )?{ ??
- ????????????DateFormat?df?=? new ?SimpleDateFormat( "yyyy年MM月dd日?HH:mm:ss" ); ??
- ???????????? return ?df.format(c.getTime()); ??
- ????????} ??
- ???????? return ? null ; ??
- ????} ??
- ??
- ???? public ? static ? void ?main(String[]?args)?{ ??
- ????????Calendar?c1?=?Calendar.getInstance(); ??
- ????????System.out.println( "創(chuàng)建方式:Calendar.getInstance()" ); ??
- ????????System.out.println( "日期時(shí)間:" ?+?DateTest.toFriendlyString(c1)); ??
- ????????System.out.println(); ??
- ??
- ????????Calendar?c2?=? new ?GregorianCalendar(); ??
- ????????System.out.println( "創(chuàng)建方式:new?GregorianCalendar()" ); ??
- ????????System.out.println( "日期時(shí)間:" ?+?DateTest.toFriendlyString(c2)); ??
- ????????System.out.println(); ??
- ??
- ???????? //?參數(shù)含義依次為:年、月、日 ??
- ????????Calendar?c3?=? new ?GregorianCalendar( 2008 ,? 8 ,? 8 ); ??
- ????????System.out.println( "創(chuàng)建方式:new?GregorianCalendar(2008,?8,?8)" ); ??
- ????????System.out.println( "日期時(shí)間:" ?+?DateTest.toFriendlyString(c3)); ??
- ????????System.out.println(); ??
- ??
- ???????? //?參數(shù)含義依次為:年、月、日、時(shí)、分 ??
- ????????Calendar?c4?=? new ?GregorianCalendar( 2008 ,? 8 ,? 8 ,? 6 ,? 10 ); ??
- ????????System.out.println( "創(chuàng)建方式:new?GregorianCalendar(2008,?8,?8,?6,?10)" ); ??
- ????????System.out.println( "日期時(shí)間:" ?+?DateTest.toFriendlyString(c4)); ??
- ????????System.out.println(); ??
- ??
- ???????? //?參數(shù)含義依次為:年、月、日、時(shí)、分、秒 ??
- ????????Calendar?c5?=? new ?GregorianCalendar( 2008 ,? 8 ,? 8 ,? 18 ,? 10 ,? 5 ); ??
- ????????System.out.println( "創(chuàng)建方式:new?GregorianCalendar(2008,?8,?8,?18,?10,?5)" ); ??
- ????????System.out.println( "日期時(shí)間:" ?+?DateTest.toFriendlyString(c5)); ??
- ????} ??
- }??
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DateTest {
/**
* 以一種較為友好的方式格式化日期時(shí)間值
*
* @param c
* 日期時(shí)間對(duì)象
* @return 格式化后的日期時(shí)間字符串
*/
public static String toFriendlyString(Calendar c) {
if (c != null) {
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
return df.format(c.getTime());
}
return null;
}
public static void main(String[] args) {
Calendar c1 = Calendar.getInstance();
System.out.println("創(chuàng)建方式:Calendar.getInstance()");
System.out.println("日期時(shí)間:" + DateTest.toFriendlyString(c1));
System.out.println();
Calendar c2 = new GregorianCalendar();
System.out.println("創(chuàng)建方式:new GregorianCalendar()");
System.out.println("日期時(shí)間:" + DateTest.toFriendlyString(c2));
System.out.println();
// 參數(shù)含義依次為:年、月、日
Calendar c3 = new GregorianCalendar(2008, 8, 8);
System.out.println("創(chuàng)建方式:new GregorianCalendar(2008, 8, 8)");
System.out.println("日期時(shí)間:" + DateTest.toFriendlyString(c3));
System.out.println();
// 參數(shù)含義依次為:年、月、日、時(shí)、分
Calendar c4 = new GregorianCalendar(2008, 8, 8, 6, 10);
System.out.println("創(chuàng)建方式:new GregorianCalendar(2008, 8, 8, 6, 10)");
System.out.println("日期時(shí)間:" + DateTest.toFriendlyString(c4));
System.out.println();
// 參數(shù)含義依次為:年、月、日、時(shí)、分、秒
Calendar c5 = new GregorianCalendar(2008, 8, 8, 18, 10, 5);
System.out.println("創(chuàng)建方式:new GregorianCalendar(2008, 8, 8, 18, 10, 5)");
System.out.println("日期時(shí)間:" + DateTest.toFriendlyString(c5));
}
}
運(yùn)行結(jié)果如下:
- 創(chuàng)建方式:Calendar.getInstance()
- 日期時(shí)間:2008年07月22日 11:54:48
- 創(chuàng)建方式:new GregorianCalendar()
- 日期時(shí)間:2008年07月22日 11:54:48
- 創(chuàng)建方式:new GregorianCalendar(2008, 8, 8)
- 日期時(shí)間:2008年09月08日 00:00:00
- 創(chuàng)建方式:new GregorianCalendar(2008, 8, 8, 6, 10)
- 日期時(shí)間:2008年09月08日 06:10:00
- 創(chuàng)建方式:new GregorianCalendar(2008, 8, 8, 18, 10, 5)
- 日期時(shí)間:2008年09月08日 18:10:05
為了便于閱讀,我們?cè)黾右粋€(gè)toFriendlyString(Calendar c)方法,它將日期時(shí)間值格式化為一種更加友好易懂的形式,我們將在接下來(lái)的內(nèi)容中講解它的實(shí)現(xiàn)原理。分析運(yùn)行結(jié)果后,我們發(fā)現(xiàn)有兩個(gè)地方需要注意:
- 在創(chuàng)建GregorianCalendar對(duì)象時(shí),月份值都設(shè)定為8,但打印結(jié)果都是9月份。這并不是我們的代碼有問(wèn)題,而是因?yàn)镴AVA表示的月份是從0開(kāi)始的,也就是說(shuō)它用來(lái)表示月份的數(shù)值總是比實(shí)際月份值小1。因此我們要表示8月份,就是應(yīng)該設(shè)置8-1=7這個(gè)值。
- GregorianCalendar的小時(shí)數(shù)是24小時(shí)制的。
為了避免出現(xiàn)因?yàn)橥浱幚?的差值而設(shè)置了錯(cuò)誤的月份,也讓代碼看起來(lái)更加直觀,推薦大家使用定義在Calendar類的的這些常量來(lái)代替直接用數(shù)字表示月份:
- 一月:Calendar.JANUARY = 0
- 二月:Calendar.FEBRUARY = 1
- 三月:Calendar.MARCH = 2
- 四月:Calendar.APRIL = 3
- 五月:Calendar.MAY = 4
- 六月:Calendar.JUNE = 5
- 七月:Calendar.JULY = 6
- 八月:Calendar.AUGUST = 7
- 九月:Calendar.SEPTEMBER = 8
- 十月:Calendar.OCTOBER = 9
- 十一月:Calendar.NOVEMBER = 10
- 十二月:Calendar.DECEMBER = 11
如果我們想要從Calendar對(duì)象獲得各種屬性的值,就需要調(diào)用它的get(int field)方法,這個(gè)方法接收一個(gè)int型的參數(shù),并且根據(jù)這個(gè)給定參數(shù)的值來(lái)返回相應(yīng)的屬性的值。該方法的定義如下:
- public ? int ?get( int ?field){ ??
- ???? //other?code ??
- }??
public int get(int field){
//other code
}
我們以一個(gè)示例來(lái)說(shuō)明get(int field)方法所能接受的一些常用參數(shù)的含義及用法:
- import ?java.text.DateFormat; ??
- import ?java.text.SimpleDateFormat; ??
- import ?java.util.Calendar; ??
- ??
- public ? class ?DateTest?{ ??
- ???? /** ?
- ?????*?以一種較為友好的方式格式化日期時(shí)間值 ?
- ?????*? ?
- ?????*?@param?c ?
- ?????*????????????日期時(shí)間對(duì)象 ?
- ?????*?@return?格式化后的日期時(shí)間字符串 ?
- ?????*/ ??
- ???? public ? static ?String?toFriendlyString(Calendar?c)?{ ??
- ???????? if ?(c?!=? null )?{ ??
- ????????????DateFormat?df?=? new ?SimpleDateFormat( "yyyy年MM月dd日?HH:mm:ss.SSS" ); ??
- ???????????? return ?df.format(c.getTime()); ??
- ????????} ??
- ???????? return ? null ; ??
- ????} ??
- ??
- ???? public ? static ? void ?main(String[]?args)?{ ??
- ????????Calendar?c?=?Calendar.getInstance(); ??
- ????????System.out.println( "當(dāng)前時(shí)刻:" ?+?DateTest.toFriendlyString(c)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.AM_PM" ); ??
- ????????System.out.println( "代表含義:上下午標(biāo)識(shí),上午返回Calendar.AM=0,下午返回Calendar.PM=1" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.AM_PM)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.DATE" ); ??
- ????????System.out.println( "代表含義:一個(gè)月中的第幾天,同Calendar.DAY_OF_MONTH" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.DATE)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.DAY_OF_MONTH" ); ??
- ????????System.out.println( "代表含義:一個(gè)月中的第幾天,同Calendar.DATE" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.DAY_OF_MONTH)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.DAY_OF_WEEK" ); ??
- ????????System.out.println( "代表含義:星期幾。" ); ??
- ????????System.out.println( "星期日:Calendar.SUNDAY=1" ); ??
- ????????System.out.println( "星期一:Calendar.MONDAY=2" ); ??
- ????????System.out.println( "星期二:Calendar.TUESDAY=3" ); ??
- ????????System.out.println( "星期三:Calendar.WEDNESDAY=4" ); ??
- ????????System.out.println( "星期四:Calendar.THURSDAY=5" ); ??
- ????????System.out.println( "星期五:Calendar.FRIDAY=6" ); ??
- ????????System.out.println( "星期六:Calendar.SATURDAY=7" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.DAY_OF_WEEK)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.DAY_OF_WEEK_IN_MONTH" ); ??
- ????????System.out.println( "代表含義:這一天所對(duì)應(yīng)的星期幾在該月中是第幾次出現(xiàn)" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.DAY_OF_WEEK_IN_MONTH)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.DAY_OF_YEAR" ); ??
- ????????System.out.println( "代表含義:一年中的第幾天" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.DAY_OF_YEAR)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.HOUR" ); ??
- ????????System.out.println( "代表含義:12小時(shí)制下的小時(shí)數(shù),中午和午夜表示為0" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.HOUR)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.HOUR_OF_DAY" ); ??
- ????????System.out.println( "代表含義:24小時(shí)制下的小時(shí)數(shù),午夜表示為0" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.HOUR_OF_DAY)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.MILLISECOND" ); ??
- ????????System.out.println( "代表含義:毫秒數(shù)" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.MILLISECOND)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.MINUTE" ); ??
- ????????System.out.println( "代表含義:分鐘" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.MINUTE)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.MONTH" ); ??
- ????????System.out.println( "代表含義:月份,從0到11表示12個(gè)月份,比實(shí)際月份值小1" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.MONTH)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.SECOND" ); ??
- ????????System.out.println( "代表含義:秒" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.SECOND)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.WEEK_OF_MONTH" ); ??
- ????????System.out.println( "代表含義:一個(gè)月中的第幾個(gè)星期" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.WEEK_OF_MONTH)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.WEEK_OF_YEAR" ); ??
- ????????System.out.println( "代表含義:一年中的第幾個(gè)星期" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.WEEK_OF_YEAR)); ??
- ????????System.out.println(); ??
- ??
- ????????System.out.println( "屬性名稱:Calendar.YEAR" ); ??
- ????????System.out.println( "代表含義:年份" ); ??
- ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.YEAR)); ??
- ????} ??
- }??
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateTest {
/**
* 以一種較為友好的方式格式化日期時(shí)間值
*
* @param c
* 日期時(shí)間對(duì)象
* @return 格式化后的日期時(shí)間字符串
*/
public static String toFriendlyString(Calendar c) {
if (c != null) {
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss.SSS");
return df.format(c.getTime());
}
return null;
}
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
System.out.println("當(dāng)前時(shí)刻:" + DateTest.toFriendlyString(c));
System.out.println();
System.out.println("屬性名稱:Calendar.AM_PM");
System.out.println("代表含義:上下午標(biāo)識(shí),上午返回Calendar.AM=0,下午返回Calendar.PM=1");
System.out.println("測(cè)試結(jié)果:" + c.get(Calendar.AM_PM));
System.out.println();
System.out.println("屬性名稱:Calendar.DATE");
System.out.println("代表含義:一個(gè)月中的第幾天,同Calendar.DAY_OF_MONTH");
System.out.println("測(cè)試結(jié)果:" + c.get(Calendar.DATE));
System.out.println();
System.out.println("屬性名稱:Calendar.DAY_OF_MONTH");
System.out.println("代表含義:一個(gè)月中的第幾天,同Calendar.DATE");
System.out.println("測(cè)試結(jié)果:" + c.get(Calendar.DAY_OF_MONTH));
System.out.println();
System.out.println("屬性名稱:Calendar.DAY_OF_WEEK");
System.out.println("代表含義:星期幾。");
System.out.println("星期日:Calendar.SUNDAY=1");
System.out.println("星期一:Calendar.MONDAY=2");
System.out.println("星期二:Calendar.TUESDAY=3");
System.out.println("星期三:Calendar.WEDNESDAY=4");
System.out.println("星期四:Calendar.THURSDAY=5");
System.out.println("星期五:Calendar.FRIDAY=6");
System.out.println("星期六:Calendar.SATURDAY=7");
System.out.println("測(cè)試結(jié)果:" + c.get(Calendar.DAY_OF_WEEK));
System.out.println();
System.out.println("屬性名稱:Calendar.DAY_OF_WEEK_IN_MONTH");
System.out.println("代表含義:這一天所對(duì)應(yīng)的星期幾在該月中是第幾次出現(xiàn)");
System.out.println("測(cè)試結(jié)果:" + c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println();
System.out.println("屬性名稱:Calendar.DAY_OF_YEAR");
System.out.println("代表含義:一年中的第幾天");
System.out.println("測(cè)試結(jié)果:" + c.get(Calendar.DAY_OF_YEAR));
System.out.println();
System.out.println("屬性名稱:Calendar.HOUR");
System.out.println("代表含義:12小時(shí)制下的小時(shí)數(shù),中午和午夜表示為0");
System.out.println("測(cè)試結(jié)果:" + c.get(Calendar.HOUR));
System.out.println();
System.out.println("屬性名稱:Calendar.HOUR_OF_DAY");
System.out.println("代表含義:24小時(shí)制下的小時(shí)數(shù),午夜表示為0");
System.out.println("測(cè)試結(jié)果:" + c.get(Calendar.HOUR_OF_DAY));
System.out.println();
System.out.println("屬性名稱:Calendar.MILLISECOND");
System.out.println("代表含義:毫秒數(shù)");
System.out.println("測(cè)試結(jié)果:" + c.get(Calendar.MILLISECOND));
System.out.println();
System.out.println("屬性名稱:Calendar.MINUTE");
System.out.println("代表含義:分鐘");
System.out.println("測(cè)試結(jié)果:" + c.get(Calendar.MINUTE));
System.out.println();
System.out.println("屬性名稱:Calendar.MONTH");
System.out.println("代表含義:月份,從0到11表示12個(gè)月份,比實(shí)際月份值小1");
System.out.println("測(cè)試結(jié)果:" + c.get(Calendar.MONTH));
System.out.println();
System.out.println("屬性名稱:Calendar.SECOND");
System.out.println("代表含義:秒");
System.out.println("測(cè)試結(jié)果:" + c.get(Calendar.SECOND));
System.out.println();
System.out.println("屬性名稱:Calendar.WEEK_OF_MONTH");
System.out.println("代表含義:一個(gè)月中的第幾個(gè)星期");
System.out.println("測(cè)試結(jié)果:" + c
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

