Java Date資料類型(非原創)

來源:互聯網
上載者:User
    Java資料類型中Date資料類型在處理一些日常應用問題常常被人們使用。

    然而在使用過程中許多人卻沒有真正的去瞭解有關於Java 資料類型中Date資料類型。

    通常在這使用某一種資料類型過程中並不一定就會有哦相應的資料類型,往往存在各種各樣的資料類型不斷的更替轉換。

    1.直接建立Date資料類型的執行個體變數date並直接將其輸出: 

    /** 
    * 建立Date類型的變數 this is date :Sat Dec 06 00:26:54 CST 2008 
    * 程式運行過程中直接將當前的時間列印出來 
    */ 
    Date date = new Date(); 
    System.out.println("this is date :" + date); 

    2.將直接執行個體化的Date資料以String的方式進行輸出: 

    ** 
    * 執行個體方法toString() Sat Dec 06 00:26:54 CST 2008 
    * 其實效果也是和程式的直接輸出一樣只不過將輸出的參數類型改變成為String的類型。 
    */ 
    String str_date = date.toString(); 
    System.out.println("this is date_ str :" + str_date); 

    3.採用DateFormat()方法格式化或者過濾所需要的資料參數: 

    /** 
    * 方法 :DateFormat.getInstance() 輸出 :08-12-6 上午12:26 
    * DateFormat()方法將日期格式化,格式輸出到當前日的分上面。 
    */ 
    String str_date_1 = DateFormat.getInstance().format(date); 
    System.out.println("this is str_date_1 :" + str_date_1); 

    4.採用SimpleDateFormat()方法格式化或者過濾所需要的資料參數: 

    /** 
    * 方法 :SimpleDateFormat() 輸出 :2008 12 06 00 26 54 
    * SimpleDateFormat()放法和DateFormat()類似他可以直接制定到當前日期的某一階段 例如執行個體指定當前的秒鐘。 
    */ 
    SimpleDateFormat time = new SimpleDateFormat("yyyy MM dd HH mm ss"); 
    System.out.println("this is SimpleDateFormat :" + time.format(date)); 

    5.採用MessageFormat()方法格式化或者過濾所需要的資料參數: 

    /** 
    * 方法 : MessageFormat() 輸出 :2008-12-06-00-26:54:2654 
    */ 
    String dateTime = MessageFormat.format( 
    "{0,date,yyyy-MM-dd-HH-mm:ss:ms}", 
    new Object[] { new java.sql.Date(System.currentTimeMillis()) }); 
    System.out.println("this is Message datetime :" + dateTime); 

    /** 
    * 方法 : MessageFormat() 輸出 :2008-12-06-00-26:54:2654 
    */ 
    String dateTime = MessageFormat.format( 
    "{0,date,yyyy-MM-dd-HH-mm:ss:ms}", 
    new Object[] { new java.sql.Date(System.currentTimeMillis()) }); 
    System.out.println("this is Message datetime :" + dateTime); 

    6.採用SimpleDateFormat()方法格式化或者過濾所需要的資料參數: 

    /** 
    * 方法 : SimpleDateFormat() 輸出 : 2008-12-06 
    * 類似於前面所講到的SimpleDateFormat()方法、其實原理都是相同的只不過在處理過程中採用別的附加條件。 
    * 
    */ 
    SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); // 格式化當前系統日期 
    String dateTime_1 = dateFm.format(new java.util.Date()); 
    System.out.println("this is SimpleDateFormat :" + dateTime_1); 

    7.採用DateFormat()方法格式化得到你所需要的Date參數: 

    /** 
    * 方法 :DateFormat() 
    * 通過DateFormat()方法所控制的不同參數來顯示當前日期時間 
    */ 
    //簡略的將當前日期時間顯示出來 
    DateFormat shortDateFormat = DateFormat.getDateTimeInstance( 
    DateFormat.SHORT, DateFormat.SHORT); 
    System.out.println(shortDateFormat.format(date)); 

    //精確地顯示當前日期時間 
    DateFormat mediumDateFormat = DateFormat.getDateTimeInstance( 
    DateFormat.MEDIUM, DateFormat.MEDIUM); 
    System.out.println(mediumDateFormat.format(date)); 

    //完全的將當前的日期時間顯示出來 
    DateFormat longDateFormat = DateFormat.getDateTimeInstance( 
    DateFormat.LONG, DateFormat.LONG); 
    System.out.println(longDateFormat.format(date)); 

    //全部標準化的將當前日期時間按輸出出來。 
    DateFormat fullDateFormat = DateFormat.getDateTimeInstance( 
    DateFormat.FULL, DateFormat.FULL); 
    System.out.println(fullDateFormat.format(date)); 

    8.同時可以通過getTime()方法擷取當前日期的時間: 

    不過這樣所得到的Date類型資料是以秒來計算的、並且是以1970年1月1日為開始的。在聲明資料類型時需要較大儲存空間使用Long資料類型或者同等儲存類型資料。 

    /** 
    * 方法 : getTime() 輸出 : 1228494414199 這個時間是按照1970年1月1日開始經曆的毫秒數了、 
    */ 
    long str_get = date.getTime(); 
    System.out.println("this is gettime :" + str_get);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.