SE中常见的API

Math类:

取绝对值

int a = Math.abs(-10);//10

向上取整

int a = Math.ceil(3.14);//4

向下取整

int a = Math.floor(3.14);//3

 四舍五入

int a = Math.round(3.5);//4

 最大值

int max = Math.max(10,20);//20

最小值

int min = Math.min(10,20);//10

 a的b次幂:返回的是浮点数

double pow = Math.pow(2.0,3.0);//8

 随机数:默认是0.0-1.0

double random = Math.random();

        /0-100的随机数
        int a = (int)Math.floor(Math.random()*100)+1;
        System.out.println("a = " + a);

        //50-100随机数,min包含,max包含   公式:Math.floor(Math.tandom()*(Max-min))+min
        int b = (int)Math.floor(Math.random()*(51))+50;
        System.out.println("b = " + b);

System类

结束当前虚拟机运行:非0表示异常终止

System.exit(0);

返回当前时间毫秒值,基准时间是:1970-1-1 00:00:00, 

System.currentTimeMills();

复制数组

int[] arr1 = {1,2,3,4,5,6};
int[] arr2 = {5,6,7,8,9,10};
//从arr1数组的第一个索引开始复制到arr2的第三个索引,复制四个
System.arraycopy(arr1,1,arr2,3,4);

  Arrays方法:

int[] arr = {1,2,3,4,5,6,7,2,4};

//漂亮打印数组,返回字符串形式
Arrays.toString(arr);
//对数组排序
Arrays.sort(arr);
//对数组的内容查找,返回的是一个索引类型,如果返回负数就是没找到
Arrays.binarySearch(arr,4);

Objects类

public static void main(String[] args) {
        Dog dog = new Dog();

        String string1 = Objects.toString(dog);
        System.out.println("string1 = " + string1);

        //调用对象toString方法,如果对象为null,就返回我们传入的字符串
        String string2 = Objects.toString(dog,"对象为空");
        System.out.println("string2 = " + string2);

        //判断对象是否为空
        boolean aNull = Objects.isNull(dog);
        System.out.println("aNull = " + aNull);
        //判断对象是否不为空
        boolean b = Objects.nonNull(dog);
        System.out.println("b = " + b);


    }

BigDecimal类

public static void main(String[] args) {
        double a = 398649.9;
        double b = 0.2;

        //创建对象,要保证传入的字符串类型
        BigDecimal bigDecimal = new BigDecimal(a + "");
        BigDecimal bigDecimal1 = new BigDecimal(b + "");


        //加法
        BigDecimal add = bigDecimal.add(bigDecimal1);
        double v = add.doubleValue();
        System.out.println("v = " + v);

        //减法
        double v1 = bigDecimal.subtract(bigDecimal1).doubleValue();
        System.out.println("v1 = " + v1);

        //乘法
        double v2 = bigDecimal.multiply(bigDecimal1).doubleValue();
        System.out.println("v2 = " + v2);

        //除法,后面横岗是向下取整,还有向上取整和四舍五入,
        double v3 = bigDecimal.divide(bigDecimal1,2,BigDecimal.ROUND_UP).doubleValue();
        System.out.println("v3 = " + v3);

    }

基本数据类型包装类

//基本数据类型和包装类的转换
public class Test01 {
    public static void main(String[] args) {
        //int -> integer 自动装箱
        int a = 10;
        Integer integer1 = a;//第一种
        Integer integer2 = new Integer(a);//第二种
        Integer integer3 = Integer.valueOf(a);//第三种,重要
        System.out.println(integer1);


        //Integer -> int 自动拆箱
        int i  = integer1.intValue();
        System.out.println("i = " + i);

    }
}

Date方法:

public static void main(String[] args) {
        //获取当前时间对象
        Date date = new Date();
        System.out.println("date = " + date);

        //获取基准时间(1970年)后数x毫秒
        Date date1 = new Date(1000L);
        System.out.println("date1 = " + date1);

        //获取改时间对象代表时间与基准时间的毫秒差
        long time = date1.getTime();
        System.out.println("time = " + time);

        //设置改时间对象所代表的时间,基准时间向后数x毫秒后
        date1.setTime(10000L);
        System.out.println("time = " + date1);
    }

格式化年月日:规则:年:y 月:M 日:d 时:H 分:m 秒:s

public static void main(String[] args) throws ParseException {
        //创建格式化对象,需要传入格式化规则(字符串形式)
        //规则:年:y  月:M  日:d  时:H  分:m  秒:s
        //2023-03-28 08:00:00  yyyy-MM-dd HH:mm:ss
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();

        //date->String
        String format = simpleDateFormat.format(date);
        System.out.println("format = " + format);

        //String -> date
        String str = "2002-02-02 09:08:09";
        Date parse = simpleDateFormat.parse(str);
        System.out.println("parse = " + parse);
    }

Calendar方法:

public static void main(String[] args) {

        //获取日历类
        Calendar instance = Calendar.getInstance();
        System.out.println("instance = " + instance);
        //calendar -> date
        Date time = instance.getTime();


        //date->calendar
        instance.setTime(time);
    }
public static void main(String[] args) {
        //获取当前时间的日历对象
        Calendar instance = Calendar.getInstance();
        System.out.println("instance = " + instance);

        //获取年月日
        int i = instance.get(Calendar.YEAR);//获取年,月日以此类推,程序中的月是从0开始的,周日是每个星期的第一天
        System.out.println("i = " + i);
        int i1 = instance.get(Calendar.MONTH);//现在十三月,打印的是二月,因为程序的月份是从0开始的
        System.out.println("i1 = " + i1);
        int i2 = instance.get(Calendar.DAY_OF_MONTH);
        System.out.println("i2 = " + i2);

        //设置年月日
        instance.set(2018,2,3,5,3,4);
        instance.set(Calendar.YEAR,2022);//月日以此类推,上一行是简单写法

        //修改年月日
        instance.add(Calendar.YEAR,-2);//往前推两年
        instance.add(Calendar.YEAR,2);//往后推两年

    }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值