Java基礎之時間

Java基礎之時間date

一.概述

    1.1簡介:

        在 JDK 1.1 以前,類 Date 有兩個其餘的函數。它容許把日期解釋爲年、月、日、小時、分鐘和秒值。它也容許格式化和解析日期字符串。不過,這些函數的 API 不易於實現國際化。從 JDK 1.1 開始,應該使用 Calendar 類實現日期和時間字段之間轉換,使用 DateFormat 類來格式化和解析日期字符串。Date 中的相應方法已廢棄。 java

        儘管 Date 類打算反映協調世界時 (UTC),但沒法作到如此準確,這取決於 Java 虛擬機的主機環境。當前幾乎全部操做系統都假定 1 天 = 24 × 60 × 60 = 86400 秒。但對於 UTC,大約每一兩年出現一次額外的一秒,稱爲「閏秒」。閏秒始終做爲當天的最後一秒增長,而且始終在 12 月 31 日或 6 月 30 日增長。例如,1995 年的最後一分鐘是 61 秒,由於增長了閏秒。大多數計算機時鐘不是特別的準確,所以不能反映閏秒的差異。 git

        一些計算機標準是按照格林威治標準時 (GMT)(1970-01-01 00:00:00) 定義的,格林威治標準時和世界時 (UT) 是相等的。GMT 是標準的「民間」名稱;UT 是相同標準的「科學」名稱。UTC 和 UT 的區別是:UTC 是基於原子時鐘的,UT 是基於天體觀察的,二者在實際應用中難分軒輊。由於地球的旋轉不是均勻的(它以複雜的方式減速和加速),因此 UT 始終不是均勻地流過。閏秒是根據須要引入 UTC 的,以便把 UTC 保持在 UT1 的 0.9 秒以內,UT1 是應用了某些更正的 UT 版本。還有其餘的時間和日期系統;例如,基於衛星的全球定位系統 (GPS) 使用的時間刻度與 UTC 同步,但沒有 針對閏秒進行調整。有關更多信息的一個有趣來源是美國海軍天文臺,特別是 Directorate of Time 的網址shell

    1.2Java中的date主要依賴類 

        a)System.currentTimeMillis():一個13位的long類型。有一個native方法獲取運行環境的數字。函數

        b)java.util.Date:java具體的時間類封裝。主要是封裝了System.currentTimeMills()的,解析其數字,獲得想要得到的 年,月、秒、小時、分鐘、秒、毫秒,也能夠直接傳一個lang進去。測試

        c)java.text.SimpleDateFormat:用戶格式化時間。封裝了Calendarspa

        d)java.util.Calendar:用於時間的對比、增減、獲取月份等具體操做。操作系統

    1.3Date的格式

    CST時區的縮寫:code

美國中部時間:Central Standard Time (USA) UT-6:00
澳大利亞中部時間:Central Standard Time (Australia) UT+9:30
中國標準時間:China Standard Time UT+8:00
古巴標準時間:Cuba Standard Time UT-4:00

    格式化orm

字母  日期或時間元素  表示  示例  
G      Era 標誌符  Text  AD  (公元)
y      年  Year  1996; 96  
M      年中的月份  Month  July; Jul; 07  
w      年中的週數  Number  27  
W      月份中的週數  Number  2  
D      年中的天數  Number  189  
d      月份中的天數  Number  10  
F      月份中的星期  Number  2  
E      星期中的天數  Text  Tuesday; Tue  
a      Am/pm 標記  Text  PM  
H      一天中的小時數(0-23)  Number  0  
k      一天中的小時數(1-24)  Number  24  (上下午的多少點)
K      am/pm 中的小時數(0-11)  Number  0  
h      am/pm 中的小時數(1-12)  Number  12  
m      小時中的分鐘數  Number  30  
s      分鐘中的秒數  Number  55  
S      毫秒數  Number  978  
z      時區  General time zone  Pacific Standard Time; PST; GMT-08:00  
Z      時區  RFC 822 time zone  -0800  

    1.4總結:

    若是須要格式化日期則使用SimpleDateFormat,若是須要進行時間操做則使用Calendarci

二.案例

    2.1測試

public void getDate() {
		// date:
		System.out.println("System.currentTimeMillis():" + System.currentTimeMillis());// 1451133176031
		System.out.println("toString():" + new Date().toString());// Sat Dec 26  20:29:13 CST
		/**
		 * 時間:Sat Dec 26 20:29:13 CST 2015 
		 * dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat). (星期) 
		 * mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec). (月) 
		 * dd is the day of the month (01 through 31), as two decimal digits. (一個月的多少號) 
		 * hh is the hour of the day (00 through 23), as two decimal digits. (時間)
		 * mm is the minute within the hour (00 through 59), as two decimal digits. (分鐘) 
		 * ss is the second within the minute (00 through 61, as two decimal digits. (秒) 
		 * zzz(時區) is the time zone (and may reflect daylight saving time). 
			Standard time zone abbreviations include those recognized by the method parse. If time zone information is not
		        available, then zzz is empty - that is, it consists of no characters at all. 
		 * yyyy is the year, as four decimal digits. (年份)
		 */

		// 格式化
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSSS zzz");
		System.out.println("format():" + sdf.format(new Date()));// 2015-12-26  21:43:49 0101 CST

		// 時間的對比、加減
		Calendar cld = Calendar.getInstance();
		cld.setTime(new Date());
		System.out.println(cld.get(Calendar.DAY_OF_MONTH));// 26號
		System.out.println(cld.get(Calendar.DAY_OF_MONTH));// 26號
	}

    2.2 Date類

