黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

日期處理

系統(tǒng) 2126 0

日期和時(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è)可以使用的:

Java代碼 復(fù)制代碼
  1. public ?Date()?{ ??
  2. ???? this (System.currentTimeMillis()); ??
  3. } ??
  4. ??
  5. public ?Date( long ?date)?{ ??
  6. ???? //other?code ??
  7. }??
    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è)本地方法,它的定義如下:

Java代碼 復(fù)制代碼
  1. 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ū)下的顯示效果:

Java代碼 復(fù)制代碼
  1. import ?java.util.Date; ??
  2. ??
  3. public ? class ?DateTest?{ ??
  4. ???? public ? static ? void ?main(String[]?args)?{ ??
  5. ????????Date?d?=? new ?Date(); ??
  6. ???????? //?在默認(rèn)時(shí)區(qū)下輸出日期和時(shí)間值 ??
  7. ????????System.out.println(d); ??
  8. ????} ??
  9. }??
    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è)方法的定義如下:

Java代碼 復(fù)制代碼
  1. public ? long ?getTime()?{ ??
  2. ???? //other?code ??
  3. } ??
  4. ??
  5. public ? void ?setTime( long ?time)?{ ??
  6. ???? //other?code ??
  7. }??
    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)為我們提供了這樣的方法:

Java代碼 復(fù)制代碼
  1. public ? boolean ?before(Date?when)?{ ??
  2. ???? //other?code ??
  3. } ??
  4. ??
  5. public ? boolean ?after(Date?when)?{ ??
  6. ???? //other?code ??
  7. } ??
  8. ??
  9. public ? int ?compareTo(Date?anotherDate)?{ ??
  10. ???? //other?code ??
  11. }??
    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)一下以上方法的用法:

Java代碼 復(fù)制代碼
  1. import ?java.util.Date; ??
  2. ??
  3. public ? class ?DateTest?{ ??
  4. ???? public ? static ? void ?main(String[]?args)?{ ??
  5. ???????? //?2008-08-08?20:00:00對(duì)應(yīng)的毫秒數(shù) ??
  6. ???????? long ?t2008?=?1218196800000L; ??
  7. ???????? //?1900-01-01?20:00:00對(duì)應(yīng)的毫秒數(shù) ??
  8. ???????? long ?t1900?=?-2208945952000L; ??
  9. ??
  10. ???????? //?指定毫秒數(shù)創(chuàng)建Date對(duì)象 ??
  11. ????????Date?d2008?=? new ?Date(t2008); ??
  12. ???????? //?使用系統(tǒng)默認(rèn)時(shí)間創(chuàng)建Date對(duì)象 ??
  13. ????????Date?d1900?=? new ?Date(); ??
  14. ???????? //?通過(guò)設(shè)置毫秒數(shù)改變?nèi)掌诤蜁r(shí)間 ??
  15. ????????d1900.setTime(t1900); ??
  16. ??
  17. ????????System.out.println( "調(diào)用方法:d1900.before(d2008)" ); ??
  18. ????????System.out ??
  19. ????????????????.print( "比較結(jié)果:\"1900-01-01?20:00:00\"在\"2008-08-08?20:00:00\"" ); ??
  20. ???????? //?使用before()方法比較 ??
  21. ???????? if ?(d1900.before(d2008))?{ ??
  22. ????????????System.out.println( "之前" ); ??
  23. ????????}? else ?{ ??
  24. ????????????System.out.println( "之后" ); ??
  25. ????????} ??
  26. ???????? ??
  27. ????????System.out.println(); ??
  28. ???????? ??
  29. ????????System.out.println( "調(diào)用方法:d2008.after(d1900)" ); ??
  30. ????????System.out ??
  31. ????????????????.print( "比較結(jié)果:\"2008-08-08?20:00:00\"在\"1900-01-01?20:00:00\"" ); ??
  32. ???????? //?使用after()方法比較 ??
  33. ???????? if ?(d2008.after(d1900))?{ ??
  34. ????????????System.out.println( "之后" ); ??
  35. ????????}? else ?{ ??
  36. ????????????System.out.println( "之前" ); ??
  37. ????????} ??
  38. ???????? ??
  39. ????????System.out.println(); ??
  40. ???????? ??
  41. ????????System.out.println( "調(diào)用方法:d1900.compareTo(d2008)" ); ??
  42. ????????System.out ??
  43. ????????????????.print( "比較結(jié)果:\"1900-01-01?20:00:00\"在\"2008-08-08?20:00:00\"" ); ??
  44. ???????? //?使用compareTo()方法比較 ??
  45. ???????? int ?i?=?d1900.compareTo(d2008); ??
  46. ???????? if ?(i?==?- 1 )?{ ??
  47. ????????????System.out.println( "之前" ); ??
  48. ????????}? else ? if ?(i?==? 1 )?{ ??
  49. ????????????System.out.println( "之后" ); ??
  50. ????????}? else ? if ?(i?==? 0 )?{ ??
  51. ????????????System.out.println( "是同一時(shí)刻" ); ??
  52. ????????} ??
  53. ????} ??
  54. }??
    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é)果:

  1. 調(diào)用方法:d1900.before(d2008)
  2. 比較結(jié)果:"1900-01-01 20:00:00"在"2008-08-08 20:00:00"之前
  3. 調(diào)用方法:d2008.after(d1900)
  4. 比較結(jié)果:"2008-08-08 20:00:00"在"1900-01-01 20:00:00"之后
  5. 調(diào)用方法:d1900.compareTo(d2008)
  6. 比較結(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í)例:

