day13

day13

一、Math类

Math 类包含用于执行基本数学运算的方法

1.1 常见方法
abs			绝对值
ceil		向上取整
floor		向下取整 (Java对于数据的默认处理方式)
max			求最大值
min			求最小值
random		随机数
pow			求某数的次方幂
round		四舍五入
1.2 常见常量
PI		
E
public class Demo {

    public static void main(String[] args) {

//        System.out.println(Math.abs(12.35));        //12.35
//        System.out.println(Math.abs(-12.35));       //12.35

//        System.out.println(Math.ceil(12.35));       //13.0
//        System.out.println(Math.ceil(12.88));       //13.0
//        System.out.println(Math.ceil(-12.35));      //-12

//        System.out.println(Math.floor(12.35));      //12.0
//        System.out.println(Math.floor(12.88));      //12.0
//        System.out.println(Math.floor(-12.35));     //-13.0

//        System.out.println(Math.max(3, 5));                 //5
//        System.out.println(Math.max(Math.max(3, 5), 8));    //8

//        System.out.println(Math.min(3, 5));         //3

//        System.out.println(Math.random());          //0.7107862281074286

//        System.out.println(Math.pow(2, 3));         //8.0
//        System.out.println(Math.pow(3, 3));         //27.0

//        System.out.println(Math.round(12.35));      //12
//        System.out.println(Math.round(12.88));      //13

        System.out.println(Math.PI);        //3.141592653589793
        System.out.println(Math.E);         //2.718281828459045
    }
}

二、System类

System 类包含一些有用的类字段和方法

2.1 常见字段
System.in		标准输入流。  关联到键盘   比如:键盘录入 Scanner sc = new Scanner(System.in);
System.out		标准输出流。	关联到控制台 比如:System.out.println();
System.err		标准错误流。  关联到控制台,只不过打印的内容是红色的。 比如:System.err.println();
2.2 常见方法
static void exit(int status)  		终止当前正在运行的 Java 虚拟机。参数0是正常终止,非0是非正常终止
static long currentTimeMillis()  	返回以毫秒为单位的当前时间
    	注意:
    		1. 1=1000毫秒
    		2. 这个时间是从19701100:00:00开始计算的
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 
        参数:
        	src - 源数组
            srcPos - 源数组中的起始位置
            dest - 目标数组
            destPos - 目标数据中的起始位置
            length - 要复制的数组元素的数量
public class Demo2 {

    public static void main(String[] args) {

        System.out.println("A...");
        System.exit(0);
//        Runtime.getRuntime().exit(0);
        System.out.println("B...");
    }
}

/*
	执行结果:
	A...
 */
public class Demo3 {

