Other Java APIs and Java APIs
Although some classes do not belong to the core classes in Java as classes in collections, multithreading, and network programming, they bring us a lot of convenience in the development process, here we will give a brief introduction and demonstration of them.
I. System class
The constructor in the System class is private, and the attributes and methods in the System class are static.
Common method: long currentTimeMills (): returns the current time in milliseconds.
SystemThe class contains some useful class fields and methods. The following demo obtains the information of the local system and prints them out.
1 package www. wsw. otherApi; 2 import java. util. properties; 3 import java. util. set; 4 5 public class SystemTest {6/** 7 * cross-platform separator. When running on any platform, call the line break of that platform. Common Information of the system can be defined as static fields for ease of use. 9 */10 public static String line_speparator = System. getProperty ("line. separator "); 11 12 public static void main (String [] args) {13 // common method: long currentTimeMills () 14 long l1 = System. currentTimeMillis (); // returns the current time, in milliseconds, 15 System. out. println (l1); // 149000571079216 demo_1 (); 17} 18 19 public static void demo_1 () {20/** 21 * obtain the keys and values of system information, and stored in the Properties set 22 * Properties is a subclass of HashTable, its keys and values are String type 23 * it is best to use the Properties method to store and retrieve elements 24 */25 Properties properties = System. getProperties (); 26 // Set <String> stringPropertyNames () returns the key Set 27 in this attribute list <String> nameSet = properties. stringPropertyNames (); 28 for (String name: nameSet) {29 String value = properties. getProperty (name); 30 System. out. println (name + ":" + value); 31} 32} 33 34}
Ii. Runtime class
Each Java application hasRuntimeClass instance, so that the application can be connected to the running environment. You can usegetRuntimeMethod to obtain the current runtime.
Applications cannot create their own Runtime instances.
The exec and destroy methods are available. For more information, see the demo.
1 package www. wsw. otherApi; 2 import java. io. IOException; 3 4 public class RuntimeDemo {5 6 public static void main (String [] args) throws IOException, InterruptedException {7/** 8 * Runtime no constructor abstract, this class cannot create objects. 9 * If the class has non-static methods, the Runtime class can return static methods of the class instance. 10 * finds that there is only one such method, the Runtime class uses the singleton design mode 11 */12 Runtime r = Runtime. getRuntime (); 13 // execute to execute exe14 Process p1_r.exe c ("notepad.exe D: \ abc \ MapDemo. java "); // open notepad and use it to parse a file under the d directory. 15 // Process is an abstract class 16 Thread. sleep (2000); 17 p. destroy (); // kill sub-process 18 19} 20 21}
Iii. Math class
* Math classes are all static mathematical operations.
* Common Methods:
* Ceil (): returns the smallest integer greater than or equal to the parameter.
* Floor (): returns the maximum integer smaller than or equal to the parameter.
* Round (): return the integer obtained by rounding the parameter.
* Pow (a, B): returns the power B of.
*
* Random (): returns a pseudo-random number in the range of [0, 1 ).
1 package www. wsw. otherApi; 2 3 public class MathDemo {4 5 public static void main (String [] args) {6 7 double d1 = Math. ceil (-7.67); //-7.0 8 double d2 = Math. floor (-7.67); //-8.0 9 double d3 = Math. round (-7.67); //-8.010 sop ("d1 =" + d1); 11 sop ("d2 =" + d2 ); 12 sop ("d3 =" + d3); 13 14 sop (Math. pow (100.015) + ""); // 16 // obtain a random number between 10 1-6 17 for (int I = 0; I <10; I ++) {18 // double d = (int) (Math. random () * 6) + 1; 19 double d = Math. ceil (Math. random () * 6); 20/** 21 * can also be written as: 22 * Random r = new Random (); // Random is located in Java. in the util package, the random number is encapsulated into the object 23 * provides more Methods 24 * int d = r. nextInt (6) + 1; 25 */26 27 sop (d + ""); 28} 29 30} 31 // conveniently output 32 public static void sop (String str) {33 System. out. println (str); 34} 35 36}
Iv. Date and DateFormat
The Date class belongs to the java. util package, ClassDateIndicates a specific moment, accurate to milliseconds.
It can be interpreted as the year, month, day, hour, minute, and second values. It also allows formatting and parsing date strings. However, the APIs of these functions are not easy to internationalize.
From JDK 1.1, you should use CalendarClass to convert the date and time fields.DateFormatClass to format and parse the date string.DateThe corresponding method in has been discarded.
DateFormat is an abstract class of the date/time format subclass of the java. text package. It formats and parses the date or time in a language-independent manner.
Converts a millisecond value to a Date object.
* 1. Construction Method of Date
* 2. Use the setTime Method
* Function: You can use the method in the Date class to operate the fields of the Date object (such as year, month, and day ).
*
*Converts a Date object to a millisecond value.
* 1. getTime Method
* Function: Calculate the millisecond value and time difference.
*
*Other methods:
* Boolean before (Date when): determines whether the Date is before the specified Date.
* Boolean after (): determines whether the date is after the specified date.
* Boolean equals (): determines whether the date is equal to the specified date.
* Int compareTo (): returns Positive and Negative zeros.
The following demo also involves two conversions: converting a Date string to a Date object and converting a Date object to a Date string in a certain format,
The parse method and format method in the DateFormat class are used respectively.
1 package www. wsw. otherApi; 2 3 import java. text. dateFormat; 4 import java. text. parseException; 5 import java. text. simpleDateFormat; 6 import java. util. date; 7 8 public class DateDemo {9 10 public static void main (String [] args) throws ParseException {11 12 methodDemo_3 (); 13} 14/** 15 * Date string --> date object 16 * @ throws ParseException date conversion exception 17 */18 public static void methodDemo_3 () throws ParseException {19 String str = "March 1, 2017"; 20 DateFormat dateFormat = new SimpleDateFormat ("yyyy -- MM -- dd"); 21/** 22 * String str = "; 23 * DateFormat dateFormat = DateFormat. getDateInstance (DateFormat. LONG); 24 */25 Date date = dateFormat. parse (str); 26 System. out. println (date ); // Wed Mar 29 00:00:00 CST 201727 28} 29/** 30 * format the date object 31 * format the date object --> The Date format String 32 * uses the format in DateFormat method 33 */34 public static void m EthodDemo_2 () {35 Date date = new Date (); 36 // get the Date formatter object. For the default style, you can specify 37 DateFormat dateFormat1 = DateFormat, such as Long full. getDateInstance (DateFormat. FULL); 38 // get date/time formatter object 39 DateFormat dateFormat2 = DateFormat. getDateTimeInstance (DateFormat. LONG, DateFormat. LONG); 40 41 // What if you want to use a custom format? 42 DateFormat dateFormat3 = new SimpleDateFormat ("MM Month ~ Dd-yyyy "); 43 String str1 = dateFormat1.format (date); 44 String str2 = dateFormat2.format (date); 45 String str3 = dateFormat3.format (date); 46 System. out. println (str1); // Wednesday, March 22, 2017, 47 System. out. println (str2); // March 22, 2017 02:14:06 P.M. 48 System. out. println (str3); // March ~ 22 -- 2017 49} 50 51 public static void methodDemo_1 () {52 long l = System. currentTimeMillis (); 53 Date d = new Date (); // encapsulate the current time as the Date object 54 Date d2 = new Date (1490023019659l ); // convert the millisecond value to the Date object 55 System. out. println (d); // Mon Mar 20 23:18:58 CST 201756 System. out. println (d2); // Mon Mar 20 23:16:59 CST 201757} 58 59}
Date exercises:
Exercise: How many days is the difference between "June 1, 2017" and "March 22, 2017?
* Idea:
* 1. convert a Date string to a Date object
* 2. Convert the Date object to the corresponding millisecond value, and then subtract
* 3. Convert the millisecond value to a day.
1 package www. wsw. otherApi; 2 3 import java. text. dateFormat; 4 import java. text. parseException; 5 import java. util. date; 6 7 public class DateTest {8 9 public static void main (String [] args) throws ParseException {10 int day = subDate ("May 1, 2017", "March 22, 2017 "); 11 System. out. println (day); 12} 13 14 public static int subDate (String str1, String str2) throws ParseException {15 DateFormat dateFormat = DateFormat. getDateInstance (DateFormat. LONG); 16 Date date1 = dateFormat. parse (str1); 17 Date date2 = dateFormat. parse (str2); 18 19 long time1 = date1.getTime (); 20 long time2 = date2.getTime (); 21 long time = time1-time2; 22 int day = getDay (time ); 23 return day; 24} 25/** 26 * convert the millisecond value to the number of days 27 * @ param time millisecond value 28 * @ return days 29 */30 public static int getDay (long time) {31 int day = (int) (time/1000/60/60/24); 32 return day; 33} 34 35}View Code
5. Calendar class
CalendarA class is an abstract class that corresponds to a groupYEAR,MONTH,DAY_OF_MONTH,HOURAnd so onCalendar FieldProvides some methods to convert between them,
It also provides some methods to operate calendar fields (such as obtaining the date of the next week.
1 package www. wsw. otherApi; 2 import java. util. calendar; 3 4 public class CalendarDemo {5 6 public static void main (String [] args) {7 Calendar Ar c = Calendar ar. getInstance (); 8 9 // use the method in the Calendar class to obtain the number of days 10 showDays (February) of January 1, 2016 ); // February 29, 2016 Monday 11} 12 13 public static void showDays (int year) {14 Calendar Ar c = Calendar ar. getInstance (); 15 c. set (year, 2, 1); // set the date to March 1 16 c. add (Calendar. DAY_OF_MONTH,-1); // offset the date from the previous day to 17 showDate (c); 18} 19 20 // obtain the calendar field, get the date that meets Chinese habits. Display 21 public static void showDate (Calendar c) {22 int year = c. get (Calendar. YEAR); 23 int month = c. get (Calendar. MONTH) + 1; // 0 indicates January 24 int day = c. get (Calendar. DAY_OF_MONTH); 25 int week = c. get (Calendar. DAY_OF_WEEK); // 1 represents Sunday, and 7 represents Saturday 26 27 System. out. println (year + "year" + month + "month" + day + "day" + getWeek (week )); // October 28} 29 30 public static String getWeek (int week) {31 String [] weeks = {"", "Sunday", "Monday", "Tuesday ", "Wednesday", "Thursday", "Friday", "Saturday"}; 32 return weeks [week]; 33} 34 35}