一、数字操作类
1.Math类
package 数字操作类;
public class MathDemo {
public static void main ( String[ ] args) {
System. out. println ( Math. round ( 5.49 ) ) ;
System. out. println ( Math. round ( - 5.49 ) ) ;
System. out. println ( Math. round ( 5.51 ) ) ;
System. out. println ( Math. round ( - 5.51 ) ) ;
System. out. println ( Math. round ( 5.5 ) ) ;
System. out. println ( Math. round ( - 5.5 ) ) ;
}
}
2.Random类
Random产生随机数,nextInt(10),表示产生一个0-9的数字 以下代码实现产生5个0-9范围内且不重复的数
package 数字操作类;
import java. util. Random;
public class RandomDemo {
public static void main ( String[ ] args) {
Random random = new Random ( ) ;
int data[ ] = new int [ 5 ] ;
int i = 0 ;
while ( i < 5 ) {
int t = random. nextInt ( 10 ) ;
if ( ! isrepeat ( data, t) )
{ data[ i] = t; i++ ; }
}
java. util. Arrays. sort ( data) ;
for ( int j = 0 ; j < data. length; j++ ) {
System. out. println ( data[ j] ) ;
}
}
public static boolean isrepeat ( int temp[ ] , int index)
{
for ( int i = 0 ; i < temp. length; i++ ) {
if ( temp[ i] == index) { return true ; }
}
return false ;
}
}
3.BigInteger类
BigInteger用于表示整形数据不能表示的非常大数字
package 数字操作类;
import java. math. BigInteger;
public class BigIntegerDemo {
public static void main ( String[ ] args) {
BigInteger bigA = new BigInteger ( "12345678987654321" ) ;
BigInteger bigB = new BigInteger ( "98765432123456789" ) ;
System. out. println ( "和:" + bigB. add ( bigA) ) ;
System. out. println ( "差:" + bigB. subtract ( bigA) ) ;
System. out. println ( "积:" + bigB. multiply ( bigA) ) ;
System. out. println ( "商:" + bigB. divide ( bigA) ) ;
BigInteger result[ ] = bigB. divideAndRemainder ( bigA) ;
System. out. println ( "商:" + result[ 0 ] + " 余数:" + result[ 1 ] ) ;
}
}
4.BigDecimal类
BigDecimal大浮点数,相比于BigInteger,BigDecimal可以保留小数,但都继承了Number抽象类 两个构造:
public BigDecimal(double val) public BigDecimal(String val) 实现准确的四舍五入,利用除法操作 public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) -----divisor:除数,scale:小数点后几位,roundingMode:进位模式,一般四舍五入向上进位
package 数字操作类;
import java. math. BigDecimal;
public class BigDecimalDemo {
public static void main ( String[ ] args) {
System. out. println ( Round ( 10 , 3 , 4 ) ) ;
}
public static double Round ( double num1, double num2, int scale)
{
BigDecimal a = new BigDecimal ( num1) ;
BigDecimal b = new BigDecimal ( num2) ;
return a. divide ( b, scale, BigDecimal. ROUND_HALF_UP) . doubleValue ( ) ;
}
}
二、日期处理类
1.Date类
Date进行与long的转化—>Date的构造函数与getTime()
package 日期处理类;
import java. util. Date;
public class DateDemo {
public static void main ( String[ ] args) {
Date date = new Date ( ) ;
long var = System. currentTimeMillis ( ) ;
System. out. println ( new Date ( var) ) ;
System. out. println ( date) ;
System. out. println ( date. getTime ( ) ) ;
}
}
2.SimpleDateFormat类
SimpleDateFormat进行日期格式化— 作为转化过程的传递对象— 制定了一种转化格式 —进行Date与String类之间的转化 String–>Date:prase(String str) Date–>String:format(Date date) 常用的时间格式:年(yyyy)月(MM)日(dd)时(HH)分(mm)秒(ss)毫秒(SSS)
package 日期处理类;
import java. text. ParseException;
import java. text. SimpleDateFormat;
import java. util. Date;
public class SimpleDateFormatDemo {
public static void main ( String[ ] args) {
System. out. println ( "String--->Date" ) ;
String str = "2020-09-16 14:35:46" ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
try {
Date date = sdf. parse ( str) ;
System. out. println ( date) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
}
System. out. println ( "Date--->String" ) ;
Date date1 = new Date ( ) ;
String str1 = sdf. format ( date1) ;
System. out. println ( str1) ;
}
}
Date与String之间的转换依靠的是SimpleDateFormat String与基本数据类型之间的转换依靠的是包装类与String.valueOf()方法 long与Date之间的转换依靠的是Date类提供的构造方法与getTime()方法
3.Calendar类
package 日期处理类;
import java. util. Calendar;
public class CalendarDemo {
public static void main ( String[ ] args) {
Calendar cal = Calendar. getInstance ( ) ;
StringBuffer buf = new StringBuffer ( ) ;
buf. append ( cal. get ( Calendar. YEAR) ) . append ( "-" ) ;
buf. append ( cal. get ( Calendar. MONTH) + 1 ) . append ( "-" ) ;
buf. append ( cal. get ( Calendar. DAY_OF_MONTH) ) . append ( "" ) ;
System. out. println ( buf) ;
}
}