    public static void main(String[] args) {

//        long l = System.currentTimeMillis();
//        System.out.println(l);      //1619061917874

        long start = System.currentTimeMillis();
        for(int i = 0; i < 10000; i++){

            System.out.println(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("共耗时:" + (end - start));

    }
}
public class Demo4 {

    public static void main(String[] args) {

        //static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
        int[] arr = {11, 22, 33, 44, 55};
        int[] arr_dest = new int[10];

        //复制之前
        bianLi(arr_dest);   //[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        System.arraycopy(arr, 0, arr_dest, 0, arr.length);
        //复制之后
        bianLi(arr_dest);   //[11, 22, 33, 44, 55, 0, 0, 0, 0, 0]

    }

    /*
        遍历数组的方法
     */
    public static void bianLi(int[] arr){

        StringBuilder sb = new StringBuilder();
        sb.append("[");

        for (int i = 0; i < arr.length; i++) {

            if(i == arr.length-1){
                sb.append(arr[i]).append("]");
            }else{
                sb.append(arr[i]).append(", ");
            }
        }

        System.out.println(sb);
    }
}

三、BigInteger类

不可变的任意精度的整数。可以表示比long还大的数。

3.1 构造方法
BigInteger(String val)       
注意:因为已经超过了最大的整数范围,所以只能以字符串的形式当做参数来传递了
3.2 常见方法
BigInteger add(BigInteger val)         加
BigInteger subtract(BigInteger val)    减
BigInteger multiply(BigInteger val)    乘
BigInteger divide(BigInteger val)
public class Demo2 {

    public static void main(String[] args) {

        BigInteger bi = new BigInteger("10000000000000000000");
        BigInteger bi2 = new BigInteger("20000000000000000000");

        //BigInteger add(BigInteger val)         加
        System.out.println(bi.add(bi2));        //30000000000000000000

        //BigInteger subtract(BigInteger val)    减
        System.out.println(bi.subtract(bi2));   //-10000000000000000000

        //BigInteger multiply(BigInteger val)    乘
        System.out.println(bi.multiply(bi2));   //200000000000000000000000000000000000000

        //BigInteger divide(BigInteger val)  	 除
        System.out.println(bi.divide(bi2));     //0

        //BigInteger mod(BigInteger m)          求余
        System.out.println(bi.mod(bi2));        //10000000000000000000

    }
}

四、BigDecimal类

不可变的、任意精度的有符号十进制数。

用之前的float或者double进行运算的话,有可能会丢失精度。但是使用BigDecimal的话,不会丢失精度。

4.1 构造方法
BigDecimal(double val) 		  【不推荐使用】
BigDecimal(String val) 		  【建议使用】
4.2 常见方法
BigDecimal add(BigDecimal augend)  				加
BigDecimal subtract(BigDecimal subtrahend)      减
BigDecimal multiply(BigDecimal multiplicand)  	乘
BigDecimal divide(BigDecimal divisor)  			除
    
如果是结果是无限不循环小数,那么会报一个算术异常,必须要使用divide的重载方法才可以解决
BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
   	参数:
    	1. divisor  被除的数
    	2. scale  要保留多少位小数
     	3. roundingMode  舍入模式
    			HALF_UP 		四舍五入
    			ROUND_CEILING 	向上取整
				ROUND_FLOOR 	向下取整

五、Date类

Date 表示特定的瞬间,精确到毫秒。

Date()				分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)
Date(long date) 	分配 Date 对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数
5.1 常见方法
void setTime(long time) 
          设置此 Date 对象,以表示 19701100:00:00 GMT 以后 time 毫秒的时间点
long getTime() 
          返回自 19701100:00:00 GMT 以来此 Date 对象表示的毫秒数 
boolean after(Date when) 
          测试此日期是否在指定日期之后。 
boolean before(Date when) 
		  测试此日期是否在指定日期之前 

六、DateFormat类

DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。

因为DateFormat类是抽象类,所以创建对象的时候只能使用它的子类SimpleDateFormat

6.1 格式化和解析
格式化: 将Date类型转为String类型
解析:	  将String类型转为Date类型
6.2 SimpleDateFormat类
构造方法
SimpleDateFormat(String pattern) 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JUMO5UuX-1620344705926)(img/image-20210422151713005.png)]

常见方法
String format(Date date)
Date parse(String source)  

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3nnfeLXt-1620344705928)(img/image-20210422163508884.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WFglCKEO-1620344705930)(img/image-20210422163543073.png)]

练习:
	键盘录入您的出生日期(格式为:"2000-12-20"),然后通过程序计算您已经在世界上活了多少天。
public class Test {

    public static void main(String[] args) throws ParseException {

        Scanner sc = new Scanner(System.in);
        System.out.println("请录入您的出生日期(格式为:2000-12-20):");
        String birthday = sc.nextLine();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = sdf.parse(birthday);
        //获取出生日期的毫秒值
        long time = d.getTime();

        //获取当前时间的毫秒值
        Date dd = new Date();
        long today = dd.getTime();

        //拿当前时间的毫秒值减去出生日期的毫秒值(得到存活的毫秒值)
        long life = today - time;

        //将毫秒值转换为天
        int day = (int) (life / 1000 / 60 / 60 / 24);
        System.out.println(day);

    }
}

七、Calender类

Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEARMONTHDAY_OF_MONTHHOUR日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法

