day_16

目录

1.  System

1.1  概述

1.2  常用方法

1.2.1  获取当前时间对应的毫秒数(1970.1.1  8点到现在的毫秒数)

1.2.2  建议垃圾回收

1.2.3  JVM关机

1.2.4  打印

2.  Date

2.1  概述

2.2  构造方法

2.2.1 显示当前系统时间

2.2.2  时间原点到指定毫秒数的时间(时间原点1970.1.1  8:00:00)

2.2.3  获取当前系统时间的前十分钟的时间

2.3  常用方法

2.4  格式化

2.5  Calendar(日历)

3.  Random(随机数)

3.1  概述

3.2  使用

4.  BigInteger和BigDecimal

4.1  概述

4.2  常用方法

4.3  使用方法

4.4  阶乘

5.  Math

5.1  概述

5.2  常用方法

5.3  使用方法

5.3.1  绝对值

5.3.2  次方

5.3.3  向下取整

5.3.4  向上取整

5.3.5  四舍五入

5.3.6  随机数  生成一个大于等于0且小于1的整数

5.3.7  四舍六入  五留双(如果x.5后,没有其他小数,则x为奇数就进一, x为偶数不进1)


1.  System

1.1  概述

System类提供的public static long currentTimeMillis()用来返回当前时 间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。

此方法适于计算时间差。

lSystem类代表系统,系统级的很多属性和控制方法都放置在该类的内部。 该类位于java.lang包。

l由于该类的构造器是private的,所以无法创建该类的对象,也就是无法实 例化该类。其内部的成员变量和成员方法都是static的,所以也可以很方便 的进行调用。

l成员变量

ØSystem类内部包含in、out和err三个成员变量,分别代表标准输入流

(键盘输入),标准输出流(显示器)和标准错误输出流(显示器)。

l成员方法

Ønative long currentTimeMillis():

该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时

间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数。

Øvoid exit(int status):

该方法的作用是退出程序。其中status的值为0代表正常退出,非零代表

异常退出。使用该方法可以在图形界面编程中实现程序的退出功能等。

1.2  常用方法

1.2.1  获取当前时间对应的毫秒数(1970.1.1  8点到现在的毫秒数)

long startTime = System.currentTimeMillis();

1.2.2  建议垃圾回收

System.gc();

1.2.3  JVM关机

System.exit(0);

1.2.4  打印

System.out.println(222);

public class System_01 {
	public static void main(String[] args) {
		// 获取当前时间对应的毫秒数(1970.1.1 8点到现在的毫秒数)
		long startTime = System.currentTimeMillis();
		System.out.println(startTime);
		// 建议垃圾回收
		System.gc();
		// JVM关机
		System.exit(0);
		// 不会执行
		System.out.println(222);
	}
}

2.  Date

2.1  概述

获取时间和时间操作

2.2  构造方法

2.2.1 显示当前系统时间

先创建当前系统时间对象

Date date = new Date();

之后打印  System.out.println(date);

2.2.2  时间原点到指定毫秒数的时间(时间原点1970.1.1  8:00:00)

//要先创建对象之后打印,  应该输出8:00:01

date = new Date(1000);

2.2.3  获取当前系统时间的前十分钟的时间

//System.currentTimeMillis()为时间原点到现在所过去的毫秒数  减去10分钟的毫秒数

//既为当前时间前10分钟的时间

date = new Date(System.currentTimeMillis() - (1000*60*10));

public class Date_01 {
	public static void main(String[] args) {
		// 创建当前系统时间对象
		Date date = new Date();
		// 时间原点到指定毫秒数的时间 (1970.1.1 8:00:00)
		date = new Date(1000);
		System.out.println(date);

		// 获取当前系统时间的前十分钟 的时间
		date = new Date(System.currentTimeMillis() - (1000 * 60 * 10));
		System.out.println(date);
	}
}

2.3  常用方法

使用一次就销毁 , 系统不推荐使用

//起始年月日到现在的过去过多少年, 多少月, 多少日

Systme.out.println(date.getYear());

//0是一月,11是12月, 所以月份要+1才是当前月份

