JAVA-基礎-常用API(時間日期類,Date,Dateformat,Calendar)

來源:互聯網
上載者:User

標籤:calendar   根據   new   time()   calendar類   類型   抽象類別   欄位   string轉換   

 Date

類 Date 表示特定的瞬間,精確到毫秒。

毫秒概念:1000毫秒=1秒

毫秒的0點:

System.currentTimeMillis()  傳回值long型別參數

用於擷取當前日期的毫秒值

時間的原點:公元1970年 一月一日,午夜0:00:00 對應的毫秒值就是0

注意:時間和日期的計算,必須依賴毫秒值

繼續查閱Date類的描述,發現Date擁有多個建構函式,只是部分已經過時,但是其中有未過時的建構函式可以把毫秒值轉成日期對象。

 String ToString()   把此Date 類型轉換為以下形式 String down Mon dd hh:mm:ss zzz yyyy  已淘汰

 DateFormat.format(Date date)  

 

long getTime()   返回自 1970年1月1日 00:00:00 GMT 以來  此Date 對象表示的毫秒數

 DateFormat

DateFormat 是日期/時間格式化子類的抽象類別,它以與語言無關的方式格式化並解析日期或時間。

DateFormat 可協助進行格式化並解析任何語言環境的日期。對於月、星期,甚至日曆格式(陰曆和陽曆),其代碼可完全與語言環境的約定無關。

DateFormat是抽象類別,我們需要使用其子類SimpleDateFormat來建立對象。

構造方法:

SimpleDateFormat(String pattern)  用給定的模式和預設語言環境的日期格式符號構造 SimpleDateformat

DateFormat類方法:

String format(Date date)  將一個Date格式化為日期/時間 字串

代碼示範://建立日期格式化對象,在擷取格式化對象時可以指定風格DateFormat df= new SimpleDateFormat("yyyy-MM-dd");//對日期進行格式化Date date = new Date(1607616000000L);String str_time = df.format(date);System.out.println(str_time);//2020年12月11日

l  DateFormat類的作用:即可以將一個Date對象轉換為一個符合指定格式的字串,也可以將一個符合指定格式的字串轉為一個Date對象。

指定格式的具體規則我們可參照SimpleDateFormat類的說明,這裡做簡單介紹,規則是在一個字串中,會將以下字母替換成對應時間組成部分,剩餘內容原樣輸出:

當出現y時,會將y替換成年

當出現M時,會將M替換成月

當出現d時,會將d替換成日

當出現H時,會將H替換成時

當出現m時,會將m替換成分

當出現s時,會將s替換成秒

把String轉換成Date對象    String str = ”2020年12月11日”;    DateFormat df = new SimpleDateFormat(“yyyy年MM月dd日”);    Date date = df.parse( str );    //Date對象中的內容為Fri Dec 11 00:00:00 CST 2020

 

l  format方法,用來將Date對象轉換成String

l  parse方法,用來將String轉換成Date(轉換時,該String要符合指定格式,否則不能轉換)。

Date parse(String source)

 

       Calendar

Calendar是日曆類,在Date後出現,替換掉了許多Date的方法。該類將所有可能用到的時間資訊封裝為靜態成員變數,方便擷取。

Calendar為抽象類別,由於語言敏感性,Calendar類在建立對象時並非直接建立,而是通過靜態方法建立,將語言敏感內容處理好,再返回子類對象,

static Calendar  getInstance   使用預設時區和和語言環境來獲得一個日曆

Calendar c = Calendar.getInstance();-----------返回目前時間

 Calendar類常用方法

abstract void add (int field, int amount)  根據日曆規則 為給定的日曆欄位加減指定的時間量

int get(int filed)  返回給定日曆欄位的值

static Calendar getInstance()   使用預設時區和語言環境獲得一個日曆

Date getTime () 返回一個表示此 Calendar 時間值(從元年到現在的毫秒位移量)的Date對象

void set (int filed ,int value)

西方星期的開始為周日,中國為周一。

在Calendar類中,月份的表示是以0-11代表1-12月。

日期是有大小關係的,時間靠後,時間越大。

1.1     求出自己已經出生多少天

思路:

1.擷取目前時間對應的天數

         2.擷取自己出生日期對應的天數

         3.兩個時間相減(目前時間天數 – 出生日期天數)

代碼實現:

public static void main(String[] args){

    Calendar my = Calendar.getInstance();

    Calendar c = Calendar.getInstance();

    //設定出生年月日 1995-05-10

    my.set(Calendar.YEAR, 1995);

    my.set(Calendar.MONTH, 4);

    my.set(Calendar.DATE, 10);

    //擷取時間中的天數

    Calendar my = Calendar.getInstance();

    Calendar c = Calendar.getInstance();

    //設定年月日 2020-01-01

    my.set(Calendar.YEAR, 2020);

    my.set(Calendar.MONTH, 0);

    my.set(Calendar.DATE, 1);

    //擷取時間中的天數

    int day = c.get(Calendar.DATE);

    int myDay = my.get(Calendar.DATE);

    System.out.println(myDay - day);

}

    int day = c.get(Calendar.DATE);

    int myDay = my.get(Calendar.DATE);

    System.out.println(day - myDay);

}

JAVA-基礎-常用API(時間日期類,Date,Dateformat,Calendar)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.