Java 時(shí)間日期詳細(xì)介紹及實(shí)例
Java 時(shí)間日期
概要:
程序就是輸入——>處理——>輸出。對(duì)數(shù)據(jù)的處理是程序員需要著重注意的地方,快速、高效的對(duì)數(shù)據(jù)進(jìn)行處理時(shí)我們的追求。其中,時(shí)間日期的處理又尤為重要和平凡,此次,我將把Java中的時(shí)間日期處理方式進(jìn)行簡(jiǎn)單的解析,為自己以后的學(xué)習(xí)做一個(gè)備忘,也為初學(xué)者做一個(gè)借鑒。
時(shí)間,英文Time;日期,英文Date;日歷,英文Calendar。Java中注重語(yǔ)義化,也是用以上的名稱(chēng)對(duì)時(shí)間日期函數(shù)和相關(guān)類(lèi)進(jìn)行命名。
我們將以Java自帶的時(shí)間日期類(lèi)和其中的處理函數(shù)進(jìn)行分析。
一、與時(shí)間日期有關(guān)的類(lèi)。
java.util.Date。實(shí)現(xiàn)類(lèi),其對(duì)象具有時(shí)間、日期組件。
java.util.Calendar。抽象類(lèi),其對(duì)象具有時(shí)間、日期組件。
java.sql.Date。實(shí)現(xiàn)類(lèi),其對(duì)象具有日期組件。
java.sql.Time。實(shí)現(xiàn)類(lèi),其對(duì)象具有時(shí)間組件。
java.sql.Timestamp。實(shí)現(xiàn)類(lèi),其對(duì)象具有時(shí)間日期組件。
java.text.DateFormat。抽象類(lèi),其對(duì)象格式化時(shí)間日期。
java.text.DateFormatSymbols。實(shí)現(xiàn)類(lèi),其對(duì)象為格式化時(shí)間日期提供參數(shù)。
?。╯un.util.*canlender*.*。System。Local。TimeZone等)
由于jdk的安裝并沒(méi)有給出全部源碼,推薦大家獲取jdk全部源碼:jdk6u23-src.rar jdk7u4-src.rar。
二、類(lèi)之間的關(guān)系。
我們通過(guò)圖解和部分jdk源代碼來(lái)說(shuō)明?!?/p>
(上圖有幾處錯(cuò)誤,Calendar拼寫(xiě)錯(cuò)誤。)
以上的圖列出了部分常用的類(lèi)。我們一般會(huì)使用的類(lèi)java.util.Date、java.util.Calendar、java.sql.Timestamp、java.text.DateFormat進(jìn)行時(shí)間日期操作,因?yàn)樗麄冇型耆臅r(shí)間日期組件和全面的格式化功能。
值得注意的是:java.sql.Date沒(méi)有時(shí)間組件!而java.sql.Time沒(méi)有日期組件!再次提醒。什么意思呢?大家請(qǐng)看下面的代碼:
public static void main(String[] args) { /* * 以下代碼用于向大家展示各個(gè)時(shí)間日期類(lèi)對(duì)象的包含組件。 */ java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis()); System.out.println(sqlDate.toString()); // 輸出結(jié)果:2012-09-01 java.sql.Time sqlTime = new java.sql.Time(System.currentTimeMillis()); System.out.println(sqlTime.toString()); // 輸出結(jié)果:12:35:11 java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(System.currentTimeMillis()); System.out.println(sqlTimestamp.toString()); // 輸出結(jié)果:2012-09-01 12:36:33.544 java.util.Date utilDate = new java.util.Date(System.currentTimeMillis()); System.out.println(utilDate.toString()); // 輸出結(jié)果:Sat Sep 01 12:37:34 CST 2012 java.util.Calendar cl = java.util.Calendar.getInstance(); System.out.println(cl.getTime().toString()); // 輸出結(jié)果:Sat Sep 01 12:39:51 CST 2012 }
可以看到:java.util.Date、java.util.Calendar、java.sql.Timestamp具有的時(shí)間日期組件(而且他們具有無(wú)參構(gòu)造方法),java.sql.Date和java.sql.Time只有時(shí)間或日期組件。
為了證實(shí)以上言論,我將部分jdk源碼貼出來(lái)供大家參考。
java.sql.Date源代碼:
package java.sql; public class Date extends java.util.Date { // 省略部分代碼…… // Override all the time operations inherited from java.util.Date; /** * This method is deprecated and should not be used because SQL Date * values do not have a time component. * * @deprecated * @exception java.lang.IllegalArgumentException if this method is invoked * @see #setHours */ public int getHours() { throw new java.lang.IllegalArgumentException(); } /** * This method is deprecated and should not be used because SQL Date * values do not have a time component. * * @deprecated * @exception java.lang.IllegalArgumentException if this method is invoked * @see #setMinutes */ public int getMinutes() { throw new java.lang.IllegalArgumentException(); } /** * This method is deprecated and should not be used because SQL Date * values do not have a time component. * * @deprecated * @exception java.lang.IllegalArgumentException if this method is invoked * @see #setSeconds */ public int getSeconds() { throw new java.lang.IllegalArgumentException(); } /** * This method is deprecated and should not be used because SQL Date * values do not have a time component. * * @deprecated * @exception java.lang.IllegalArgumentException if this method is invoked * @see #getHours */ public void setHours(int i) { throw new java.lang.IllegalArgumentException(); } /** * This method is deprecated and should not be used because SQL Date * values do not have a time component. * * @deprecated * @exception java.lang.IllegalArgumentException if this method is invoked * @see #getMinutes */ public void setMinutes(int i) { throw new java.lang.IllegalArgumentException(); } /** * This method is deprecated and should not be used because SQL Date * values do not have a time component. * * @deprecated * @exception java.lang.IllegalArgumentException if this method is invoked * @see #getSeconds */ public void setSeconds(int i) { throw new java.lang.IllegalArgumentException(); } /** * Private serial version unique ID to ensure serialization * compatibility. */ static final long serialVersionUID = 1511598038487230103L; }
java.sql.Time源代碼:
// 省略部分源代碼…… /** * This method is deprecated and should not be used because SQL <code>TIME</code> * values do not have a year component. * * @deprecated * @exception java.lang.IllegalArgumentException if this * method is invoked * @see #setYear */ @Deprecated public int getYear() { throw new java.lang.IllegalArgumentException(); } /** * This method is deprecated and should not be used because SQL <code>TIME</code> * values do not have a month component. * * @deprecated * @exception java.lang.IllegalArgumentException if this * method is invoked * @see #setMonth */ @Deprecated public int getMonth() { throw new java.lang.IllegalArgumentException(); } /** * This method is deprecated and should not be used because SQL <code>TIME</code> * values do not have a day component. * * @deprecated * @exception java.lang.IllegalArgumentException if this * method is invoked */ @Deprecated public int getDay() { throw new java.lang.IllegalArgumentException(); } /** * This method is deprecated and should not be used because SQL <code>TIME</code> * values do not have a date component. * * @deprecated * @exception java.lang.IllegalArgumentException if this * method is invoked * @see #setDate */ @Deprecated public int getDate() { throw new java.lang.IllegalArgumentException(); } /** * This method is deprecated and should not be used because SQL <code>TIME</code> * values do not have a year component. * * @deprecated * @exception java.lang.IllegalArgumentException if this * method is invoked * @see #getYear */ @Deprecated public void setYear(int i) { throw new java.lang.IllegalArgumentException(); } /** * This method is deprecated and should not be used because SQL <code>TIME</code> * values do not have a month component. * * @deprecated * @exception java.lang.IllegalArgumentException if this * method is invoked * @see #getMonth */ @Deprecated public void setMonth(int i) { throw new java.lang.IllegalArgumentException(); } /** * This method is deprecated and should not be used because SQL <code>TIME</code> * values do not have a date component. * * @deprecated * @exception java.lang.IllegalArgumentException if this * method is invoked * @see #getDate */ @Deprecated public void setDate(int i) { throw new java.lang.IllegalArgumentException(); } /** * Private serial version unique ID to ensure serialization * compatibility. */ static final long serialVersionUID = 8397324403548013681L; }
從上面的代碼可以看出:java.sql.Date和java.sql.Time確實(shí)是不具有完整組件的!
我們?cè)俅卫么a來(lái)說(shuō)明:
public static void main(String[] args) { /* * 以下代碼用于向大家展示各個(gè)時(shí)間日期類(lèi)對(duì)象的包含組件。 */ java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis()); System.out.println(sqlDate.toString()); // 輸出結(jié)果:2012-09-01 java.sql.Time sqlTime = new java.sql.Time(System.currentTimeMillis()); System.out.println(sqlTime.toString()); // 輸出結(jié)果:12:35:11 java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(System.currentTimeMillis()); System.out.println(sqlTimestamp.toString()); // 輸出結(jié)果:2012-09-01 12:36:33.544 java.util.Date utilDate = new java.util.Date(System.currentTimeMillis()); System.out.println(utilDate.toString()); // 輸出結(jié)果:Sat Sep 01 12:37:34 CST 2012 java.util.Calendar cl = java.util.Calendar.getInstance(); System.out.println(cl.getTime().toString()); // 輸出結(jié)果:Sat Sep 01 12:39:51 CST 2012 /* * 以下代碼用于試驗(yàn)java.sql.Date和java.sql.Time是否具有完整組件。 */ System.out.println(); try { System.out.println(sqlDate.getHours()); } catch (Exception e) { System.out.println(e.getMessage()); // 輸出 null } try { System.out.println(sqlTime.getDate()); } catch (Exception e) { System.out.println(e.getMessage()); // 輸出 null } }
實(shí)驗(yàn)成功,所有給大家一個(gè)忠告:在進(jìn)行數(shù)據(jù)庫(kù)時(shí)間日期操作時(shí),使用java.sql.Timestamp類(lèi)。
那么很簡(jiǎn)單,如果您需要在程序中進(jìn)行完整的時(shí)間日期操作,推薦您使用java.util.Date+java.text.DateFormat。
如果您需要進(jìn)行復(fù)雜或深入的操作,您可以選擇java.util.Calendar。有人說(shuō)Calendar是Date的復(fù)雜版本,我覺(jué)得說(shuō)得有一些道理。我們可以通過(guò)他們的依賴(lài)對(duì)象(通過(guò)源碼文件中引入的外部類(lèi))來(lái)證實(shí)這個(gè)說(shuō)法:
java.util.Date:
package java.util; import java.text.DateFormat; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.lang.ref.SoftReference; import sun.util.calendar.BaseCalendar; import sun.util.calendar.CalendarDate; import sun.util.calendar.CalendarSystem; import sun.util.calendar.CalendarUtils; import sun.util.calendar.Era; import sun.util.calendar.Gregorian; import sun.util.calendar.ZoneInfo;
java.util.Calendar:
package java.util; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OptionalDataException; import java.io.Serializable; import java.security.AccessControlContext; import java.security.AccessController; import java.security.PermissionCollection; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import java.security.ProtectionDomain; import java.text.DateFormat; import java.text.DateFormatSymbols; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import sun.util.BuddhistCalendar; import sun.util.calendar.ZoneInfo; import sun.util.resources.LocaleData;
java.util.Date更多地用到了sun.util.*calendar*.*。而java.util.Calendar對(duì)他們的依賴(lài)則很少,并且Calendar中加入了更好的格式化功能等……(sun.util等源碼安裝jdk不會(huì)提供,我在頂部的下載連接中提供了)。
其實(shí)說(shuō)這么多都是廢話。對(duì)大家有用的東西無(wú)非只有兩點(diǎn):一是怎樣獲得時(shí)間日期,二是怎樣按照自定義格式顯示。
現(xiàn)在我才來(lái)講解以上兩點(diǎn):
大家可以通過(guò)java.util.Date date = new java.util.Date()或者java.util.Date date = java.util.Calendar.getInstance().getTime()獲得java.util.Date對(duì)象。至少我推薦這樣做,和數(shù)據(jù)庫(kù)打交道的話就用java.sql.Timestamp。
(而實(shí)際上jdk是不推薦我們使用java.util.Date對(duì)象來(lái)進(jìn)行時(shí)間日期獲取的,我們從java.util.Date類(lèi)方法注釋可以看到,基本所有的方法都有@Deprecated注解,而方法注釋大意則是"從JDK1.1開(kāi)始,我們推薦您使用Calendar的靜態(tài)成員和對(duì)象成員來(lái)對(duì)時(shí)間日期進(jìn)行操作"。我覺(jué)得其中的考慮可能有為了避免歧義吧,畢竟Date的意思是日期)
大家可以通過(guò)java.text.DateFormat或者他的直接實(shí)現(xiàn)類(lèi)java.text.SimpleDateFormat來(lái)實(shí)現(xiàn)時(shí)間日期的格式化。
下面的代碼會(huì)給大家展示如何格式化時(shí)間日期:
public static void main(String[] args) { /* * 以下代碼用于向大家展示各個(gè)時(shí)間日期類(lèi)對(duì)象的包含組件。 */ java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis()); System.out.println(sqlDate.toString()); // 輸出結(jié)果:2012-09-01 java.sql.Time sqlTime = new java.sql.Time(System.currentTimeMillis()); System.out.println(sqlTime.toString()); // 輸出結(jié)果:12:35:11 java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(System.currentTimeMillis()); System.out.println(sqlTimestamp.toString()); // 輸出結(jié)果:2012-09-01 12:36:33.544 java.util.Date utilDate = new java.util.Date(System.currentTimeMillis()); System.out.println(utilDate.toString()); // 輸出結(jié)果:Sat Sep 01 12:37:34 CST 2012 java.util.Calendar cl = java.util.Calendar.getInstance(); System.out.println(cl.getTime().toString()); // 輸出結(jié)果:Sat Sep 01 12:39:51 CST 2012 /* * 以下代碼用于試驗(yàn)java.sql.Date和java.sql.Time是否具有完整組件。 */ System.out.println(); try { System.out.println(sqlDate.getHours()); } catch (Exception e) { System.out.println(e.getMessage()); // 輸出 null } try { System.out.println(sqlTime.getDate()); } catch (Exception e) { System.out.println(e.getMessage()); // 輸出 null } /* * 下面的代碼給大家展示時(shí)間日期的格式化。 */ System.out.println(); java.text.DateFormat dateFormat = java.text.SimpleDateFormat.getInstance(); // java.util.Date原本的格式 System.out.println(utilDate.toString()); // 輸出:Sat Sep 01 13:16:13 CST 2012 // java.util.Date格式化后的格式 System.out.println(dateFormat.format(sqlDate)); // 輸出:12-9-1 下午1:16 System.out.println(); // 很多時(shí)候以上的結(jié)果并不是我們希望的,我們希望更加自由、更見(jiàn)簡(jiǎn)單的操作方式 // 此時(shí),java.text.SimpleDateFormat就成了我們的不二選擇 // SimpleDateFormat提供了無(wú)參和自定義格式參數(shù)的構(gòu)造方法使我們能夠輕松地實(shí)現(xiàn)自定義格式化 java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss a"); System.out.println(simpleDateFormat.format(sqlDate)); // 輸出:2012-09-01 13:20:41 下午 }
?。ㄎ也皇菫榱苏计刨N上來(lái)重復(fù)代碼的哦^_^)
java.text.SimpleDateFormat的format方法使用參數(shù)提供了強(qiáng)大的格式化功能(另外,值得一提的是:它的parse方法也提供了強(qiáng)大的字符串轉(zhuǎn)化為Date的功能)。您可以參照以下表格進(jìn)行選擇:
(上圖有一出錯(cuò)誤:m和mm中,前者表示當(dāng)分鐘數(shù)小于10會(huì)只占用一個(gè)輸出位,即輸出0-9而不會(huì)輸出00-09)
好了,大家趕緊利用jdk進(jìn)行時(shí)間日期的操作處理吧!
感謝閱讀,希望能幫助到大家,謝謝大家,對(duì)本站的支持!
- Java對(duì)日期Date類(lèi)進(jìn)行加減運(yùn)算、年份加減月份加減、時(shí)間差等等
- Java處理日期時(shí)間的方法匯總
- java分割日期時(shí)間段代碼
- Java 8中日期和時(shí)間的處理方法
- PHP與Java對(duì)比學(xué)習(xí)日期時(shí)間函數(shù)
- Java日期時(shí)間使用方法匯總
- 學(xué)習(xí)Java中的日期和時(shí)間處理及Java日歷小程序的編寫(xiě)
- Java中的日期和時(shí)間類(lèi)以及Calendar類(lèi)用法詳解
- java獲取當(dāng)前日期和時(shí)間的二種方法分享
- java獲取當(dāng)前時(shí)間和前一天日期(實(shí)現(xiàn)代碼)
- java計(jì)算兩個(gè)日期中間的時(shí)間
相關(guān)文章
Spring如何基于aop實(shí)現(xiàn)事務(wù)控制
這篇文章主要介紹了Spring如何基于aop實(shí)現(xiàn)事務(wù)控制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03SpringBoot中的static靜態(tài)資源訪問(wèn)、參數(shù)配置、代碼自定義訪問(wèn)規(guī)則詳解
這篇文章主要介紹了SpringBoot的static靜態(tài)資源訪問(wèn)、參數(shù)配置、代碼自定義訪問(wèn)規(guī)則,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07SpringCloud項(xiàng)目中集成Sentinel問(wèn)題
在SpringCloud項(xiàng)目中集成Sentinel,可以實(shí)現(xiàn)流量控制、熔斷降級(jí)等功能,提升系統(tǒng)穩(wěn)定性和可用性,集成步驟包括添加Sentinel依賴(lài)、配置控制臺(tái)地址、啟動(dòng)控制臺(tái)、配置限流熔斷規(guī)則、使用注解和集成SpringCloudGateway,這有助于處理高并發(fā)場(chǎng)景,保護(hù)服務(wù)穩(wěn)定運(yùn)行2024-10-10