Java Calendar類的詳解及使用實(shí)例
Java Calendar類的使用總結(jié)
在實(shí)際項(xiàng)目當(dāng)中,我們經(jīng)常會(huì)涉及到對(duì)時(shí)間的處理,例如登陸網(wǎng)站,我們會(huì)看到網(wǎng)站首頁顯示XXX,歡迎您!今天是XXXX年。。。。某些網(wǎng)站會(huì)記錄下用戶登陸的時(shí)間,比如銀行的一些網(wǎng)站,對(duì)于這些經(jīng)常需要處理的問題,Java中提供了Calendar這個(gè)專門用于對(duì)日期進(jìn)行操作的類,那么這個(gè)類有什么特殊的地方呢,首先我們來看Calendar的聲明
public abstract class Calendar extends Objectimplements Serializable, Cloneable, Comparable<Calendar>
該類被abstract所修飾,說明不能通過new的方式來獲得實(shí)例,對(duì)此,Calendar提供了一個(gè)類方法getInstance,以獲得此類型的一個(gè)通用的對(duì)象,getInstance方法返回一個(gè)Calendar對(duì)象(該對(duì)象為Calendar的子類對(duì)象),其日歷字段已由當(dāng)前日期和時(shí)間初始化:
Calendar rightNow = Calendar.getInstance();
為什么說返回的是Calendar的子類對(duì)象呢,因?yàn)槊總€(gè)國(guó)家地區(qū)都有自己的一套日歷算法,比如西方國(guó)家的第一個(gè)星期大部分為星期日,而中國(guó)則為星期一,我們來看看getInstance方法獲取實(shí)例的源碼
/**
* Gets a calendar using the default time zone and locale. The
* <code>Calendar</code> returned is based on the current time
* in the default time zone with the default locale.
*
* @return a Calendar.
*/
public static Calendar getInstance()
{
Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
cal.sharedZone = true;
return cal;
}
其中createCalendar方法就是根據(jù)不同國(guó)家地區(qū)返回對(duì)應(yīng)的日期子類
private static Calendar createCalendar(TimeZone zone,
Locale aLocale)
{
Calendar cal = null;
String caltype = aLocale.getUnicodeLocaleType("ca");
if (caltype == null) {
// Calendar type is not specified.
// If the specified locale is a Thai locale,
// returns a BuddhistCalendar instance.
if ("th".equals(aLocale.getLanguage())
&& ("TH".equals(aLocale.getCountry()))) {
cal = new BuddhistCalendar(zone, aLocale);
} else {
cal = new GregorianCalendar(zone, aLocale);
}
} else if (caltype.equals("japanese")) {
cal = new JapaneseImperialCalendar(zone, aLocale);
} else if (caltype.equals("buddhist")) {
cal = new BuddhistCalendar(zone, aLocale);
} else {
// Unsupported calendar type.
// Use Gregorian calendar as a fallback.
cal = new GregorianCalendar(zone, aLocale);
}
return cal;
}
為了更加便捷的對(duì)日期進(jìn)行操作,Calendar類對(duì)YEAR、MONTH、DAY_OF_MONTH、HOUR等日歷字段之間的轉(zhuǎn)換提供了一些方法,并為操作日歷字段(例如獲得下星期的日期)提供了一些方法。瞬間可用毫秒值來表示,它是距歷元(即格林威治標(biāo)準(zhǔn)時(shí)間 1970 年 1 月 1 日的 00:00:00.000,格里高利歷)的偏移量。
下面看看Calendar常用的方法
package com.test.calendar;
import java.util.Calendar;
import org.junit.Before;
import org.junit.Test;
public class CalendarDemo {
Calendar calendar = null;
@Before
public void test() {
calendar = Calendar.getInstance();
}
// 基本用法,獲取年月日時(shí)分秒星期
@Test
public void test1() {
// 獲取年
int year = calendar.get(Calendar.YEAR);
// 獲取月,這里需要需要月份的范圍為0~11,因此獲取月份的時(shí)候需要+1才是當(dāng)前月份值
int month = calendar.get(Calendar.MONTH) + 1;
// 獲取日
int day = calendar.get(Calendar.DAY_OF_MONTH);
// 獲取時(shí)
int hour = calendar.get(Calendar.HOUR);
// int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小時(shí)表示
// 獲取分
int minute = calendar.get(Calendar.MINUTE);
// 獲取秒
int second = calendar.get(Calendar.SECOND);
// 星期,英語國(guó)家星期從星期日開始計(jì)算
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println("現(xiàn)在是" + year + "年" + month + "月" + day + "日" + hour
+ "時(shí)" + minute + "分" + second + "秒" + "星期" + weekday);
}
// 一年后的今天
@Test
public void test2() {
// 同理換成下個(gè)月的今天calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.YEAR, 1);
// 獲取年
int year = calendar.get(Calendar.YEAR);
// 獲取月
int month = calendar.get(Calendar.MONTH) + 1;
// 獲取日
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("一年后的今天:" + year + "年" + month + "月" + day + "日");
}
// 獲取任意一個(gè)月的最后一天
@Test
public void test3() {
// 假設(shè)求6月的最后一天
int currentMonth = 6;
// 先求出7月份的第一天,實(shí)際中這里6為外部傳遞進(jìn)來的currentMonth變量
// 1
calendar.set(calendar.get(Calendar.YEAR), currentMonth, 1);
calendar.add(Calendar.DATE, -1);
// 獲取日
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("6月份的最后一天為" + day + "號(hào)");
}
// 設(shè)置日期
@Test
public void test4() {
calendar.set(Calendar.YEAR, 2000);
System.out.println("現(xiàn)在是" + calendar.get(Calendar.YEAR) + "年");
calendar.set(2008, 8, 8);
// 獲取年
int year = calendar.get(Calendar.YEAR);
// 獲取月
int month = calendar.get(Calendar.MONTH);
// 獲取日
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("現(xiàn)在是" + year + "年" + month + "月" + day + "日");
}
}
程序輸出結(jié)果:
現(xiàn)在是2016年11月7日11時(shí)42分18秒星期2 一年后的今天:2017年11月7日 6月份的最后一天為30號(hào) 現(xiàn)在是2000年 現(xiàn)在是2008年8月8日
Calendar類中也有before,after,compareTo等方法,用法與Date類的類似,只是現(xiàn)在推薦用Calendar類操作日期。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
SpringBoot實(shí)現(xiàn)本地存儲(chǔ)文件上傳及提供HTTP訪問服務(wù)的方法
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)本地存儲(chǔ)文件上傳及提供HTTP訪問服務(wù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Java自定義數(shù)組列表的實(shí)現(xiàn)操作
這篇文章主要介紹了Java自定義數(shù)組列表的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
SpringBoot如何讀取xml配置bean(@ImportResource)
這篇文章主要介紹了SpringBoot如何讀取xml配置bean(@ImportResource),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
快速解決commons-fileupload組件無法處理自定義head信息的bug
問題在于fileupload組件解析完自定義的head節(jié)點(diǎn)后,卻忘記傳遞到FileItemStreamImpl中了,稍作修訂,即可修正該bug2013-08-08
Java設(shè)計(jì)模式中的單一責(zé)任原則詳解
這篇文章主要介紹了Java設(shè)計(jì)模式中的單一責(zé)任原則詳解,應(yīng)該有且僅有一個(gè)原因引起類的變更,即單一指責(zé)原則要求一個(gè)借口或類只有一個(gè)原因引起變化,也就是一個(gè)接口或類只有一個(gè)職責(zé),它就負(fù)責(zé)一件事情,需要的朋友可以參考下2023-11-11
聊聊@RequestMapping和@GetMapping @PostMapping的區(qū)別
這篇文章主要介紹了@RequestMapping和@GetMapping及@PostMapping的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