7.1 获取对象的方式
方式一: 通过其子类GregorianCalendar创建对象
方式二: 通过Calender的静态方法getInstance获取对象

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1ChDhRai-1620344705932)(img/image-20210422161742241.png)]

7.2 常见方法
int get(int field) 					返回给定日历字段的值 
void set(int field, int value)  	将给定的日历字段设置为给定值
abstract  void add(int field, int amount) 	根据日历的规则,为给定的日历字段添加或减去指定的时间量 

boolean after(Object when)  
boolean before(Object when)
void setTime(Date date)  
Date getTime()  
常见的日历字段:
YEAR 			年
MONTH 			月
DAY_OF_MONTH 	一月中的第几天
HOUR			时
MINUTE 			分
SECOND 			秒		
public class Demo {

    public static void main(String[] args) {

        //1. 获取Calendar类的对象
        Calendar calendar = Calendar.getInstance();
//        System.out.println(calendar);

        //2. 常见的方法
        //int get(int field)
        System.out.println(calendar.get(Calendar.YEAR));        //2021
        System.out.println(calendar.get(1));                    //2021

        /*
            因为月份是从0开始的,所以要想得到真正的月份儿必须要加1
         */
        System.out.println(calendar.get(Calendar.MONTH) + 1);   //4
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//22

        System.out.println(calendar.get(Calendar.HOUR));        //4
        System.out.println(calendar.get(Calendar.MINUTE));      //26
        System.out.println(calendar.get(Calendar.SECOND));      //15
        System.out.println("---------------------------");

        //void set(int field, int value)  	将给定的日历字段设置为给定值
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(Calendar.YEAR, 1992);
        System.out.println(calendar2.get(Calendar.YEAR));       //1992
        System.out.println("---------------------------");

        //abstract  void add(int field, int amount) 	根据日历的规则,为给定的日历字段添加或减去指定的时间量
//        calendar2.add(Calendar.YEAR, 2);
//        System.out.println(calendar2.get(Calendar.YEAR));       //1994

        calendar2.add(Calendar.YEAR, -2);
        System.out.println(calendar2.get(Calendar.YEAR));       //1990

        //boolean after(Object when)
        System.out.println(calendar.after(calendar2));      //true

        //boolean before(Object when)
        System.out.println(calendar.before(calendar2));     //false

        //void setTime(Date date)
        Date d = new Date(3000);
        Calendar calendar3 = Calendar.getInstance();
        calendar3.setTime(d);

        //Date getTime()
        System.out.println(calendar3.getTime());        //Thu Jan 01 08:00:03 CST 1970

    }
}

八、Arrays工具类

此类包含用来操作数组(比如排序和搜索)的各种方法

static int binarySearch(int[] a, int key) 
static void sort(int[] a)   
static String toString(int[] a) 
static int[] copyOf(int[] original, int newLength)   
public class Demo {

    public static void main(String[] args) {

        int[] arr = {11, 22, 55, 44, 33};
        /*
            使用binarySearch查找之前,一定要先排序
         */
//        System.out.println(Arrays.toString(arr));   //[11, 22, 55, 44, 33]
//        Arrays.sort(arr);
//        System.out.println(Arrays.toString(arr));   //[11, 22, 33, 44, 55]
//        System.out.println(Arrays.binarySearch(arr, 33));       //2

        //static int[] copyOf(int[] original, int newLength)
        int[] arr_xin = Arrays.copyOf(arr, 10);

        System.out.println(Arrays.toString(arr_xin));    //[11, 22, 55, 44, 33, 0, 0, 0, 0, 0]
        
    }
}

33]
// Arrays.sort(arr);
// System.out.println(Arrays.toString(arr)); //[11, 22, 33, 44, 55]
// System.out.println(Arrays.binarySearch(arr, 33)); //2

    //static int[] copyOf(int[] original, int newLength)
    int[] arr_xin = Arrays.copyOf(arr, 10);

    System.out.println(Arrays.toString(arr_xin));    //[11, 22, 55, 44, 33, 0, 0, 0, 0, 0]
    
}

}












































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值