java中的Calendar
在實際項目當中,我們經常會涉及到對時間的處理,例如登陸網站,我們會看到網站首頁顯示XXX,歡迎您!今天是XXXX年。。。。某些網站會記錄下用戶登陸的時間,比如銀行的一些網站,對于這些經常需要處理的問題,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的子類對象呢,因為每個國家地區都有自己的一套日歷算法,比如西方國家的第一個星期大部分為星期日,而中國則為星期一,我們來看看getInstance方法獲取實例的源碼
1 /**2 * Gets a calendar using the default time zone and locale. The3 * <code>Calendar</code> returned is based on the current time4 * in the default time zone with the default locale.5 *6 * @return a Calendar.7 */8 public static Calendar getInstance()9 { 10 Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT)); 11 cal.sharedZone = true; 12 return cal; 13 }其中createCalendar方法就是根據不同國家地區返回對應的日期子類
1 private static Calendar createCalendar(TimeZone zone,2 Locale aLocale)3 {4 Calendar cal = null;5 6 String caltype = aLocale.getUnicodeLocaleType("ca");7 if (caltype == null) {8 // Calendar type is not specified.9 // If the specified locale is a Thai locale, 10 // returns a BuddhistCalendar instance. 11 if ("th".equals(aLocale.getLanguage()) 12 && ("TH".equals(aLocale.getCountry()))) { 13 cal = new BuddhistCalendar(zone, aLocale); 14 } else { 15 cal = new GregorianCalendar(zone, aLocale); 16 } 17 } else if (caltype.equals("japanese")) { 18 cal = new JapaneseImperialCalendar(zone, aLocale); 19 } else if (caltype.equals("buddhist")) { 20 cal = new BuddhistCalendar(zone, aLocale); 21 } else { 22 // Unsupported calendar type. 23 // Use Gregorian calendar as a fallback. 24 cal = new GregorianCalendar(zone, aLocale); 25 } 26 27 return cal; 28 }為了更加便捷的對日期進行操作,Calendar類對YEAR、MONTH、DAY_OF_MONTH、HOUR等日歷字段之間的轉換提供了一些方法,并為操作日歷字段(例如獲得下星期的日期)提供了一些方法。瞬間可用毫秒值來表示,它是距歷元(即格林威治標準時間 1970 年 1 月 1 日的 00:00:00.000,格里高利歷)的偏移量。
下面看看Calendar常用的方法
1 package com.test.calendar;2 3 import java.util.Calendar;4 5 import org.junit.Before;6 import org.junit.Test;7 8 public class CalendarDemo {9 Calendar calendar = null; 10 11 @Before 12 public void test() { 13 calendar = Calendar.getInstance(); 14 } 15 16 // 基本用法,獲取年月日時分秒星期 17 @Test 18 public void test1() { 19 // 獲取年 20 int year = calendar.get(Calendar.YEAR); 21 22 // 獲取月,這里需要需要月份的范圍為0~11,因此獲取月份的時候需要+1才是當前月份值 23 int month = calendar.get(Calendar.MONTH) + 1; 24 25 // 獲取日 26 int day = calendar.get(Calendar.DAY_OF_MONTH); 27 28 // 獲取時 29 int hour = calendar.get(Calendar.HOUR); 30 // int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小時表示 31 32 // 獲取分 33 int minute = calendar.get(Calendar.MINUTE); 34 35 // 獲取秒 36 int second = calendar.get(Calendar.SECOND); 37 38 // 星期,英語國家星期從星期日開始計算 39 int weekday = calendar.get(Calendar.DAY_OF_WEEK); 40 41 System.out.println("現在是" + year + "年" + month + "月" + day + "日" + hour 42 + "時" + minute + "分" + second + "秒" + "星期" + weekday); 43 } 44 45 // 一年后的今天 46 @Test 47 public void test2() { 48 // 同理換成下個月的今天calendar.add(Calendar.MONTH, 1); 49 calendar.add(Calendar.YEAR, 1); 50 51 // 獲取年 52 int year = calendar.get(Calendar.YEAR); 53 54 // 獲取月 55 int month = calendar.get(Calendar.MONTH) + 1; 56 57 // 獲取日 58 int day = calendar.get(Calendar.DAY_OF_MONTH); 59 60 System.out.println("一年后的今天:" + year + "年" + month + "月" + day + "日"); 61 } 62 63 // 獲取任意一個月的最后一天 64 @Test 65 public void test3() { 66 // 假設求6月的最后一天 67 int currentMonth = 6; 68 // 先求出7月份的第一天,實際中這里6為外部傳遞進來的currentMonth變量 69 // 1 70 calendar.set(calendar.get(Calendar.YEAR), currentMonth, 1); 71 72 calendar.add(Calendar.DATE, -1); 73 74 // 獲取日 75 int day = calendar.get(Calendar.DAY_OF_MONTH); 76 77 System.out.println("6月份的最后一天為" + day + "號"); 78 } 79 80 // 設置日期 81 @Test 82 public void test4() { 83 calendar.set(Calendar.YEAR, 2000); 84 System.out.println("現在是" + calendar.get(Calendar.YEAR) + "年"); 85 86 calendar.set(2008, 8, 8); 87 // 獲取年 88 int year = calendar.get(Calendar.YEAR); 89 90 // 獲取月 91 int month = calendar.get(Calendar.MONTH); 92 93 // 獲取日 94 int day = calendar.get(Calendar.DAY_OF_MONTH); 95 96 System.out.println("現在是" + year + "年" + month + "月" + day + "日"); 97 } 98 }程序輸出結果:
1 現在是2016年11月7日11時42分18秒星期2 2 一年后的今天:2017年11月7日 3 6月份的最后一天為30號 4 現在是2000年 5 現在是2008年8月8日Calendar類中也有before,after,compareTo等方法,用法與Date類的類似,只是現在推薦用Calendar類操作日期。
轉載于:https://www.cnblogs.com/dayandday/p/10971914.html
總結
以上是生活随笔為你收集整理的java中的Calendar的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java使用strategy模式构造程序
- 下一篇: 假如一个程序员有社交牛逼症