Java Calendar日歷類原理及使用方法
這篇文章主要介紹了Java Calendar日歷類原理及使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
java.util.Calendar 是日歷類,在Date后出現(xiàn),替換掉了許多Date的方法。該類將所有可能用到的時(shí)間信息封裝為靜態(tài)成員變量,方便獲取。日歷類就是方便獲取各個(gè)時(shí)間屬性的。Calendar類無法直接創(chuàng)建對象使用,里邊有一個(gè)靜態(tài)方法getInstance(),該方法返回了Calendar類的子類對象。Calendar類中提供很多成員常量,代表給定的日歷字段:具體如下圖
1
獲取方式
Calendar為抽象類,由于語言敏感性,Calendar類在創(chuàng)建對象時(shí)并非直接創(chuàng)建,而是通過靜態(tài)方法創(chuàng)建,返回子類對象,如下:
Calendar靜態(tài)方法
public static Calendar getInstance() :使用默認(rèn)時(shí)區(qū)和語言環(huán)境獲得一個(gè)日歷
//獲取Calendar類的子類對象 Calendar cal = Calendar.getInstance();
常用方法
根據(jù)Calendar類的API文檔,常用方法有:
public int get(int field) :返回給定日歷字段的值。
package demo04; import java.util.Calendar; public class Demo01 { public static void main(String[] args) { //通過靜態(tài)方法創(chuàng)建,返回Calendar子類對象 Calendar instance = Calendar.getInstance(); /* public int get(int field):返回給定日歷字段的值。 參數(shù):傳遞指定的日歷字段(YEAR,MONTH...) 返回值:日歷字段代表的具體的值 */ System.out.print("現(xiàn)在是"+instance.get(Calendar.YEAR)+"年"); //在Calendar類中,月份的表示是以0-11代表1-12月。 System.out.print(instance.get(Calendar.MONTH)+"月"); System.out.println(instance.get(Calendar.DAY_OF_MONTH)+"日"); } }
代碼執(zhí)行后的結(jié)果
2
public void set(int field, int value) :將給定的日歷字段設(shè)置為給定值。
package demo04; import java.util.Calendar; public class Demo02 { /* public void set(int field, int value):將給定的日歷字段設(shè)置為給定值。 參數(shù): int field:傳遞指定的日歷字段(YEAR,MONTH...) int value:給指定字段設(shè)置的值 */ public static void main(String[] args) { //使用getInstance方法獲取Calendar對象 Calendar c = Calendar.getInstance(); //設(shè)置年為9999 c.set(Calendar.YEAR, 9999); //設(shè)置月為9月 c.set(Calendar.MONTH, 9); //設(shè)置日9日 c.set(Calendar.DATE, 9); //獲取設(shè)置后的日期 System.out.println("現(xiàn)在的日期是" + c.get(Calendar.YEAR) + "年" + c.get(Calendar.MONTH) + "月" + c.get(Calendar.DAY_OF_MONTH) + "日"); //同時(shí)設(shè)置年月日,可以使用set的重載方法 c.set(8888, 8, 8); System.out.println("現(xiàn)在的日期是" + c.get(Calendar.YEAR) + "年" + c.get(Calendar.MONTH) + "月" + c.get(Calendar.DAY_OF_MONTH) + "日"); } }
代碼執(zhí)行后的結(jié)果
3
public abstract void add(int field, int amount) :根據(jù)日歷的規(guī)則,為給定的日歷字段添加或減去指定的時(shí)間量。
package demo04; import java.util.Calendar; public class Demo03 { /* public abstract void add(int field, int amount):根據(jù)日歷的規(guī)則,為給定的日歷字段添加或減去指定的時(shí)間量。 把指定的字段增加/減少指定的值 參數(shù): int field:傳遞指定的日歷字段(YEAR,MONTH...) int amount:增加/減少指定的值 正數(shù):增加 負(fù)數(shù):減少 */ public static void main(String[] args) { //使用getInstance方法獲取Calendar對象 Calendar c = Calendar.getInstance(); //獲取當(dāng)前的日期 System.out.println("現(xiàn)在的日期是" + c.get(Calendar.YEAR) + "年" + c.get(Calendar.MONTH) + "月" + c.get(Calendar.DAY_OF_MONTH) + "日"); //把年增加2年 c.add(Calendar.YEAR, 2); //把月份減少3個(gè)月 c.add(Calendar.MONTH, -3); //獲取設(shè)置后的日期 System.out.println("現(xiàn)在的日期是" + c.get(Calendar.YEAR) + "年" + c.get(Calendar.MONTH) + "月" + c.get(Calendar.DAY_OF_MONTH) + "日"); } }
代碼執(zhí)行后的結(jié)果
4
public Date getTime() :返回一個(gè)表示此Calendar時(shí)間值(從歷元到現(xiàn)在的毫秒偏移量)的Date對象。
package demo04; import java.util.Calendar; import java.util.Date; public class Demo04 { /* public Date getTime():返回一個(gè)表示此Calendar時(shí)間值(從歷元到現(xiàn)在的毫秒偏移量)的Date對象。 把日歷對象,轉(zhuǎn)換為日期對象 */ public static void main(String[] args) { //使用getInstance方法獲取Calendar對象 Calendar c = Calendar.getInstance(); //日歷對象--->日期對象 Date date = c.getTime(); System.out.println(date); } }
代碼執(zhí)行后的結(jié)果
5
注意事項(xiàng)
- 西方星期的開始為周日,中國為周一。
- 在Calendar類中,月份的表示是以0-11代表1-2月。
- 日期是有大小關(guān)系的,時(shí)間靠后,時(shí)間越大。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud版本問題報(bào)錯(cuò)及解決方法
這篇文章主要介紹了SpringCloud版本問題報(bào)錯(cuò)及解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07詳解jenkins自動部署springboot應(yīng)用的方法
這篇文章主要介紹了詳解jenkins自動部署springboot應(yīng)用的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08springboot接收J(rèn)SON實(shí)現(xiàn)示例解析
這篇文章主要為大家介紹了springboot如何接收J(rèn)SON的實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07調(diào)用Mybatis?plus中的saveBatch方法報(bào)找不到表的問題
在用Mybatis plus開發(fā)的項(xiàng)目中,用自帶的API批量保存的方法saveBatch操作時(shí),發(fā)現(xiàn)報(bào)沒有找到表的錯(cuò)誤,本文就來詳細(xì)的介紹一下解決方法,感興趣的可以了解一下2024-03-03SWT(JFace) 文本編輯器 實(shí)現(xiàn)代碼
SWT(JFace) 文本編輯器 實(shí)現(xiàn)代碼2009-06-06Java?List集合取交集的8種不同實(shí)現(xiàn)方式總結(jié)
工作中經(jīng)常遇到需要取兩個(gè)集合之間的交集、差集情況,下面這篇文章主要給大家總結(jié)介紹了關(guān)于Java?List集合取交集的8種不同實(shí)現(xiàn)方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04