Common Java classes

Source: Internet
Author: User

Package java. Lang .*

The lang Package provides the most basic class package required by Java for development and design. The java. util. * util package provides the event model, Collection framework, datetime tools, and various useful classes.

 

1, string

A string is a string consisting of a group of characters.

1> statement

String S = "ABC ";

String S = new string ("ABC ");

2> common methods

Trim () removes spaces at both ends of the string;

Replace () replacement;

Indexof (), lastindexof () lookup. If any, an index is returned. If no,-1 is returned;

Startswith (), endswith () to determine whether there is an suffix;

Contains () determines whether it contains;

Tochararray () is converted to a character array;

Getchars (), getbytes () is converted to an array and saved to the specified array;

Format () format the string;

3> Best Practices

Once it is necessary to change the original content, each string method returns a New String object. Therefore, we recommend using stringbuffer and stringbuilder;

String. valueof (), convert other types to string;

2. Math class

ABS () returns the absolute value.

Ceil () returns the smallest integer greater than or equal to the parameter

Floor () returns the maximum integer smaller than or equal to the value of the parameter.

Max () returns the greater limit of the two parameters.

Min () returns a smaller value of the two parameters.

Random () returns a random number of the double type between 0.0 and 1.0, including 0.0 but not 1.0.

Round () returns the rounded integer of the parameter.

Sin (), cos (), Tan () sine, cosine, tangent

SQRT () returns the square root of the Parameter

Pow () returns the second power of the first parameter

 

3. processing time and date

Java. util. date, which encapsulates a long data, indicates the number of milliseconds that are separated from the time of GMT 00:00:00, January 1, January 1, 1970.

Java. Text. dateformat: format the date

Java. util. calendar, which allows you to flexibly set or read the year, month, day, minute, and second information of a date.

Java. util. gregoriancalendar,

For example:

Date = new date ();

Simpledateformat F = new simpledateformat ("yyyy-Mmmm-dd-eeee ");

System. Out. println (F. Format (date ));

Another example is:

Gregoriancalendar GC = new gregoriancalendar ();

String now = GC. Get (calendar. Year) + "year"

+ GC. Get (calendar. Month + 1) + "month"

+ GC. Get (calendar. Date) + "day"

+ GC. Get (calendar. Hour) + "hour"

+ GC. Get (calendar. Minute) + "Minute"

+ GC. Get (calendar. Second) + "seconds"

 

4. format the output.

1> Use formatter

Formatter = new formatter (system. Out );
Formatter. format ("% 4 $2 S % 3 $2 S % 2 $2 S % 1 $2 s", "A", "B", "C ", "D ")
//-> "D c B"

% [Argument_index $] [flags] [width] [. Precision] Conversion

Specific settings:

Http://doc.java.sun.com/DocWeb/api/java.util.Formatter

Haowen:

Http://wenku.baidu.com/view/c018ca1efc4ffe473368ab70.html? From = rec & Pos = 0 & Weight = 7 & lastweight = 2 & COUNT = 4

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.