一、System
1.概述
System类提供的public static long currentTimeMillis()用来返回当前时 间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。
此方法适于计算时间差。
注意:此方法返回值是 long 型
System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。该类位于java.lang包。
由于该类的构造器是private的,所以无法创建该类的对象,也就是无法实例化该类。其内部的成员变量和成员方法都是static的,所以也可以很方便 的进行调用。
- 成员变量
System类内部包含in、out和err三个成员变量,分别代表标准输入流
(键盘输入),标准输出流(显示器)和标准错误输出流(显示器)。
- 成员方法
native long currentTimeMillis():
该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时
间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数。
void exit(int status):
该方法的作用是退出程序。其中status的值为0代表正常退出,非零代表
异常退出。使用该方法可以在图形界面编程中实现程序的退出功能等。
2.常用方法
// out 标准输出,默认打印在控制台
// in 标准输入,默认接受控制台输入
// err 错误,打印为红色,异步打印
System.out.println("控制台打印");
// 获取当前时间的毫秒数
long startTime = System.currentTimeMillis();
// 事情
long endTime = System.currentTimeMillis();
// 耗时
System.out.println(endTime - startTime);
// 建议垃圾回收
System.gc();
// 关闭JVM,0表示正常退出,一般用于图形化界面
System.exit(0);
二、Date
1.概述
获取时间和时间操作
2.构造方法
注意 : 导入 java.util.Date
// 获取当前系统时间
Date date = new Date();
// 时间原点到指定毫秒数对应的时间,相当于
date = new Date(1000);
System.out.println(date);
3.格式化
// 格式化
// 年 y 月 M 日 d 时 H 分 m 秒 s 毫秒 S 这里的 - 可以换成年、月、日
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
// format:把时间对象按照对应格式进行格式化,返回格式化后的字符串
String strTime = sdf.format(date);
System.out.println(strTime);
strTime = "2024-01-16 11:33:59 666";
// parse:把时间格式的字符串,转换为Date对象
// 注意:字符串格式,必须和SimpleDateFormat指定的格式一致
Date newDate = sdf.parse(strTime);//此处可能会报错,需要放到 try{ //放这里 }catch(){} 中
System.out.println(newDate);
4.Calendar
//获取当前系统日历
Calendar calendar=Calendar.getInstance();
//获取今天是本周的 第几天,周日是第一天
int i =calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(i);
//获取年
System.out.println(calendar.get(Calendar.YEAR));
//获取月
System.out.println(calendar.get(Calendar.MONTH)+1);
//获取日
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
//获取时
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
//获取分
System.out.println(calendar.get(Calendar.MINUTE));
//获取秒
System.out.println(calendar.get(Calendar.SECOND));
三、Random
1.概述
生成从 0 开始的随机数
2.使用
//创建生成器对象
Random random=new Random();
//在int范围内,随机生成一个数
int result=random.nextInt();
//在n-m之间生成,包含n和m
int n=6;
int m=10;
result = random.nextInt(m-n+1)+n;
System.out.println("aa "+result);
//a-z,这里要注意进行强转,否则就会输出数字而不是字母
System.out.println((char)(random.nextInt('z'-'a'+1)+'a'));
四、BigInteger
1.概述
-
Integer类作为int的包装类,能存储的最大整型值为231-1,Long类也是有限的, 最大为263-1。如果要表示再大的整数,不管是基本数据类型还是他们的包装类都无能为力,更不用说进行运算了。
-
java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供
所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。 另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、 位操作以及一些其他操作。
就是更大的数字类型,比long大得多
2.常用方法
public BigInteger abs():返回此 BigInteger 的绝对值的 BigInteger
BigInteger add(BigInteger val) :返回其值为 (this + val) 的 BigInteger
BigInteger subtract(BigInteger val) :返回其值为 (this - val) 的 BigInteger
BigInteger multiply(BigInteger val) :返回其值为 (this * val) 的 BigInteger
BigInteger divide(BigInteger val) :返回其值为 (this / val) 的 BigInteger 整数相除只保留整数部分,与int类似
BigInteger remainder(BigInteger val) :返回其值为 (this % val) 的 BigInteger
BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟(this % val) 的两个 BigInteger 的数组
BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger
3.使用
//两种对象创建
BigInteger bigInteger=new BigInteger("15");
BigDecimal bigDecimal=new BigDecimal(20);
//运算操作
BigDecimal b1=new BigDecimal(10);
BigDecimal b2=new BigDecimal(20);
//值
BigDecimal b3=null;
//加
b3=b1.add(b2);
//减
b3=b1.subtract(b2);
//乘
b3=b1.multiply(b2);
//除
b3=b1.divide(b2);
//余
b3=b1.remainder(b2);
System.out.println(b3);
4.阶乘
n 的阶乘就是从 1 开始乘到 n
//调用方法 输出结果
System.out.println(test(100));
//求n的阶乘的方法
public static BigDecimal test(int n){
BigDecimal result=new BigDecimal(1);
for(int i=1;i<=n;i++){
result=result.multiply(new BigDecimal(i));
}
return result;
}
五、Math
1.概述
java.lang.Math提供了一系列静态方法用于科学计算。其方法的参数和返回值类型一般为double型。
2.常用方法
abs 绝对值 acos,asin,atan,cos,sin,tan 三角函数 sqrt 平方根
pow(double a,doble b) a的b次幂 log 自然对数
exp e为底指数
max(double a,double b)
min(double a,double b)
random() 返回0.0到1.0的随机数
long round(double a) double型数据a转换为long型(四舍五入)
toDegrees(double angrad) 弧度—>角度
toRadians(double angdeg) 角度—>弧度
3.使用方式
//绝对值
System.out.println(Math.abs(-6.6));
//向上取整
System.out.println(Math.ceil(6.6));
//向下取整
System.out.println(Math.floor(6.6));
//最大值
System.out.println(Math.max(6, 7));
//最小值
System.out.println(Math.min(6, 7));
//平方根
System.out.println(Math.sqrt(9));
//立方根
System.out.println(Math.cbrt(8));
//随机数,生成一个大于等于0且小于1的小数
System.out.println(Math.random());
//四舍五入
System.out.println(Math.round(6.5));
//负数 .5不进位
System.out.println(Math.round(-6.5));
//四舍六入五留双 , 就是保留偶数
System.out.println(Math.rint(6.5));
System.out.println(Math.rint(7.5));
//2的3次幂
System.out.println(Math.pow(2, 3));
//生成大于等于n 且小于m
System.out.println(Math.random()*(m-n+1)+n);
//a-z , 注意强转
System.out.println((char)(Math.random()*('z'-'a'+1)+'a'));