J2se basics: 8. Common System Class 2

Source: Internet
Author: User
1: transformation between basic data and encapsulation types


A: Basic Data Type ---> encapsulation type (object type)
Boolean boolean_1 = new Boolean (true );
Byte ----> byte
Short ----> short
Char ----> character
Int ---> integer
Long --> long
Float --> float
Double --> double
B: encapsulation type ---> basic type
Integer. intvalue ---> int
Folat. floatvalue ---> float
Double. doublevalue ---> double


2: conversion between basic types and strings


Call the string. valueof () method.
String STR = string. valueof (any data type );


3: Convert string to basic data type


Call the parse method of the encapsulation class.
Int I = integer. parseint ("100 ");
Double D = double. parsedouble ("12.334 ");




4: some common characeter methods:


Character mainly works with the string object to perform some simple verification on the string object.
String. chatat (I );

Simple data verification

Java. util. RegEx package

Pattern: compile a regular expression.

Matcher: checks whether the string matches the regular expression.


5: The String object has the length method. The array has the Length attribute.


Math object

1: generate random number

A: use math object: Math. Random (); to obtain a random number between 0 and 1. This random number is always less than 0 and less than 1 decimal number.

B: Use a random object.


2: decimal operation
Math. Round (): the decimal point is not retained, and the numeric value is rounded down.
12.65 ---> 13
Math. Ceil (): retain the decimal place to obtain the ceiling closest to the number.
12.65 --> 13.0
Math. Floor (): the floor with the nearest digit is retained.
12.65 --> 12.0






Date object

1: how to get the system time:
A: system. currenttimemillis:
It indicates the number of milliseconds that have elapsed since January 1, lunar January 1970 yen.
B: Java. util. Date ();
Subclass: Java. SQL. Date: The year, month, and day when the Java program processes the date fields in the database.

Java. SQL. Time: the time, minute, and second used by Java programs to process the date fields in the database.

Java. SQL. timestamp: The year, month, hour, minute, and second used by Java programs to process date fields in the database.

C: Calendar class:
Calendar




Mutual conversion between the three objects.


Between long and date.

Long ---> date
Long L = 20171110888578l;
Date = new date (L );


Date ---> long
Date = new date ();
Long time_long = date. gettime ();


Conversion between long and calendar
Long ---> calendar
Long long_time = 121110864656l;
Calendar calendar = calendar. getinstance ();
Calendar. settimeinmillis (long_time );
Calendar ---> long
Calendar calendar = calendar. getinstance ();
Long time_long = calendar. gettimeinmillis ();

Conversion between date and calendar
Date ---> calendar
Calendar calendar = calendar. getinstance ();
Calendar. settime (new date ());

Calendar ---> date
Calendar calendar = calendar. getinstance ();
Date = calendar. gettime ();


Formatting time


Format ---> dateformat ---> simpledateformat ();


Convert date to string

Call simpledateformat. Format


String to date


Call simpledateformat. parse.


Precise computing object
Biginteger: exact integer calculation

Bigdecimal: exact decimal calculation.


Truncation and rounding.


Normal format
Numberformat = numberformat. getinstance ();


Percent format
Numberformat = numberformat. getpercentinstance ();


// Set the Maximum/minimum reserved integer bits.
Numberformat. setmaximumintegerdigits (4 );
Numberformat. setminimumintegerdigits ();

// Set the Maximum/minimum number of digits after the decimal point
Numberformat. setmaximumfractiondigits (2 );
Numberformat. setmaximumfractiondigits ();
Loan currency format
Numberformat = numberformat. getcurrencyinstance ();




Decimalformat
Number ---> string (rounding function)
Decimalformat = new decimalformat (".##");
String result = decimalformat. Format (d );


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.