最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 時(shí)間日期詳細(xì)介紹及實(shí)例

 更新時(shí)間:2017年01月13日 14:27:34   投稿:lqh  
這篇文章主要介紹了Java 時(shí)間日期詳細(xì)介紹及實(shí)例的相關(guān)資料,需要的朋友可以參考下

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ǔ)義化,也是用以上的名稱對(duì)時(shí)間日期函數(shù)和相關(guān)類進(jìn)行命名。

  我們將以Java自帶的時(shí)間日期類和其中的處理函數(shù)進(jìn)行分析。

一、與時(shí)間日期有關(guān)的類。

  java.util.Date。實(shí)現(xiàn)類,其對(duì)象具有時(shí)間、日期組件。

  java.util.Calendar。抽象類,其對(duì)象具有時(shí)間、日期組件。

  java.sql.Date。實(shí)現(xiàn)類,其對(duì)象具有日期組件。

  java.sql.Time。實(shí)現(xiàn)類,其對(duì)象具有時(shí)間組件。

  java.sql.Timestamp。實(shí)現(xiàn)類,其對(duì)象具有時(shí)間日期組件。

  java.text.DateFormat。抽象類,其對(duì)象格式化時(shí)間日期。

  java.text.DateFormatSymbols。實(shí)現(xiàn)類,其對(duì)象為格式化時(shí)間日期提供參數(shù)。

  (sun.util.*canlender*.*。System。Local。TimeZone等)

  由于jdk的安裝并沒(méi)有給出全部源碼,推薦大家獲取jdk全部源碼:jdk6u23-src.rar jdk7u4-src.rar。

二、類之間的關(guān)系。

  我們通過(guò)圖解和部分jdk源代碼來(lái)說(shuō)明。 

  

  (上圖有幾處錯(cuò)誤,Calendar拼寫(xiě)錯(cuò)誤。)

  以上的圖列出了部分常用的類。我們一般會(huì)使用的類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í)間日期類對(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í)間日期類對(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類。

  那么很簡(jiǎn)單,如果您需要在程序中進(jìn)行完整的時(shí)間日期操作,推薦您使用java.util.Date+java.text.DateFormat。

  如果您需要進(jìn)行復(fù)雜或深入的操作,您可以選擇java.util.Calendar。有人說(shuō)Calendar是Date的復(fù)雜版本,我覺(jué)得說(shuō)得有一些道理。我們可以通過(guò)他們的依賴對(duì)象(通過(guò)源碼文件中引入的外部類)來(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ì)他們的依賴則很少,并且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類方法注釋可以看到,基本所有的方法都有@Deprecated注解,而方法注釋大意則是"從JDK1.1開(kāi)始,我們推薦您使用Calendar的靜態(tài)成員和對(duì)象成員來(lái)對(duì)時(shí)間日期進(jìn)行操作"。我覺(jué)得其中的考慮可能有為了避免歧義吧,畢竟Date的意思是日期)

    大家可以通過(guò)java.text.DateFormat或者他的直接實(shí)現(xiàn)類java.text.SimpleDateFormat來(lái)實(shí)現(xiàn)時(shí)間日期的格式化。

    下面的代碼會(huì)給大家展示如何格式化時(shí)間日期:

public static void main(String[] args) {
    /*
     * 以下代碼用于向大家展示各個(gè)時(shí)間日期類對(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ì)本站的支持!

相關(guān)文章

  • JdbcTemplate方法介紹與增刪改查操作實(shí)現(xiàn)

    JdbcTemplate方法介紹與增刪改查操作實(shí)現(xiàn)

    這篇文章主要給大家介紹了關(guān)于JdbcTemplate方法與增刪改查操作實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用JdbcTemplate具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • SpringBoot多線程進(jìn)行異步請(qǐng)求的處理方式

    SpringBoot多線程進(jìn)行異步請(qǐng)求的處理方式

    這篇文章主要介紹了SpringBoot多線程進(jìn)行異步請(qǐng)求的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜
    2021-12-12
  • Spring?Data?Elasticsearch?5.0.x修改數(shù)據(jù)后無(wú)法立即刷新解決方法示例

    Spring?Data?Elasticsearch?5.0.x修改數(shù)據(jù)后無(wú)法立即刷新解決方法示例

    這篇文章主要為大家介紹了Spring?Data?Elasticsearch?5.0.x修改數(shù)據(jù)后無(wú)法立即刷新解決方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Java?spring?MVC環(huán)境中實(shí)現(xiàn)WebSocket的示例代碼

    Java?spring?MVC環(huán)境中實(shí)現(xiàn)WebSocket的示例代碼

    這篇文章主要介紹了Java?spring?MVC環(huán)境中實(shí)現(xiàn)WebSocket,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • Java 動(dòng)態(tài)代理深入理解

    Java 動(dòng)態(tài)代理深入理解

    這篇文章主要介紹了Java 動(dòng)態(tài)代理深入理解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Java內(nèi)存各部分OOM出現(xiàn)原因及解決方法(必看)

    Java內(nèi)存各部分OOM出現(xiàn)原因及解決方法(必看)

    下面小編就為大家?guī)?lái)一篇Java內(nèi)存各部分OOM出現(xiàn)原因及解決方法(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • Android、iOS和Java通用的AES128加密解密示例代碼

    Android、iOS和Java通用的AES128加密解密示例代碼

    現(xiàn)在很多App在與服務(wù)器接口的請(qǐng)求和響應(yīng)過(guò)程中,為了安全都會(huì)涉及到加密和解密的問(wèn)題,如果不加的話就會(huì)是明文的,即使加了GZIP也可以被直接解壓成明文。如果同時(shí)有Android和IOS的App的話、必須要保證加密和解密的算法一致、不然后臺(tái)沒(méi)法處理,下面通過(guò)這篇文章學(xué)習(xí)下。
    2016-11-11
  • kafka 啟動(dòng)報(bào)錯(cuò) missingTopicsFatal is true的解決

    kafka 啟動(dòng)報(bào)錯(cuò) missingTopicsFatal is true的解決

    這篇文章主要介紹了kafka 啟動(dòng)報(bào)錯(cuò) missingTopicsFatal is true的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java正則表達(dá)式詳解及實(shí)用案例(附詳細(xì)代碼)

    Java正則表達(dá)式詳解及實(shí)用案例(附詳細(xì)代碼)

    正則表達(dá)式是一種強(qiáng)大的字符串處理工具,用于匹配和解析文本,這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式詳解及實(shí)用案例的相關(guān)資料,本文通過(guò)代碼示例講解了正則表達(dá)式的基本語(yǔ)法規(guī)則,包括字符類、預(yù)定義字符類和數(shù)量詞,需要的朋友可以參考下
    2024-11-11
  • java上界通配符(? extends Type)的使用

    java上界通配符(? extends Type)的使用

    在Java中,? extends Type是一個(gè)上界通配符,本文主要介紹了java上界通配符(? extends Type)的使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01

最新評(píng)論

西丰县| 谢通门县| 台山市| 太康县| 金堂县| 永德县| 富顺县| 无为县| 石城县| 龙泉市| 科尔| 图片| 监利县| 南岸区| 曲周县| 广东省| 聊城市| 梨树县| 上林县| 新源县| 田东县| 石家庄市| 盐亭县| 鹿泉市| 分宜县| 安塞县| 神农架林区| 兴城市| 嵊州市| 南雄市| 武威市| 徐水县| 镶黄旗| 鄂尔多斯市| 武邑县| 松阳县| 扬中市| 霍山县| 松溪县| 石狮市| 澄城县|