public void testLang() {
		// 測試格林威治時間(失敗,不要去弄了)
		Date date = new Date(1970, 01, 01, 00, 00, 10); //
		Calendar cld = Calendar.getInstance();
		cld.set(1970, 01, 01, 00, 00, 10);
		System.out.println(date.getTime()); // 59960880010000
		System.out.println(new Date().getTime()); // 1451136454150
		System.out.println(cld.getTimeInMillis()); // 2649610482
	}

    2.3simpleDateFormat類

public void testFormat() {
		Date date = new Date();
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSSS zzz");
		System.out.println(sdf1.format(date));// 2015-12-26 22:00:21 0908 CST

		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd號 HH小時mm分鐘ss秒 SSSS毫秒 zzz時區");
		System.out.println(sdf2.format(date));// 2015年12月26號 22小時00分鐘21秒 0908毫秒
												// CST時區

		SimpleDateFormat sdf3 = new SimpleDateFormat("G");
		System.out.println(sdf3.format(date));// 公元

		SimpleDateFormat sdf4 = new SimpleDateFormat("周/年:ww/52");
		System.out.println(sdf4.format(date));// 周/年:52/52

		SimpleDateFormat sdf5 = new SimpleDateFormat("周/月:WW/4");
		System.out.println(sdf5.format(date));// 周/月:04/4

		SimpleDateFormat sdf6 = new SimpleDateFormat("天/年:DDD/(365|366)");
		System.out.println(sdf6.format(date));// 天/年:360/(366|366)

		SimpleDateFormat sdf7 = new SimpleDateFormat("星期/月:F");
		System.out.println(sdf7.format(date));// 星期/月:4/4

		SimpleDateFormat sdf8 = new SimpleDateFormat("E");
		System.out.println(sdf8.format(date));// 星期六

		SimpleDateFormat sdf9 = new SimpleDateFormat("a");
		System.out.println(sdf9.format(date));// 下午

		SimpleDateFormat sdf10 = new SimpleDateFormat("kk點");
		System.out.println(sdf10.format(date));// 22點

		SimpleDateFormat sdf11 = new SimpleDateFormat("K點");
		System.out.println(sdf11.format(date));// 10點(上午下午的多少點)

		SimpleDateFormat sdf12 = new SimpleDateFormat("h點");
		System.out.println(sdf12.format(date));// 10點(上午下午的多少點)
	}

    2.4 Calender類

public void testCalendar() {
		Calendar cld = Calendar.getInstance();
		cld.set(2015, 12, 26, 22, 12, 14);
		
		//能夠獲取的太多,只列舉經常使用的
		System.out.println("年:" + cld.get(Calendar.YEAR));//年:2015
		System.out.println("月:" + cld.get(Calendar.MONTH));//月:11(月份+1)
		System.out.println("日:" + cld.get(Calendar.DATE));//日:26
		System.out.println("小時:" + cld.get(Calendar.HOUR));//小時:10
		System.out.println("分鐘:" + cld.get(Calendar.MINUTE));//分鐘:12
		System.out.println("秒:" + cld.get(Calendar.SECOND));//秒:14
		System.out.println("幾號:" + cld.get(Calendar.DAY_OF_MONTH));//幾號:26
		System.out.println("星期:" + cld.get(Calendar.DAY_OF_WEEK));//星期:7(星期須要-1)
		
		//對比
		Calendar cld2 = Calendar.getInstance();
		cld2.set(2015, 12, 26, 22, 12, 15);//比cld多了1秒
		Calendar cld3 = Calendar.getInstance();
		cld3.set(2015, 12, 26, 22, 12, 15);//比cld多了1秒
		//cld.set(2015, 12, 26, 22, 12, 14);
		System.out.println(cld2.compareTo(cld));//1
		System.out.println(cld.compareTo(cld2));//-1
		System.out.println(cld3.compareTo(cld2));//0
		System.out.println(cld2.before(cld));//false
		System.out.println(cld2.after(cld));// true
		
		//增長
		//cld3.set(2015, 12, 26, 22, 12, 15);
		//cld2.set(2015, 12, 26, 22, 12, 15)
		cld2.add(Calendar.DATE, 1);
		System.out.println(cld2.get(Calendar.DATE));//27
		cld3.add(Calendar.DATE, -1);
		System.out.println(cld3.get(Calendar.DATE));//25
	}
相關文章
相關標籤/搜索