@Test Public void Test () { /// because the calendar's construction method is private, instantiate a calendar object with the GetInstance method Calendar Calendar = calendar.getinstance (); -1); // get the day before Date date = calendar.gettime (); New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); System.out.println (Df.format (date)); }
This is a very good example, easy to understand, can help us to easily grasp the calendar of the use of methods. Can watch the source code, the underlying use of the map collection to achieve
PublicMap<string, Integer> getdisplaynames (intFieldintstyle, locale locale) { if(!checkdisplaynameparams (field, style, All_styles, LONG, Locale, Era_mask| month_mask| day_of_week_mask|am_pm_mask)) { return NULL; } //All_styles if(style = =all_styles) {Map<String,Integer> Shortnames =Getdisplaynamesimpl (field, short, locale); if(field = = ERA | | field = =am_pm) { returnShortnames; } Map<String,Integer> Longnames =getdisplaynamesimpl (field, LONG, locale); if(Shortnames = =NULL) { returnLongnames; } if(Longnames! =NULL) {shortnames.putall (longnames); } returnShortnames; } //Short or LONG returngetdisplaynamesimpl (field, style, locale); } PrivateMap<string,integer> Getdisplaynamesimpl (intFieldintstyle, locale locale) {DateFormatSymbols symbols=dateformatsymbols.getinstance (locale); String[] Strings=getfieldstrings (field, style, symbols); if(Strings! =NULL) {Map<String,Integer> names =NewHashmap<string,integer>(); for(inti = 0; i < strings.length; i++) { if(strings[i].length () = = 0) { Continue; } names.put (Strings[i], i); } returnnames; } return NULL; }
Calendar gets the current time before