Dark Horse programmer _ day21 _ other APIs

Source: Internet
Author: User

----------- Android training, java training, and java learning technology blog. We look forward to communicating with you! ------------
I. System class:

It cannot be instantiated. No constructor.

The methods in this class are static.

Common Methods:

Exit (); currentTimeMillis () to get the current millisecond value; gc (); getProperties ();


Long time = System. currentTimeMillis ();

System. out. println ("hello:" + time );

SystemMethodDemo2 ();

}

Demonstrate the getProperties Method

Is used to obtain the system property set.

System attribute information: some inherent information about the jvm and the operating system that the program obtains at runtime. After the information is obtained, data is provided for subsequent programs.

Public static void systemMethodDemo (){

// Obtain the system property information and store it in the Properties set.

Properties prop = System. getProperties ();

// You want to obtain information in the set.

You can use stringPropertyNames () in this set to convert it into a set first. The specific type has been specified for this method.

Set <String> nameSet = prop. stringPropertyNames ();


For (String name: nameSet ){

String value = prop. getProperty (name );

System. out. println (name + ":" + value );

}

}


Obtains the specified attribute information in the system.

Public static void systemMethodDemo2 (){

Properties prop = System. getProperties ();


String value = prop. getProperty ("OS. name ");

System. out. println (value );

}

}


Small applications of the System class.

System. getProperty ("line. separator"): gets line breaks from different System platforms.

Public class SystemTest {

Private static final String LINE_SPARATOR = System. getProperty ("line. separator ");

Public static void main (String [] args ){

System. out. println ("hello" + LINE_SPARATOR + "world ");

}

}


2. Runtime: indicates the object when the application is running. This object does not need to be created and can be obtained directly through getRuntime, because this Runtime class can be used to combine applications and Runtime environments.

This class has no constructor. However, we found that this class actually has a unique non-static method.

It does not allow other programs to create such objects. It will certainly provide us with a method to obtain its objects.

And this method must be static. The return value type must be Runtime.

This method is getRuntime.

Note: This class is completed in the singleton design mode.

Public class RuntimeDemo {

Public static void main (String [] args) throws Exception {

Runtime r = Runtime. getRuntime ();

Process p = r.exe c ("notepad.exe c :\\ OtherApi.txt ");

Thread. sleep (4000 );

P. destroy ();

}


}


Iii. Math class

Math: all methods are static.

A value greater than the minimum integer of the parameter.

Double d1 = Math. ceil (12.34); // 13

System. out. println ("d1 =" + d1 );

The value is smaller than the maximum integer of the parameter.

Double d2 = Math. floor (12.34); // 12

System. out. println ("d2 =" + d2 );

Rounding.

Double d3 = Math. round (12.54 );

System. out. println ("d3 =" + d3 );

Power 3 of 10

Double d4 = Math. pow (10, 3 );

Two methods for generating random numbers

1. Using the random () of the math class ():

For (int x = 0; x <10; x ++ ){

// Double d = Math. ceil (Math. random () * 10 );

// Double d = Math. floor (Math. random () * 10 + 1 );

Int d = (int) (Math. random () * 10 + 1 );

System. out. println (d );

}

2. By using the Random number object: nextInt () method of the Random class

Random r = new Random ();

For (int x = 0; x <10; x ++ ){

Int num = r. nextInt (10) + 1;

System. out. println ("num =" + num );

}

}


4. Date class.

Date: Date class.

// Obtain the object of the current date and time

Just use new Date.

Date d = new Date ();

System. out. println (d );

// Convert the date object to the millisecond value.

To perform basic operations between date objects, you must convert them to specific millisecond values.


Converts a date object to a millisecond value. Use getTime () in the Date object ();

Date d = new Date ();

Long time = d. getTime ();

System. out. println (time );

Converts a millisecond value to a date object.

// Use the Date constructor.

// Use the set8Time method of the Date class to set a specified millisecond value.


Date d2 = new Date (1343807461796l); or:

Date d3 = new Date ();

