The Calendar class is an abstract class that provides methods for converting between specific moments and a set of calendar fields such as year, MONTH, Day_of_month, HOUR, and provides methods for manipulating calendar fields, such as getting the date of the next week. The instantaneous value is expressed as a millisecond, which is the offset from the calendar element (that is, Greenwich Mean Time January 1, 1970, 00:00:00.000, Gregorian calendar). As with other locales, Calendar provides a class method, getinstance, to obtain a generic object of this type. The getinstance method of the calendar returns a Calendar object (the object that is a child of the calendar) with the current date and time initialized:
Calendar RightNow = Calendar.getinstance ();
Source Code implementation:
1 Public Static Calendar getinstance () 2 {3 Calendar cal = Createcalendar (timezone.getdefaultref (), Locale.getdefault (Locale.Category.FORMAT)); 4 true ; 5 return Cal; 6 }
1 Private StaticCalendar Createcalendar (TimeZone zone,2 Locale Alocale)3 {4Calendar cal =NULL;5 6String Caltype = Alocale.getunicodelocaletype ("Ca");7 if(Caltype = =NULL) {8 //Calendar type is not specified.9 //If The specified locale is a Thai locale,Ten //returns a Buddhistcalendar instance. One if("th". Equals (Alocale.getlanguage ()) A&& ("TH". Equals (Alocale.getcountry ()))) { -Cal =NewBuddhistcalendar (zone, Alocale); -}Else { theCal =NewGregorianCalendar (zone, Alocale); - } -}Else if(Caltype.equals ("Japanese")) { -Cal =NewJapaneseimperialcalendar (zone, Alocale); +}Else if(Caltype.equals ("Buddhist")) { -Cal =NewBuddhistcalendar (zone, Alocale); +}Else { A //unsupported calendar type. at //Use Gregorian calendar as a fallback. -Cal =NewGregorianCalendar (zone, Alocale); - } - - returnCal; -}
Different date subclasses are returned according to different regions.
Here's a look at the common methods of calendar
1 PackageCom.test.calendar;2 3 ImportJava.util.Calendar;4 5 ImportOrg.junit.Before;6 Importorg.junit.Test;7 8 Public classCalendardemo {9Calendar Calendar =NULL;Ten One @Before A Public voidTest () { -Calendar =calendar.getinstance (); - } the - //basic usage, date and time of day and week - @Test - Public voidtest1 () { + //Get year - intYear =Calendar.get (calendar.year); + A //To get the month, it takes a range of months to 0~11, so it takes +1 for the month to get the current month value at intmonth = Calendar.get (calendar.month) + 1; - - //Get Day - intDay =Calendar.get (calendar.day_of_month); - - //when acquired in inthour =Calendar.get (calendar.hour); - //int hour = Calendar.get (calendar.hour_of_day);//24-hour indication to + //Get points - intminute =Calendar.get (calendar.minute); the * //Get seconds $ intSecond =Calendar.get (calendar.second);Panax Notoginseng - //Week, English-speaking countries start counting from Sunday the intWeekday =Calendar.get (calendar.day_of_week); + ASystem.out.println ("Now is" + year + "years" + month + "Month" + Day + "days" +Hour the+ "Time" + Minute + "min" + second + "seconds" + "Week" +weekday); + } - $ //one year later, today $ @Test - Public voidtest2 () { - //in the same vein as next month's Today Calendar.add (Calendar.month, 1); theCalendar.add (Calendar.year, 1); - Wuyi //Get year the intYear =Calendar.get (calendar.year); - Wu //Get Month - intmonth = Calendar.get (calendar.month) + 1; About $ //Get Day - intDay =Calendar.get (calendar.day_of_month); - -System.out.println ("A year later today:" + year + "years" + month + "Month" + Day + "Days")); A } + the //get the last day of any one month - @Test $ Public voidtest3 () { the //Suppose the last day of June the intCurrentmonth = 6; the //First day of July, in fact, here 6 is the Currentmonth variable passed in externally the //1 -Calendar.set (Calendar.get (calendar.year), Currentmonth, 1); in theCalendar.add (Calendar.date, 1); the About //Get Day the intDay =Calendar.get (calendar.day_of_month); the theSystem.out.println ("The last day of June is" + Days + "); + } - the //Set DateBayi @Test the Public voidtest4 () { theCalendar.set (Calendar.year, 2000); -System.out.println ("Now is" + calendar.get (calendar.year) + "year"); - theCalendar.set (2008, 8, 8); the //Get year the intYear =Calendar.get (calendar.year); the - //Get Month the intmonth =Calendar.get (calendar.month); the the //Get Day94 intDay =Calendar.get (calendar.day_of_month); the theSystem.out.println ("Now is" + year + "years" + month + "Month" + Day + "Days")); the }98}
Program Output Result:
1 It's November 7, 2016, 11:42, 18 seconds, 2 . 2 one year later today: November 7, 2017 3 the last day of June is number 30th. 4 It's 2000 . 5 It's August 8, 2008.
The Calendar class also has methods such as Before,after,compareto, which are similar to the date class, but are now recommended for the Calendar class operation date.
The Java foundation of the Calendar class