国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java 時間日期詳細介紹及實例

2019-11-26 13:15:37
字體:
來源:轉載
供稿:網友

Java 時間日期

概要:

  程序就是輸入――>處理――>輸出。對數據的處理是程序員需要著重注意的地方,快速、高效的對數據進行處理時我們的追求。其中,時間日期的處理又尤為重要和平凡,此次,我將把Java中的時間日期處理方式進行簡單的解析,為自己以后的學習做一個備忘,也為初學者做一個借鑒。

  時間,英文Time;日期,英文Date;日歷,英文Calendar。Java中注重語義化,也是用以上的名稱對時間日期函數和相關類進行命名。

  我們將以Java自帶的時間日期類和其中的處理函數進行分析。

一、與時間日期有關的類。

  java.util.Date。實現類,其對象具有時間、日期組件。

  java.util.Calendar。抽象類,其對象具有時間、日期組件。

  java.sql.Date。實現類,其對象具有日期組件。

  java.sql.Time。實現類,其對象具有時間組件。

  java.sql.Timestamp。實現類,其對象具有時間日期組件。

  java.text.DateFormat。抽象類,其對象格式化時間日期。

  java.text.DateFormatSymbols。實現類,其對象為格式化時間日期提供參數。

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

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

二、類之間的關系。

  我們通過圖解和部分jdk源代碼來說明。 

  

  (上圖有幾處錯誤,Calendar拼寫錯誤。)

  以上的圖列出了部分常用的類。我們一般會使用的類java.util.Date、java.util.Calendar、java.sql.Timestamp、java.text.DateFormat進行時間日期操作,因為他們有完全的時間日期組件和全面的格式化功能。

  值得注意的是:java.sql.Date沒有時間組件!而java.sql.Time沒有日期組件!再次提醒。什么意思呢?大家請看下面的代碼:

public static void main(String[] args) {     /*     * 以下代碼用于向大家展示各個時間日期類對象的包含組件。     */     java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());     System.out.println(sqlDate.toString()); // 輸出結果:2012-09-01     java.sql.Time sqlTime = new java.sql.Time(System.currentTimeMillis());     System.out.println(sqlTime.toString()); // 輸出結果:12:35:11     java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(System.currentTimeMillis());     System.out.println(sqlTimestamp.toString()); // 輸出結果:2012-09-01 12:36:33.544     java.util.Date utilDate = new java.util.Date(System.currentTimeMillis());     System.out.println(utilDate.toString()); // 輸出結果:Sat Sep 01 12:37:34 CST 2012     java.util.Calendar cl = java.util.Calendar.getInstance();     System.out.println(cl.getTime().toString()); // 輸出結果:Sat Sep 01 12:39:51 CST 2012   }

  可以看到:java.util.Date、java.util.Calendar、java.sql.Timestamp具有的時間日期組件(而且他們具有無參構造方法),java.sql.Date和java.sql.Time只有時間或日期組件。

  為了證實以上言論,我將部分jdk源碼貼出來供大家參考。

  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確實是不具有完整組件的!

  我們再次利用代碼來說明:

