JAVA深化_05——時(shí)間處理相關(guān)類
JAVA深化篇_05——時(shí)間處理相關(guān)類
時(shí)間處理相關(guān)類
“時(shí)間如流水,一去不復(fù)返”,時(shí)間是一維的。所以,我們需要一把刻度尺來(lái)表達(dá)和度量時(shí)間。在計(jì)算機(jī)世界,我們把**1970 年 1 月 1 日 00:00:00定為基準(zhǔn)時(shí)間,每個(gè)度量單位是毫秒**(1秒的千分之一),如圖所示。
我們用long類型的變量來(lái)表示時(shí)間,從基準(zhǔn)時(shí)間前后幾億年都能表示。
這個(gè)“時(shí)刻數(shù)值”是所有時(shí)間類的核心值,年月日都是根據(jù)這個(gè)“數(shù)值”計(jì)算出來(lái)的。
我們工作學(xué)習(xí)涉及的時(shí)間相關(guān)類有如下這些:
Date時(shí)間類(java.util.Date)
在標(biāo)準(zhǔn)Java類庫(kù)中包含一個(gè)Date類。它的對(duì)象表示一個(gè)特定的瞬間,精確到毫秒。
-
Date()分配一個(gè)Date對(duì)象,并初始化此對(duì)象為系統(tǒng)當(dāng)前的日期和時(shí)間,可以精確到毫秒)。 -
Date(long date)分配 Date 對(duì)象并初始化此對(duì)象,以表示自從標(biāo)準(zhǔn)基準(zhǔn)時(shí)間以來(lái)的毫秒數(shù)。 -
boolean equals(Object obj)比較兩個(gè)日期的相等性。 -
long getTime()返回毫秒數(shù)。 -
String toString()把此 Date 對(duì)象轉(zhuǎn)換為以下形式的 String:dow mon dd hh:mm:ss zzz yyyy其中:dow 是一周中的某一天 。
【示例】Date類的使用
long nowNum = System.currentTimeMillis(); //當(dāng)前時(shí)刻對(duì)應(yīng)的毫秒數(shù)
Date d = new Date(); //當(dāng)前時(shí)刻的對(duì)象
System.out.println(d.getTime()); //返回時(shí)間對(duì)應(yīng)的毫秒數(shù)
Date d2 = new Date(1000L * 3600 * 24 * 365 * 150); //距離1970年150年
System.out.println(d2);
DateFormat類和SimpleDateFormat類
·DateFormat類的作用
把時(shí)間對(duì)象轉(zhuǎn)化成指定格式的字符串。反之,把指定格式的字符串轉(zhuǎn)化成時(shí)間對(duì)象。
DateFormat是一個(gè)抽象類,一般使用它的的子類SimpleDateFormat類來(lái)實(shí)現(xiàn)。
StringBuffer | format(Date date, StringBuffer toAppendTo, FieldPosition pos) 給定的 Date進(jìn)入日期/時(shí)間線和附加的結(jié)果給出的 StringBuffer格式。 |
|---|---|
Date | parse(String text, ParsePosition pos) 解析文本字符串以產(chǎn)生 Date。 |
【示例】DateFormat類和SimpleDateFormat類的使用
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDateFormat {
public static void main(String[ ] args) throws ParseException {
// new出SimpleDateFormat對(duì)象
SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat s2 = new SimpleDateFormat("yyyy-MM-dd");
// 將時(shí)間對(duì)象轉(zhuǎn)換成字符串
String daytime = s1.format(new Date());
System.out.println(daytime);
System.out.println(s2.format(new Date()));
System.out.println(new SimpleDateFormat("hh:mm:ss").format(new Date()));
// 將符合指定格式的字符串轉(zhuǎn)成成時(shí)間對(duì)象.字符串格式需要和指定格式一致。
String time = "2049-10-1";
Date date = s2.parse(time);
System.out.println("date1: " + date);
time = "2049-10-1 20:15:30";
date = s1.parse(time);
System.out.println("date2: " + date);
}
}
代碼中的格式化字符的具體含義見表:
格式化字符的含義
| 字母 | 日期或時(shí)間元素 | 表示 | 示例 |
|---|---|---|---|
| G | Era 標(biāo)志符 | Text | AD |
| y | 年 | Year | 1996; 96 |
| M | 年中的月份 | Month | July; Jul; 07 |
| w | 年中的周數(shù) | Number | 27 |
| W | 月份中的周數(shù) | Number | 2 |
| D | 年中的天數(shù) | Number | 189 |
| d | 月份中的天數(shù) | Number | 10 |
| F | 月份中的星期 | Number | 2 |
| E | 星期中的天數(shù) | Text | Tuesday; Tue |
| a | Am/pm 標(biāo)記 | Text | PM |
| H | 一天中的小時(shí)數(shù)(0-23) | Number | 0 |
| k | 一天中的小時(shí)數(shù)(1-24) | Number | 24 |
| K | am/pm 中的小時(shí)數(shù)(0-11) | Number | 0 |
| h | am/pm 中的小時(shí)數(shù)(1-12) | Number | 12 |
| m | 小時(shí)中的分鐘數(shù) | Number | 30 |
| s | 分鐘中的秒數(shù) | Number | 55 |
| S | 毫秒數(shù) | Number | 978 |
| z | 時(shí)區(qū) | General time zone | Pacific Standard Time; PST; GMT-08:00 |
| Z | 時(shí)區(qū) | RFC 822 time zone | 0800 |
時(shí)間格式字符也可以為我們提供其他的便利。比如:獲得當(dāng)前時(shí)間是今年的第幾天。
【示例】獲取今天時(shí)本年度第幾天
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDateFormat2 {
public static void main(String[ ] args) {
SimpleDateFormat s1 = new SimpleDateFormat("D");
String daytime = s1.format(new Date());
System.out.println(daytime);
}
}
執(zhí)行結(jié)果如下所示:
137
?
浙公網(wǎng)安備 33010602011771號(hào)