Systme.out.println(date.getMonth());

//周日是第一天

Systme.out.println(date.getDate());

public class Date_02 {
	public static void main(String[] args) {
		Date date = new Date();
		// 123 , 起始年份 1900年 , 到现在 123年
		System.out.println(date.getYear());
		// 9  , 月份0开始, 0表示1月
		System.out.println(date.getMonth());
		// 日
		System.out.println(date.getDate());
	}
}

2.4  格式化

将系统默认的时间格式改为我们设置的格式

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");

将时间格式的字符串, 转换为Date对象,  需要注意, 格式必须和sdf传入的格式一致

public class Date_03 {
	public static void main(String[] args) throws ParseException {
		Date date = new Date();
		// Fri Oct 13 09:39:49 CST 2023
		// 而我们习惯 2023-10-13 10:20:56
		// 所以我们需要对时间日期进行格式化
		System.out.println(date);
		// 格式化对象
		// y 年 , M 月 d 日 H 时 m 分 s 秒 S 毫秒
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
		// 把 Date对象 按照指定格式进行转换,并返回的是转换之后的字符串
		String strDate = sdf.format(date);
		System.out.println(strDate);
		String string = "2023-10-13 09:43:21 908";
		// 把时间格式的字符串,转换为Date对象,需要注意,格式必须和sdf传入的格式一致
		date = sdf.parse(string);
		System.out.println(date);
	}
}

2.5  Calendar(日历)

3.  Random(随机数)

3.1  概述

随机数 从 0 开始

3.2  使用

//创建随机数

Random random = new Random();

//在int有效范围内随机生成一个数

int result = random.nextlnt();

//传入10, 是在0~9内生成, 不包含10

result = random.nextlnt(10);

//生成一个大于等于0且小于1的值

System.out.println(random.nextDouble());

public class Random_01 {
	public static void main(String[] args) {
		// 1 创建一个随机数生成器
		Random random = new Random();
		// 在int有效范围内随机生成一个数
		int result = random.nextInt();
		System.out.println(result);
		// 传入10,是在0~9之内 生成,不包含10
		result = random.nextInt(10);
		System.out.println(result);
		// 生成一个 大于等于0 且 小于1
		System.out.println(random.nextDouble());
	}
}

//生成1~10

result = random.nextlnt(10)+1;

//生成10~20

result = random.nextlnt(11)+10;

//生成任何区间

nextlnt(最大值 - 最小值 + 1) + 最小值;

例如 : 生成 a~z的值

int c = random.nextInt('z' - 'a' +1) + 'a';

char c1 = (char)c;

System.out.printIn(c1);

public class Random_02 {
	public static void main(String[] args) {
		Random random = new Random();
		// 生成1~10
		int result = random.nextInt(10) + 1;
		System.out.println(result);
		// 生成10~20
		// nextInt(最大-最小+1) + 最小
		result = random.nextInt(11) + 10;
		// 生成 1500 ~ 3200
		result = random.nextInt(1701) + 1500;
		// 生成a~z
		int c = random.nextInt('z' - 'a' + 1) + 'a';
		char c1 = (char) c;
		System.out.println(c1);
	}
}

//随机生成数组里的数据

String[] arr = {"a","啊","想","不","无","b"};

Random random = new Random ();

// i < 4 就是随机四个数据

  for(int i = 0; i < 4; i++){

   int index = random.nextInt(arr.lenght);

   System.out.printIn(arr[index]);

}

代码: 

public class Random_03 {
	public static void main(String[] args) {
		String[] arr = { "啊", "a", "1", "c", "D", "的", "想", "8" };
		Random random = new Random();

		for (int i = 0; i < 4; i++) {
			int index = random.nextInt(arr.length);
			System.out.println(arr[index]);
		}
	}
}

4.  BigInteger和BigDecimal

4.1  概述

1、Integer类作为int的包装类,能存储的最大整型值为231-1,Long类也是有限的, 最大为263-1。如果要表示再大的整数,不管是基本数据类型还是他们的包装类 都无能为力,更不用说进行运算了。

2、java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供

所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。 另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、 位操作以及一些其他操作。