public static void main(String[] args) {    /*     * 以下代碼用于向大家展示各個時間日期類對象的包含組件。     */    java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());    System.out.println(sqlDate.toString()); // 輸出結果:2012-09-01    java.sql.Time sqlTime = new java.sql.Time(System.currentTimeMillis());    System.out.println(sqlTime.toString()); // 輸出結果:12:35:11    java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(System.currentTimeMillis());    System.out.println(sqlTimestamp.toString()); // 輸出結果:2012-09-01 12:36:33.544    java.util.Date utilDate = new java.util.Date(System.currentTimeMillis());    System.out.println(utilDate.toString()); // 輸出結果:Sat Sep 01 12:37:34 CST 2012    java.util.Calendar cl = java.util.Calendar.getInstance();    System.out.println(cl.getTime().toString()); // 輸出結果:Sat Sep 01 12:39:51 CST 2012        /*     * 以下代碼用于試驗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    }  }

  實驗成功,所有給大家一個忠告:在進行數據庫時間日期操作時,使用java.sql.Timestamp類。

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

  如果您需要進行復雜或深入的操作,您可以選擇java.util.Calendar。有人說Calendar是Date的復雜版本,我覺得說得有一些道理。我們可以通過他們的依賴對象(通過源碼文件中引入的外部類)來證實這個說法:

  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對他們的依賴則很少,并且Calendar中加入了更好的格式化功能等……(sun.util等源碼安裝jdk不會提供,我在頂部的下載連接中提供了)。

  其實說這么多都是廢話。對大家有用的東西無非只有兩點:一是怎樣獲得時間日期,二是怎樣按照自定義格式顯示。

  現在我才來講解以上兩點:

    大家可以通過java.util.Date date = new java.util.Date()或者java.util.Date date = java.util.Calendar.getInstance().getTime()獲得java.util.Date對象。至少我推薦這樣做,和數據庫打交道的話就用java.sql.Timestamp。

    (而實際上jdk是不推薦我們使用java.util.Date對象來進行時間日期獲取的,我們從java.util.Date類方法注釋可以看到,基本所有的方法都有@Deprecated注解,而方法注釋大意則是"從JDK1.1開始,我們推薦您使用Calendar的靜態成員和對象成員來對時間日期進行操作"。我覺得其中的考慮可能有為了避免歧義吧,畢竟Date的意思是日期)

    大家可以通過java.text.DateFormat或者他的直接實現類java.text.SimpleDateFormat來實現時間日期的格式化。

    下面的代碼會給大家展示如何格式化時間日期:

public static void main(String[] args) {    /*     * 以下代碼用于向大家展示各個時間日期類對象的包含組件。     */    java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());    System.out.println(sqlDate.toString()); // 輸出結果:2012-09-01    java.sql.Time sqlTime = new java.sql.Time(System.currentTimeMillis());    System.out.println(sqlTime.toString()); // 輸出結果:12:35:11    java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(System.currentTimeMillis());    System.out.println(sqlTimestamp.toString()); // 輸出結果:2012-09-01 12:36:33.544    java.util.Date utilDate = new java.util.Date(System.currentTimeMillis());    System.out.println(utilDate.toString()); // 輸出結果:Sat Sep 01 12:37:34 CST 2012    java.util.Calendar cl = java.util.Calendar.getInstance();    System.out.println(cl.getTime().toString()); // 輸出結果:Sat Sep 01 12:39:51 CST 2012        /*     * 以下代碼用于試驗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    }        /*     * 下面的代碼給大家展示時間日期的格式化。     */    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();    // 很多時候以上的結果并不是我們希望的,我們希望更加自由、更見簡單的操作方式    // 此時,java.text.SimpleDateFormat就成了我們的不二選擇    // SimpleDateFormat提供了無參和自定義格式參數的構造方法使我們能夠輕松地實現自定義格式化    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 下午  }

  (我不是為了占篇幅才貼上來重復代碼的哦^_^)

  java.text.SimpleDateFormat的format方法使用參數提供了強大的格式化功能(另外,值得一提的是:它的parse方法也提供了強大的字符串轉化為Date的功能)。您可以參照以下表格進行選擇:

  

  (上圖有一出錯誤:m和mm中,前者表示當分鐘數小于10會只占用一個輸出位,即輸出0-9而不會輸出00-09)

  好了,大家趕緊利用jdk進行時間日期的操作處理吧!

感謝閱讀,希望能幫助到大家,謝謝大家,對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 刚察县| 岱山县| 罗城| 贞丰县| 奇台县| 灵璧县| 平南县| 泌阳县| 绥阳县| 阿拉善右旗| 石柱| 松潘县| 梓潼县| 宣化县| 休宁县| 阜康市| 项城市| 鄂托克旗| 丘北县| 搜索| 仁布县| 溧水县| 和平县| 济源市| 桑植县| 滁州市| 孙吴县| 霍山县| 屏山县| 元江| 澎湖县| 许昌县| 浦城县| 富蕴县| 凤台县| 宁夏| 兴业县| 商河县| 白河县| 宜阳县| 亳州市|