D3.setTime (1343807461796l );


}

}

V. DateFormat

1. Obtain the DateFormat object. You can use the static method getDateInstance () of this class by referring to the api description. This method returns the default format object of the date.

Date d = new Date ();

// Default style. MEDIUM

DateFormat dateFormat = DateFormat. getDateInstance ();

// Use the specified style.

DateFormat = DateFormat. getDateInstance (DateFormat. LONG );

// Use the specified style, including the date and time.

DateFormat = DateFormat. getDateTimeInstance (DateFormat. LONG, DateFormat. LONG );

System. out. println (dateFormat );

2. You want to change the date or time to a custom style. For example: xxxx # mm #2012/8/1

DateFormat = new SimpleDateFormat ("yyyy/MM/dd HH: mm: ss ");

String str_date = dateFormat. format (d );

System. out. println (str_date );

}

}


6. convert a string into a date object

The date string is converted into a date object.

To obtain the date or time fields.

The parsing uses the parse method in the DateFormat class.

Public static void dateFormatDemo2 () throws ParseException {

// Date string.

String str_date = "2011/5/16"; note: this date format is not one of the four default formats: LONG, SHORT, MEDIUM, and FULL. If they are, they will be available.

DateFormat dateFormat = DateFormat. getDateInstance (); to get the date format object.


// Define a date format object. To parse a date string in a specific format, the date format object must be able to parse the format


DateFormat = new SimpleDateFormat ("yyyy/MM/dd ");

Date date = dateFormat. parse (str_date );

System. out. println (date );

}

VII. DateFormat exercise

How many days are there between "" and?

Ideas:

String --> date object --> millisecond value.


String str_date1 = "2012/4/12 ";

String str_date2 = "2012/6/3 ";


// 1. convert a string in date format into a date object.

// Use the parse method in the Dateformat object. The custom format is also encapsulated into a DateFormat object.

DateFormat dateFormat = new SimpleDateFormat ("yyyy/MM/dd ");


// 2. parse the date string.

Date date_1 = dateFormat. parse (str_date1 );

Date date_2 = dateFormat. parse (str_date2 );


// 3. Convert the date object to a millisecond value.

Long time_1 = date_1.getTime ();

Long time_2 = date_2.getTime ();


// 4, subtraction.

Long time = Math. abs (time_1-time_2 );

System. out. println (time/1000/60/60/24); converted from milliseconds to days

}

}


8. Calendar class

Before JDK1.1, APIs of some methods in the Date class were not easy to internationalize. After JDK1.1, use the Calendar class to convert the date and time fields.

Use the DateFormat class to format and parse the date string. The corresponding method in Date has been discarded.

The Calendar class is an abstract class. Provides a class method getInstance () to obtain a common object of this type.

Public static void show (Calendar c ){


Int year = c. get (Calendar. YEAR );

Int month = c. get (Calendar. MONTH) + 1; 0 indicates January 1, January.

Therefore, when the data is restored to a numeric value on January 1, January, we need to add one.

Int day = c. get (Calendar. DAY_OF_MONTH );

String week = getWeek (c. get (Calendar. DAY_OF_WEEK ));

Define the getWeek method using the Lookup Table Method

System. out. println (year + "year" + month + "month" + day + "day" + week );

}


Public static String getWeek (int week ){

If (week> 7 | week <1)

Throw new NoWeekException ("this week is not available ");

String [] weeks = {"", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday "}; americans regard Sunday as the first day of a week.

Return weeks [week];

}


Calendar c = Calendar. getInstance ();

// Set the specified date for this calendar object.

// C. set (2011, 10, 4 );


// The calendar can be offset on the specified date.

C. add (Calendar. DAY_OF_MONTH,-1 );


// Obtain the number of days for any year in February.

Public static Calendar getDays (int year ){


Calendar c = Calendar. getInstance ();

C. set (year,); it is the year.


C. add (Calendar. DAY_OF_MONTH,-1); reduce the number of days by one day, which is the last day of January 1, February of a year.

Return c;

Show (c );

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.