Java代碼 復(fù)制代碼
  1. public ? static ?Calendar?getInstance(){ ??
  2. ???? //other?code ??
  3. }??
    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)看下面的例子:

Java代碼 復(fù)制代碼
  1. import ?java.text.DateFormat; ??
  2. import ?java.text.SimpleDateFormat; ??
  3. import ?java.util.Calendar; ??
  4. import ?java.util.GregorianCalendar; ??
  5. ??
  6. public ? class ?DateTest?{ ??
  7. ???? /** ?
  8. ?????*?以一種較為友好的方式格式化日期時(shí)間值 ?
  9. ?????*? ?
  10. ?????*?@param?c ?
  11. ?????*????????????日期時(shí)間對(duì)象 ?
  12. ?????*?@return?格式化后的日期時(shí)間字符串 ?
  13. ?????*/ ??
  14. ???? public ? static ?String?toFriendlyString(Calendar?c)?{ ??
  15. ???????? if ?(c?!=? null )?{ ??
  16. ????????????DateFormat?df?=? new ?SimpleDateFormat( "yyyy年MM月dd日?HH:mm:ss" ); ??
  17. ???????????? return ?df.format(c.getTime()); ??
  18. ????????} ??
  19. ???????? return ? null ; ??
  20. ????} ??
  21. ??
  22. ???? public ? static ? void ?main(String[]?args)?{ ??
  23. ????????Calendar?c1?=?Calendar.getInstance(); ??
  24. ????????System.out.println( "創(chuàng)建方式:Calendar.getInstance()" ); ??
  25. ????????System.out.println( "日期時(shí)間:" ?+?DateTest.toFriendlyString(c1)); ??
  26. ????????System.out.println(); ??
  27. ??
  28. ????????Calendar?c2?=? new ?GregorianCalendar(); ??
  29. ????????System.out.println( "創(chuàng)建方式:new?GregorianCalendar()" ); ??
  30. ????????System.out.println( "日期時(shí)間:" ?+?DateTest.toFriendlyString(c2)); ??
  31. ????????System.out.println(); ??
  32. ??
  33. ???????? //?參數(shù)含義依次為:年、月、日 ??
  34. ????????Calendar?c3?=? new ?GregorianCalendar( 2008 ,? 8 ,? 8 ); ??
  35. ????????System.out.println( "創(chuàng)建方式:new?GregorianCalendar(2008,?8,?8)" ); ??
  36. ????????System.out.println( "日期時(shí)間:" ?+?DateTest.toFriendlyString(c3)); ??
  37. ????????System.out.println(); ??
  38. ??
  39. ???????? //?參數(shù)含義依次為:年、月、日、時(shí)、分 ??
  40. ????????Calendar?c4?=? new ?GregorianCalendar( 2008 ,? 8 ,? 8 ,? 6 ,? 10 ); ??
  41. ????????System.out.println( "創(chuàng)建方式:new?GregorianCalendar(2008,?8,?8,?6,?10)" ); ??
  42. ????????System.out.println( "日期時(shí)間:" ?+?DateTest.toFriendlyString(c4)); ??
  43. ????????System.out.println(); ??
  44. ??
  45. ???????? //?參數(shù)含義依次為:年、月、日、時(shí)、分、秒 ??
  46. ????????Calendar?c5?=? new ?GregorianCalendar( 2008 ,? 8 ,? 8 ,? 18 ,? 10 ,? 5 ); ??
  47. ????????System.out.println( "創(chuàng)建方式:new?GregorianCalendar(2008,?8,?8,?18,?10,?5)" ); ??
  48. ????????System.out.println( "日期時(shí)間:" ?+?DateTest.toFriendlyString(c5)); ??
  49. ????} ??
  50. }??
    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é)果如下:

  1. 創(chuàng)建方式:Calendar.getInstance()
  2. 日期時(shí)間:2008年07月22日 11:54:48
  3. 創(chuàng)建方式:new GregorianCalendar()
  4. 日期時(shí)間:2008年07月22日 11:54:48
  5. 創(chuàng)建方式:new GregorianCalendar(2008, 8, 8)
  6. 日期時(shí)間:2008年09月08日 00:00:00
  7. 創(chuàng)建方式:new GregorianCalendar(2008, 8, 8, 6, 10)
  8. 日期時(shí)間:2008年09月08日 06:10:00
  9. 創(chuàng)建方式:new GregorianCalendar(2008, 8, 8, 18, 10, 5)
  10. 日期時(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è)地方需要注意:

  1. 在創(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è)值。
  2. 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)的屬性的值。該方法的定義如下:

Java代碼 復(fù)制代碼
  1. public ? int ?get( int ?field){ ??
  2. ???? //other?code ??
  3. }??
    public int get(int field){
    //other code
}

  


我們以一個(gè)示例來(lái)說(shuō)明get(int field)方法所能接受的一些常用參數(shù)的含義及用法:

Java代碼 復(fù)制代碼
  1. import ?java.text.DateFormat; ??
  2. import ?java.text.SimpleDateFormat; ??
  3. import ?java.util.Calendar; ??
  4. ??
  5. public ? class ?DateTest?{ ??
  6. ???? /** ?
  7. ?????*?以一種較為友好的方式格式化日期時(shí)間值 ?
  8. ?????*? ?
  9. ?????*?@param?c ?
  10. ?????*????????????日期時(shí)間對(duì)象 ?
  11. ?????*?@return?格式化后的日期時(shí)間字符串 ?
  12. ?????*/ ??
  13. ???? public ? static ?String?toFriendlyString(Calendar?c)?{ ??
  14. ???????? if ?(c?!=? null )?{ ??
  15. ????????????DateFormat?df?=? new ?SimpleDateFormat( "yyyy年MM月dd日?HH:mm:ss.SSS" ); ??
  16. ???????????? return ?df.format(c.getTime()); ??
  17. ????????} ??
  18. ???????? return ? null ; ??
  19. ????} ??
  20. ??
  21. ???? public ? static ? void ?main(String[]?args)?{ ??
  22. ????????Calendar?c?=?Calendar.getInstance(); ??
  23. ????????System.out.println( "當(dāng)前時(shí)刻:" ?+?DateTest.toFriendlyString(c)); ??
  24. ????????System.out.println(); ??
  25. ??
  26. ????????System.out.println( "屬性名稱:Calendar.AM_PM" ); ??
  27. ????????System.out.println( "代表含義:上下午標(biāo)識(shí),上午返回Calendar.AM=0,下午返回Calendar.PM=1" ); ??
  28. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.AM_PM)); ??
  29. ????????System.out.println(); ??
  30. ??
  31. ????????System.out.println( "屬性名稱:Calendar.DATE" ); ??
  32. ????????System.out.println( "代表含義:一個(gè)月中的第幾天,同Calendar.DAY_OF_MONTH" ); ??
  33. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.DATE)); ??
  34. ????????System.out.println(); ??
  35. ??
  36. ????????System.out.println( "屬性名稱:Calendar.DAY_OF_MONTH" ); ??
  37. ????????System.out.println( "代表含義:一個(gè)月中的第幾天,同Calendar.DATE" ); ??
  38. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.DAY_OF_MONTH)); ??
  39. ????????System.out.println(); ??
  40. ??
  41. ????????System.out.println( "屬性名稱:Calendar.DAY_OF_WEEK" ); ??
  42. ????????System.out.println( "代表含義:星期幾。" ); ??
  43. ????????System.out.println( "星期日:Calendar.SUNDAY=1" ); ??
  44. ????????System.out.println( "星期一:Calendar.MONDAY=2" ); ??
  45. ????????System.out.println( "星期二:Calendar.TUESDAY=3" ); ??
  46. ????????System.out.println( "星期三:Calendar.WEDNESDAY=4" ); ??
  47. ????????System.out.println( "星期四:Calendar.THURSDAY=5" ); ??
  48. ????????System.out.println( "星期五:Calendar.FRIDAY=6" ); ??
  49. ????????System.out.println( "星期六:Calendar.SATURDAY=7" ); ??
  50. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.DAY_OF_WEEK)); ??
  51. ????????System.out.println(); ??
  52. ??
  53. ????????System.out.println( "屬性名稱:Calendar.DAY_OF_WEEK_IN_MONTH" ); ??
  54. ????????System.out.println( "代表含義:這一天所對(duì)應(yīng)的星期幾在該月中是第幾次出現(xiàn)" ); ??
  55. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.DAY_OF_WEEK_IN_MONTH)); ??
  56. ????????System.out.println(); ??
  57. ??
  58. ????????System.out.println( "屬性名稱:Calendar.DAY_OF_YEAR" ); ??
  59. ????????System.out.println( "代表含義:一年中的第幾天" ); ??
  60. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.DAY_OF_YEAR)); ??
  61. ????????System.out.println(); ??
  62. ??
  63. ????????System.out.println( "屬性名稱:Calendar.HOUR" ); ??
  64. ????????System.out.println( "代表含義:12小時(shí)制下的小時(shí)數(shù),中午和午夜表示為0" ); ??
  65. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.HOUR)); ??
  66. ????????System.out.println(); ??
  67. ??
  68. ????????System.out.println( "屬性名稱:Calendar.HOUR_OF_DAY" ); ??
  69. ????????System.out.println( "代表含義:24小時(shí)制下的小時(shí)數(shù),午夜表示為0" ); ??
  70. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.HOUR_OF_DAY)); ??
  71. ????????System.out.println(); ??
  72. ??
  73. ????????System.out.println( "屬性名稱:Calendar.MILLISECOND" ); ??
  74. ????????System.out.println( "代表含義:毫秒數(shù)" ); ??
  75. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.MILLISECOND)); ??
  76. ????????System.out.println(); ??
  77. ??
  78. ????????System.out.println( "屬性名稱:Calendar.MINUTE" ); ??
  79. ????????System.out.println( "代表含義:分鐘" ); ??
  80. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.MINUTE)); ??
  81. ????????System.out.println(); ??
  82. ??
  83. ????????System.out.println( "屬性名稱:Calendar.MONTH" ); ??
  84. ????????System.out.println( "代表含義:月份,從0到11表示12個(gè)月份,比實(shí)際月份值小1" ); ??
  85. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.MONTH)); ??
  86. ????????System.out.println(); ??
  87. ??
  88. ????????System.out.println( "屬性名稱:Calendar.SECOND" ); ??
  89. ????????System.out.println( "代表含義:秒" ); ??
  90. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.SECOND)); ??
  91. ????????System.out.println(); ??
  92. ??
  93. ????????System.out.println( "屬性名稱:Calendar.WEEK_OF_MONTH" ); ??
  94. ????????System.out.println( "代表含義:一個(gè)月中的第幾個(gè)星期" ); ??
  95. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.WEEK_OF_MONTH)); ??
  96. ????????System.out.println(); ??
  97. ??
  98. ????????System.out.println( "屬性名稱:Calendar.WEEK_OF_YEAR" ); ??
  99. ????????System.out.println( "代表含義:一年中的第幾個(gè)星期" ); ??
  100. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.WEEK_OF_YEAR)); ??
  101. ????????System.out.println(); ??
  102. ??
  103. ????????System.out.println( "屬性名稱:Calendar.YEAR" ); ??
  104. ????????System.out.println( "代表含義:年份" ); ??
  105. ????????System.out.println( "測(cè)試結(jié)果:" ?+?c.get(Calendar.YEAR)); ??
  106. ????} ??
  107. }??
    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ì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論