4.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。整数 相除只保留整数部分。

BigInteger remainder(BigInteger val) :返回其值为 (this % val) 的 BigInteger。

BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟(this % val) 的两个 BigInteger 的数组。

BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger。

4.3  使用方法

处理大数据

//可以是字符串,字节数组等,但是不能直接传入整数

//表示更大的整数

BigInteger bigInteger = new BigInteger("123");

//可以传入整数

//表示更大的浮点数

BigDecimal bigDecimal = new BigDecimal(20);

BigDecimal result = null;

//相加(add)

//相减(subtract)

//相乘(multiply)

//相除(divide)

//取余(remainder)

import java.math.BigDecimal;
import java.math.BigInteger;

public class BigInteger_01 {
	public static void main(String[] args) {
		// 可以是字符串,字节数组等,但是不能直接传入整数
		// 表示更大的整数
		BigInteger bigInteger = new BigInteger("123");
		// 可以传入整数
		// 表示更大的浮点数
		BigDecimal bigDecimal = new BigDecimal(20);
		BigDecimal result = null;
		// 相加
		result = bigDecimal.add(new BigDecimal(30));
		// 相减
		result = bigDecimal.subtract(new BigDecimal(10));
		// 相乘
		result = bigDecimal.multiply(new BigDecimal(100));
		// 相除
		result  = bigDecimal.divide(new BigDecimal(10));
		// 取余
		result = bigDecimal.remainder(new BigDecimal(3));
		System.out.println(result);
	}
}

4.4  阶乘

import java.math.BigDecimal;

public class BigInteger_02 {
	public static void main(String[] args) {
		System.out.println(test1(28));
		System.out.println(test2(1028));
	}
    //普通方法算阶乘,数据有限算不了太大的数
	public static long test1(int n) {
		long result = 1;
		for (int i = 1; i <= n; i++) {
			result *= i;
		}
		return result;
	}
    //BigDecimal方法算阶乘, 可以算巨大的数据
	public static BigDecimal test2(int n) {
		BigDecimal result = new BigDecimal(1);
		for (int i = 1; i <= n; i++) {
			result  = result.multiply(new BigDecimal(i));
		}
		return result;
	}
}

5.  Math

5.1  概述

java.lang.Math提供了一系列静态方法用于科学计算。其方法的参数和返回

值类型一般为double型。

5.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) 角度—>弧度

5.3  使用方法

5.3.1  绝对值

System.out.printIn(Math.abs(-19.5));

5.3.2  次方

//10的3次方

System.out.printIn(Math.pow(10, 3));

5.3.3  向下取整

System.out.printIn(Math.floor(12.9));

5.3.4  向上取整

System.out.printIn(Math.ceil(12.1));

5.3.5  四舍五入

//返回的是整数, 只看小数点后一位(当小数位数特别多的时候可能出现bug)

//负数 . 5不进位

System.out.printIn(Math.round(2.5));

System.out.printIn(Math.round(-2.5));

5.3.6  随机数  生成一个大于等于0且小于1的整数

System.out.printIn(Math.random());

5.3.7  四舍六入  五留双(如果x.5后,没有其他小数,则x为奇数就进一, x为偶数不进1)

System.out.printIn(Math.rint(2.5));

public class Math_01 {
	public static void main(String[] args) {
		// 绝对值
		System.out.println(Math.abs(-19.5));
		// 10的3次方
		System.out.println(Math.pow(10, 3));
		// 向下取整
		System.out.println(Math.floor(12.9));
		// 向上取整
		System.out.println(Math.ceil(12.1));
		// 四舍五入
		System.out.println(Math.round(2.499999999999999));
		// 负数.5不进位
		System.out.println(Math.round(-3.5));
		// 四舍六入五留双 , 如果x.5后 没有其他小数,则x为奇数就进1 , 为偶数,就不进1 
		System.out.println(Math.rint(3.5)+"====");
		// 随机数 生成一个大于等于0 且小于1的整数
		System.out.println(Math.random());
		// 0.30000000000000004
		// 小数永远不要判断相等
		System.out.println(0.1 + 0.2);
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值