java中Calendar類用法實(shí)例詳解
本文實(shí)例講述了java中Calendar類用法。分享給大家供大家參考,具體如下:
java中的Calendar在開發(fā)中經(jīng)常被忽略,這篇博客總結(jié)一下這個類,對后面項(xiàng)目中使用時期的時候有幫助。
Calendar常量(field)的作用
Calendar cal = Calendar.getInstance(); cal.get(Calendar.DATE);//-----------------------當(dāng)天 1-31 cal.get(Calendar.DAY_OF_MONTH);//---------------當(dāng)天 1-31 cal.get(Calendar.DAY_OF_WEEK);//----------------從星期天開始計算,如果今天星期二,那么返回3 cal.get(Calendar.DAY_OF_YEAR);//---------------- cal.get(Calendar.HOUR);//-----------------------12小時制 cal.get(Calendar.HOUR_OF_DAY);//----------------24小時制,一般使用這個屬性賦值 cal.get(Calendar.MILLISECOND);//---------------- cal.get(Calendar.MINUTE);//--------------------- cal.get(Calendar.SECOND);//--------------------- cal.get(Calendar.WEEK_OF_MONTH);//-------------- cal.get(Calendar.WEEK_OF_YEAR);//--------------- cal.get(Calendar.MONTH);//-----------------------月份獲取需要 +1,那么,賦值時需要 -1
總結(jié):
1)常量的真正意義如上,我們一般使用這些常量進(jìn)行賦值,換句話說,可以通過它獲取值同樣可以通過它進(jìn)行對應(yīng)賦值
2)賦值時,week 與 month 是很值得注意的,week 需要指定 setFirstDayOfWeek , 然而,月份則需要加減 1
3)賦值時,我們一般采用 年 月 日 時 分 秒
Calendar.YEAR、Calendar.MONTH 、Calendar.DAY_OF_MONTH、 Calendar.HOUR_OF_DAY 、Calendar.MINUTE、 Calendar.SECOND
主要賦值語句
cal.set(Calendar.XXX, VVVV);//--------------------- 對以上每個字段(field)進(jìn)行賦值,代碼重復(fù)較大 cal.set(year,month,date,hour,minute,second);//----- 分別對字段(field)進(jìn)行賦值,效率高
主要計算
cal1.roll(Calendar.MONTH,3);//---------------------- 一般不使用,原因是該方法只在一個月里面循環(huán)計算,其大小不會超過該月最值 cal1.add(Calendar.YEAR,-1);//----------------------- 使用 XX_OF_XX 的field進(jìn)行加減計算效果更佳,而且計算準(zhǔn)確 cal1.add(field,value);//----------------------------
總結(jié):
1)關(guān)于roll的計算,cal.roll(Calendar.DAY_OF_MONTH, 32);雖然32已經(jīng)超出了最大的可能31,但是cal實(shí)際是不會超出該月的,而是把32減去該月天數(shù)之后,重新計算剩下的天數(shù);
2)關(guān)于add的計算,cal1.add(Calendar.MONTH, 1); 如果當(dāng)前為8-31,那么,加一個月的話就是9-30,這個才是真正的準(zhǔn)確
主要取值語句
cal.getMaximum(Calendar.DATE); cal.get(Calendar.DATE); cal.getMinimum(Calendar.DATE); cal.setTimeInMillis(cal.getTime().getTime()); cal.setTimeInMillis(new Date().getTime());
總結(jié):
1)獲取最大值,最小值是很常用的方法
2)獲取毫秒數(shù)后,可以通過 1000*60*60 進(jìn)行計算
Calendar 獲取當(dāng)天\當(dāng)月\當(dāng)周
// 當(dāng)天
public String getThisToday(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND,0);
String start = sdf.format(cal.getTime());
cal.set(Calendar.HOUR_OF_DAY,23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND,59);
String end = sdf.format(cal.getTime());
return start+"|"+end;
}
// 本周
public String getThisWeekDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar ca = Calendar.getInstance();
ca.setFirstDayOfWeek(Calendar.MONDAY);
ca.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONTH),ca.get(Calendar.DATE),23,59,59);
String end = sdf.format(ca.getTime());
ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
ca.set(Calendar.HOUR_OF_DAY,0);
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND,0);
String start = sdf.format(ca.getTime());
return start+"|"+end;
}
//本月日期段
public String getThisMonthDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cc2 = Calendar.getInstance();
int maxMonthDay = cc2.getActualMaximum(Calendar.DAY_OF_MONTH);
cc2.set(cc2.get(Calendar.YEAR), cc2.get(Calendar.MONTH),maxMonthDay,23,59,59);
String end = sdf.format(cc2.getTime());
cc2.set(cc2.get(Calendar.YEAR), cc2.get(Calendar.MONTH),1,0,0,0);
String start = sdf.format(cc2.getTime());
return start+"|"+end;
}
PS:這里再為大家推薦幾款關(guān)于日期與天數(shù)計算的在線工具供大家使用:
在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線萬年歷日歷:
http://tools.jb51.net/bianmin/wannianli
在線陰歷/陽歷轉(zhuǎn)換工具:
http://tools.jb51.net/bianmin/yinli2yangli
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《java日期與時間操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
Java 本地方法Native Method詳細(xì)介紹
這篇文章主要介紹了 Java 本地方法Native Method詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02
Spring?Boot?2.6.x整合Swagger啟動失敗報錯問題的完美解決辦法
這篇文章主要給大家介紹了關(guān)于Spring?Boot?2.6.x整合Swagger啟動失敗報錯問題的完美解決辦法,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-03-03

