Common Classes Based on JAVA

Keywords: Attribute less Java jvm

Common Basic Classes

1. Date class

Date represents a specific instant, accurate to milliseconds. Most methods in the Date class have been replaced by methods in the Calendar class.

Membership methods in the Date class:
1. Judging the front and back of two date objects

Date date1 = new Date();//Get the current system time
Date date2 = new Date(10000);//Get a time point of 10,000 milliseconds from the standard reference time

boolean boo1 = date1.after(date2);//If date1 is after date2, it returns true, otherwise it returns false.
boolean boo2 = date1.before(date2);//If date1 precedes date2, it returns true, otherwise it returns false.

2. Acquisition time

Date date = new Date();
long l = date.getTime();

3. Modification time

Date date = new Date();
date.setTime(1000);

Date date = new Date (System. current Time Millis ();//milliseconds

2.Calendar class

The abstract class cannot create objects when the Calendar class is in use. The object (an Abstract subclass object) can be obtained by the static getInstance method in the Calendar class.

Membership methods commonly used in Calendar classes:

1. Get a Calendar object:

Calendar c = Calendar.getInstance();//The getInstance method is a static method that is called directly by the class name.
System.out.println(c);

2. Get the value of the specified attribute of a calendar object

Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.DATE));//Get the date attribute value of the calendar represented by the c object

3. Modify the specified attribute value of a calendar object:

Calendar c = Calendar.getInstance();	
c.set(Calendar.YEAR, 2017);

3.SimpleDateFormat class

//format date
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String str=sdf.format(date);
System.out.println(str);
//Resolution date
String str2="2019-8-3";
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
Date date1=sdf2.parse(str2);
System.out.println(date1);

4.Math class

Membership methods commonly used in Math classes:
1. Absolute value
Math.abs(a)

2. Maximum and Minimum
Math.max(a,b)
Math.min(a,b)

3. Power operation
The b-th power of Math.pow(a,b)/a

4. Square and cubic roots
Math.sqrt(a)// The result is square root
Math.cbrt(a)//The result is cubic root

5. Rounding
Math.round(a)

6. Random Numbers
Decimal between Math.random()//0-1

7. ceil floor
Math.ceil(3.2)//the smallest positive number greater than or equal to this number
Math.floor(2.5) // The largest positive number less than or equal to this number

// Accurate storage decimal
BigDecimal bigDecimal1=new BigDecimal("1.4");

// Rounding

BigDecimal bigDecimal4=new BigDecimal("3.145");
double v=bigDecimal4.setScale(2, RoundingMode.HALF_UP).doubleValue();
System.out.println(v);

Rounding Method
//System.out.printf("%.2f",3.145);
//String.format("%.2f", 3.4548);
//Math.round();
//BigDecimal
//DecimalFormat

DecimalFormat

DecimalFormat decimalFormat = new DecimalFormat("#.##");
System.out.println(decimalFormat.format(3.1415926));//3.14

5.Random class

Examples of this class are used to generate pseudo-random number streams. This class uses 48 bits of seeds and uses linear congruential form to modify them.

Random random=new Random();
double v=random.nextDouble();
System.out.println(v);

Generating random numbers

Random random = new Random(10);//Using 10 as seed and using linear congruence formula to generate pseudo-random numbers
int i1 = random.nextInt();//Generate a random integer
int i2 = random.nextInt(10);//Generate a random integer of less than 10
double d = random.nextDouble();//Generate a random double value
boolean b = random.nextBoolean();//Generate a random boolean value

6.Sysytem class

Common methods in the System class:
1. Acquiring System Time

long time1 = System.currentTimeMillis();//Get the current time, milliseconds
long time2 = System.nanoTime();//Get the current time, nanoseconds

2. Forced exit from virtual machine

System.exit();

3. Waste Recycling and Disposal Mechanism:

gc(): run the garbage collector.

7.Runtime class

Each Java application has a Runtime class instance that enables the application to connect to its running environment. The current runtime can be obtained through the getRuntime method.

1 exec (command) executes the specified string command in a separate process.

//1 Create Runtime objects
Runtime runtime=Runtime.getRuntime();

//2exec starts a process
Process process=runtime.exec("notepad");

//3 Closing process
//Program dormancy
Thread.sleep(5000);
process.destroy();

2 exit(int status) exit jvm

Runtime runtime=Runtime.getRuntime();
runtime.exit(0);

Posted by roots on Wed, 28 Aug 2019 06:15:34 -0700