Java Calendar類使用案例詳解
在實際項目當中,我們經(jīng)常會涉及到對時間的處理,例如登陸網(wǎng)站,我們會看到網(wǎng)站首頁顯示XXX,歡迎您!今天是XXXX年。。。。某些網(wǎng)站會記錄下用戶登陸的時間,比如銀行的一些網(wǎng)站,對于這些經(jīng)常需要處理的問題,Java中提供了Calendar這個專門用于對日期進行操作的類,那么這個類有什么特殊的地方呢,首先我們來看Calendar的聲明
public abstract class Calendar extends Objectimplements Serializable, Cloneable, Comparable<Calendar>
該類被abstract所修飾,說明不能通過new的方式來獲得實例,對此,Calendar提供了一個類方法getInstance,以獲得此類型的一個通用的對象,getInstance方法返回一個Calendar對象(該對象為Calendar的子類對象),其日歷字段已由當前日期和時間初始化:
Calendar rightNow = Calendar.getInstance();
為什么說返回的是Calendar的子類對象呢,因為每個國家地區(qū)都有自己的一套日歷算法,比如西方國家的第一個星期大部分為星期日,而中國則為星期一,我們來看看getInstance方法獲取實例的源碼
/**
* 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ù)不同國家地區(qū)返回對應的日期子類
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;
}
為了更加便捷的對日期進行操作,Calendar類對YEAR、MONTH、DAY_OF_MONTH、HOUR等日歷字段之間的轉(zhuǎn)換提供了一些方法,并為操作日歷字段(例如獲得下星期的日期)提供了一些方法。瞬間可用毫秒值來表示,它是距歷元(即格林威治標準時間 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();
}
// 基本用法,獲取年月日時分秒星期
@Test
public void test1() {
// 獲取年
int year = calendar.get(Calendar.YEAR);
// 獲取月,這里需要需要月份的范圍為0~11,因此獲取月份的時候需要+1才是當前月份值
int month = calendar.get(Calendar.MONTH) + 1;
// 獲取日
int day = calendar.get(Calendar.DAY_OF_MONTH);
// 獲取時
int hour = calendar.get(Calendar.HOUR);
// int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小時表示
// 獲取分
int minute = calendar.get(Calendar.MINUTE);
// 獲取秒
int second = calendar.get(Calendar.SECOND);
// 星期,英語國家星期從星期日開始計算
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println("現(xiàn)在是" + year + "年" + month + "月" + day + "日" + hour
+ "時" + minute + "分" + second + "秒" + "星期" + weekday);
}
// 一年后的今天
@Test
public void test2() {
// 同理換成下個月的今天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 + "日");
}
// 獲取任意一個月的最后一天
@Test
public void test3() {
// 假設求6月的最后一天
int currentMonth = 6;
// 先求出7月份的第一天,實際中這里6為外部傳遞進來的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 + "號");
}
// 設置日期
@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é)果:
1 現(xiàn)在是2016年11月7日11時42分18秒星期2
2 一年后的今天:2017年11月7日
3 6月份的最后一天為30號
4 現(xiàn)在是2000年
5 現(xiàn)在是2008年8月8日
Calendar類中也有before,after,compareTo等方法,用法與Date類的類似,只是現(xiàn)在推薦用Calendar類操作日期。
到此這篇關(guān)于Java Calendar類使用案例詳解的文章就介紹到這了,更多相關(guān)Java Calendar類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring攔截器HandlerInterceptor接口代碼解析
這篇文章主要介紹了Spring攔截器HandlerInterceptor接口代碼解析,具有一定借鑒價值,需要的朋友可以參考下2017-12-12
spring mvc中@PathVariable / 帶斜杠方式獲取
這篇文章主要介紹了spring mvc中@PathVariable / 帶斜杠方式獲取,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
JAVA簡單鏈接Oracle數(shù)據(jù)庫 注冊和登陸功能的實現(xiàn)代碼
這篇文章主要介紹了JAVA鏈接Oracle并實現(xiàn)注冊與登錄功能的代碼實例,有需要的朋友可以參考一下2014-01-01
mybatis如何使用注解實現(xiàn)一對多關(guān)聯(lián)查詢
這篇文章主要介紹了mybatis如何使用注解實現(xiàn)一對多關(guān)聯(lián)查詢的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
關(guān)于Java?中?Future?的?get?方法超時問題
這篇文章主要介紹了Java?中?Future?的?get?方法超時,最常見的理解就是,“超時以后,當前線程繼續(xù)執(zhí)行,線程池里的對應線程中斷”,真的是這樣嗎?本文給大家詳細介紹,需要的朋友參考下吧2022-06-06

