Prior knowledge (on time)
- Measurement Unit: ms
- A variable representing the number of milliseconds Type: lang
- Time: at 00:00 on July 1, 1970
Date class
- A date very primitive class, now basically abandoned, only briefly here
Common method
- new Date (): Returns the current time is a time of the object
- new Date (long time): Returns the object represents the time after the standard time is time milliseconds
- getTime (): returns the number of milliseconds from the standard time
Test implementation
1 // Create the current time of the object 2 a Date DA1 = new new a Date (); . 3 // Create Time after time after the target 400,000,000 milliseconds . 4 a Date DA2 = new new a Date (400,000,000);
DateFormat class
- Abstract classes, subclasses need to implement SimpleDateFormate
- Formatting the output time for the object
SimpleDateFormat类
- DateFormat class inheritance to achieve
Format format
- yyyy-MM-dd hh: mm: ss in brackets
- Extraction of need, when the month or in several other dates, defined as only a few specific elements of the format on the line
- Special markers do not change, they have an ability to specify the variables can be identified
- Other such as "-", ":" You can change the date
Common method
- new SimpleDateFormat ( "yyyy-MM-dd hh: mm: ss") defines a format object formatted
- formate (target time) after the time of the object in terms of return string
- parse (specific string representing the date of the date format) represented by the character string recognition time format specified time into the object
Test implementation
1 // Create the current time of the object 2 a Date DA1 = new new a Date (); . 3 // custom formatter formats . 4 DateFormat DF1 = new new the SimpleDateFormat ( "the MM-dd-YYYY HH: mm: SS" ); . 5 // formatted output . 6 String str1 = df1.format (DA1); . 7 System.out.println (str1); // 2020-01-09 02:43:38 . 8 // another format . 9 DateFormat DF2 = new new the SimpleDateFormat ( "YYYY Year when mM month dd hh mm ss a day " ); 10 String str2 = df2.format (DA1); . 11 System.out.println (str2); // 2020 on January 2 when 09 minutes 38 seconds 43 12 // formatted input 13 a Date DA2 = df2.parse ( "2020 January 9 02 hours 00 minutes 38 seconds "); // here to throw an error 14 a Date DA3 = df2.parse (" 2020 at 2:00:38 on January 9 " ); 15 System.out.println (DA2); // Thu 02 Jan 09 : 00: 38 is CST 2020 16 System.out.println (DA3); // Thu Jan 09 02:00:38 CST 2020, so there is no 0 is the same, there may be some automatic conversion of 17 // get a particular element 18 DF3 = DateFormat new new SimpleDateFormat ( "yyyy Year" ); 19 String year = df3.format (DA1); 20 System.out.println(year);//2020年
Canlendar class
- Abstract class, subclass of GregorianCalendar need to be implemented
- Examples of the addition time for the object, the relevant display and provide a calendar calculating function
GregorianCalendar class
- Class is a subclass of Canlendar implementation class
- Will print out a bunch of attributes of an object instance is directly output after the printing
Construction method
- 传参方式(year,month,day)、(year,month,day,hour,minute.........)
- Do not pass the reference date also represents the current object
Common method
- get (. Calendar element) to obtain a time element (such as YEAR, MONTH, Day OF WEEK, etc.) (Note: 0 corresponds to January 1 Sunday 2 Monday)
- set (Calendar. element values) the assignment of a time element
- add (Calendar. element value) positive values indicate a new object represents the current time after many years (month, day), how many years (month, day) before the negative representation
- getTime () time is converted to an object of type Date
- setTime (Date da) Date Type Type Switch Calendar
- getActualMaximum (Calendar. elements) to obtain the maximum value of an element is currently located in the peripheral elements
- You can try to use a method of encapsulation format conversion implemented
Test implementation
1 // initialize the calendar object 2 Calendar CAN1 = new new the GregorianCalendar (2020,3,5 ); . 3 Calendar CAN2 = new new the GregorianCalendar (2020,3,5,5,6,3 ); . 4 // get a specific element . 5 int year = can1.get (Calendar.YEAR); . 6 int day = can1.get (Calendar.DATE); . 7 System.out.println (year + "in" + day + "days" ); 8 // set a specific element . 9 can1.set (Calendar.YEAR, 1999 ); 10 int Yearn = can1.get (Calendar.YEAR); . 11 System.out.println(yearn+"年"+day+"天"); 12 //加减运算 13 can1.add(Calendar.YEAR, 10); 14 System.out.println(can1.get(Calendar.YEAR)+"年"); 15 can1.add(Calendar.YEAR, -10); 16 System.out.println(can1.get(Calendar.YEAR)+"年"); 17 //与Date之间的转换 18 Date da=can1.getTime(); 19 System.out.println(da); 20 can1.setTime(da); 21 System.out.println(can1.get(Calendar.YEAR)+"年"+can1.get(Calend 22 #######################
2020 5 days in 1999, five days 2009 1999 Mon Apr 05 00:00:00 CST 1999 1999 5 days