Java's Utility class

Source: Internet
Author: User
Tags add time dateformat stringbuffer

1) Random class

Used to generate random numbers, all generated numbers are equal probabilities.

Nextint (): The resulting value is between all ranges of values of int (-231 ~ 231-1)

Nextint (int value): The value generated from 0 to value (contains 0, does not contain value).

2) Math class

Used for general mathematical operations.

Random (): Generates a random decimal between 0-1 (including 0, excluding 1).

ABS (Double): Take absolute value.

Pow (double, double): Calculates the n-th square of M

sqrt (double): Calculates the root of M.

Floor (Double): Gets the largest integer value smaller than M.

Ceil (double): Gets the smallest integer value larger than M.

Round (): Rounds rounding.

3) String class

IndexOf (): Gets the index position of one string in another string (whether a string contains another string).

LastIndexOf (): Gets the last index position of a string in another string.

SUBSTRING (): Takes a substring from the specified position of a string.

Concat (): concatenation of two strings into a single string.

Equals (): Compares the contents of two strings (note the difference from = =).

toLowerCase (): Converts a string to lowercase characters.

toUpperCase (): Converts a string to uppercase characters.

Equalsignorecase (): Compares whether two strings are the same (case insensitive).

4) StringBuilder and StringBuffer class

You need to use StringBuilder or stringbuffer when you need to do frequent concatenation of strings.

5) Date class and SimpleDateFormat class

The date class is used to manipulate dates, and SimpleDateFormat is used to format dates.

Note: Many of the methods available in the date class are obsolete methods and are recommended using the methods provided by the Calendar class.

When you get a month, either by date or by calender, it is between 0-11.

Get System Current Time
Date date = new Date ();
Format Time YYYY-MM-DD HH:mm:ss
SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String Text = Dateformat.format (date);
Knows a date string, if it is converted to a time object

String time = "2016-10-05 12:10:05";

Date date2 = Dateformat.parse (time);

6) Calendar class

Create a Calendar class object
Calender cal = Calendar.getinstance ();
Get year
Int year = Cal.get (calendar.year);
Get month
Int month = Cal.get (calendar.month);
Calendar.date, Calendar.hour, Calendar.minite, Calendar.second
Add time period (year, month, day, hour, minute, second)
Cal.add (calender.year, 1);//plus 1 years
Cal.add (Calender.hour,-2);//minus 2 hours
Calcender Conversion to date
Date date = Cal.gettime ();

Java's Utility class

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.