java Date類的簡單使用

1. java.util.Date類

1.1 構造方法

    1. 第一個構造函數使用當前日期和時間來初始化對象。

      Date( )java

    1. 第二個構造函數接收一個參數,該參數是從1970年1月1日起的毫秒數。

      Date(long millisec)git

    1.2 方法

    序號 方法和描述
    1 boolean after(Date date) 若當調用此方法的Date對象在指定日期以後返回true,不然返回false。
    2 boolean before(Date date) 若當調用此方法的Date對象在指定日期以前返回true,不然返回false。
    3 Object clone( ) 返回此對象的副本。
    4 int compareTo(Date date) 比較當調用此方法的Date對象和指定日期。二者相等時候返回0。調用對象在指定日期以前則返回負數。調用對象在指定日期以後則返回正數。
    5 int compareTo(Object obj) 若obj是Date類型則操做等同於compareTo(Date) 。不然它拋出ClassCastException。
    6 boolean equals(Object date) 當調用此方法的Date對象和指定日期相等時候返回true,不然返回false。
    7 long getTime( ) 返回自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 對象表示的毫秒數。
    8 int hashCode( ) 返回此對象的哈希碼值。
    9 void setTime(long time) 用自1970年1月1日00:00:00 GMT之後time毫秒數設置時間和日期。
    10 String toString( ) 把此 Date 對象轉換爲如下形式的 String: dow mon dd hh:mm:ss zzz yyyy 其中: dow 是一週中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat)。

    2. java.text.SimpleDateFormat

    SimpleDateFormat 是一個以與語言環境有關的方式來格式化和解析日期的具體類。它容許進行格式化(日期 -> 文本)、解析(文本 -> 日期)和規範化app

    2.1 構造方法

    1. SimpleDateFormat()
      用默認的模式和默認語言環境的日期格式符號構造 SimpleDateFormat。
    2. SimpleDateFormat(String pattern)
      用給定的模式和默認語言環境的日期格式符號構造 SimpleDateFormat。
    3. SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)
      用給定的模式和日期符號構造 SimpleDateFormat。
    4. SimpleDateFormat(String pattern, Locale locale)
      用給定的模式和給定語言環境的默認日期格式符號構造 SimpleDateFormat。

    2.2 方法

    返回類型 方法
    void applyLocalizedPattern(String pattern) 將給定的本地化模式字符串應用於此日期格式。
    void applyPattern(String pattern) 將給定模式字符串應用於此日期格式。
    Object clone() 建立此 SimpleDateFormat 的一個副本。
    boolean equals(Object obj) 比較給定對象與此 SimpleDateFormat 的相等性。
    StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) 將給定的 Date 格式化爲日期/時間字符串,並將結果添加到給定的 StringBuffer。
    AttributedCharacterIterator formatToCharacterIterator(Object obj) 格式化生成 AttributedCharacterIterator 的對象。
    Date get2DigitYearStart() 返回在 100 年週期內被解釋的兩位數字年份的開始日期。
    DateFormatSymbols getDateFormatSymbols() 獲取此日期格式的日期和時間格式符號的一個副本。
    int hashCode() 返回此 SimpleDateFormat 對象的哈希碼值。
    Date parse(String text, ParsePosition pos) 解析字符串的文本,生成 Date。
    void set2DigitYearStart(Date startDate) 設置 100 年週期的兩位數年份,該年份將被解釋爲從用戶指定的日期開始。
    void setDateFormatSymbols(DateFormatSymbols newFormatSymbols) 設置此日期格式的日期和時間格式符號。
    String toLocalizedPattern() 返回描述此日期格式的本地化模式字符串。
    String toPattern() 返回描述此日期格式的模式字符串。

    2.3 日期時間模式

    字母 日期或時間元素 表示 示例
    G Era 標誌符 Text AD
    y Year 1996; 96
    M 年中的月份 Month July; Jul; 07
    w 年中的週數 Number 27
    W 月份中的週數 Number 2
    D 年中的天數 Number 189
    d 月份中的天數 Number 10
    F 月份中的星期 Number 2
    E 星期中的天數 Text Tuesday; Tue
    a Am/pm 標記 Text PM
    H 一天中的小時數(0-23) Number 0
    k 一天中的小時數(1-24) Number 24
    K am/pm 中的小時數(0-11) Number 0
    h am/pm 中的小時數(1-12) Number 12
    m 小時中的分鐘數 Number 30
    s 分鐘中的秒數 Number 55
    S 毫秒數 Number 978
    z 時區 General time zone Pacific Standard Time; PST; GMT-08:00
    Z 時區 RFC 822 time zone -800

    3. 簡單代碼示例

    3.1 demo代碼

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Main {
    
        public static void main(String[] args) {
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
            String s = format.format(date);
    
            System.out.println("格式化日期:"+s);
    
        }
    }

    3.2 打印結果

    格式化日期:2020-12-10 15:17:48

    4. 總結

    java8之後貌似推薦使用LocalDate函數

    相關文章
    相關標籤/搜索