04-常用API

CommonAPI

1、Math类

1.1、作用

​ Math表示数学类,包含执行基本数字运算的方法

1.2、常用方法

1.2.1、绝对值

  • public static int abs(int a)

1.2.2、向上取整

  • public static double ceil(double a)

1.2.3、向下取整

  • public static double floor(double a)

1.2.4、求幂

  • public static double pow(double a, double b) // 返回a的b次幂

1.2.5、四舍五入

  • public static int round(float a)

1.2.6、示例代码

  • public class Demo01 {
        public static void main(String[] args) {
            System.out.println(Math.abs(5)); // 5
            System.out.println(Math.abs(-5)); // 5
            System.out.println(Math.ceil(5.1)); // 6.0
            System.out.println(Math.floor(5.1)); // 5.0
            System.out.println(Math.floor(5.9)); // 5.0
            System.out.println(Math.pow(2, 3)); // 8 2*2*2
            System.out.println(Math.round(5.4)); // 5
            System.out.println(Math.round(5.6)); // 6
        }
    }

2、System类

2.1、作用

​ System表示系统类。有和系统相关的方法

2.2、常用方法

2.2.1、终止当前运行的java虚拟机

  • public static void exit(int status) //终止当前运行的 Java 虚拟机。

2.2.2、返回当前时间(ms)

  • public static long currentTimeMillis()//返回当前时间(以毫秒为单位)

2.2.3、拷贝数组

  • public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) //拷贝数组
    @param
    src:源数组
    srcPos:被复制的源数组的开始索引
    dest:目标数组,即要赋值的数组
    destPos:目标数组开始被赋值的索引
    length:复制的长度

2.2.4、示例代码

public class Demo02 {
    public static void main(String[] args) {
        System.out.println("a");
        // System.exit(0);
        System.out.println("b");
        // 从1970年1月1日 0时0分0秒到现在的毫秒值        System.out.println(System.currentTimeMillis()); // 1612141563497        // 可以统计代码的耗时        long start = System.currentTimeMillis();
        long sum = 0;
        for (int i = 0; i < 10000; i++) {
            sum += i;
        }
        System.out.println("sum = " + sum);
        long end = System.currentTimeMillis();
        System.out.println("消耗时间: " + (end - start)); // 334毫秒
        // public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
        // 数组复制
        // Object src: 被复制的数组
        // int srcPos: 被复制的数组的开始索引
        // Object dest: 复制后的数组
        // int destPos: 复制后的数组哪个位置开始放数据
        // int length: 复制几个数据
        int[] a = {11, 22, 33, 44, 55};
        int[] b = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // 目标{0, 0, 0, 0, 22, 33, 44, 0, 0, 0}
        System.arraycopy(a, 1, b, 4, 3);
        System.out.println(b); // 显示数组的地址
        System.out.println(Arrays.toString(b)); // 将数组的内容变成字符串打印
    }
}

3、BigDecimal类

3.1、double存在的问题

  • 在Java中double是双精度浮点型,也就是有精度的小数,而不是绝对准确的。

  • eg:

  • public class Demo07 { public static void main(String[] args) {       
    // 小数在计算机中是近似值(浮点型)
    double d = 0.1 + 0.2//0.3
    System.out.println("d = " + d);
    //0.300000000000000
        }
    }

3.2、作用

​ BigDecimal类可以表示任意精度的数字

3.3、BigDecimal类的构造器

3.3.1、根据double数据创建BigDecimal

  • BigDecimal(double val)//根据double数据创建BigDecimal

3.3.2、根据String数据创建BigDecimal

  • BigDecimal(String val)//根据String数据创建BigDecimal

3.4、BigDecimal类中的常用方法

3.4.1、加法

  • public BigDecimal add(另一个BigDecimal对象)//加法

3.4.2、减法

  • public BigDecimal subtract (另一个BigDecimal对象)//减法

3.4.3、乘法

  • public BigDecimal multiply (另一个BigDecimal对象)//乘法

3.4.4、除法

  • public BigDecimal divide (另一个BigDecimal对象)//除法

3.4.5、除法(保留小数位数)

4、包装类

4.1、为什么要有包装类

​ 基本数据类型有8种,虽然基本数据类型效率高,但是功能极其有限,只能做加减乘除运算,为了对基本数据类型进行更多的操作,Java为每种基本数据类型提供了对应的类----包装类

4.2、基本数据类型对应的包装类

  • byte----->Byte

  • short----->Short

  • int----->Integer

  • long----->Long

  • float----->Float

  • double----->Double

  • char----->Character

  • boolean----->Boolean

  • 包装类的规律:首字母大写,特殊的两个int -> Integer, char ->Character

4.3、int和String相互转换

4.3.1、int转成String

  • 5 - > "5"
    • String s1 = 5 + "";
    • String s2 = String.valueOf(5);

4.3.2、String转成int

  • "5" - > 5
    • int i = Integer.parseInt("5");

4.4、int和Integer相互转换

4.4.1、int转换成Integer

  • 把基本数据类型转成包装类,装箱操作
    • Integer ig1 = Integer.valueOf(5); // 手动装箱
    • Integer ig2 = 6; // 自动装箱

4.4.2、Integer转换成int

  • 将包装类转换成基本数据类型,拆箱操作
    • int i = ig1.intValue(); // 手动拆箱
    • int i2 = ig2; // 自动拆箱

4.5、示例代码

public class Demo042 {
    public static void main(String[] args) {
        // int -> Integer: 装箱 (把苹果放到箱子里面)
        // static Integer valueOf(int i)
        Integer ig1 = Integer.valueOf(5); // 手动装箱
        // Integer -> int: 拆箱 (把箱子拆开拿到里面的苹果)
        // int intValue()
        int i = ig1.intValue(); // 手动拆箱
        // 自动装箱: 将基本数据类型转成包装类
        Integer ig2 = 6; // 自动装箱的本质: Integer ig2 = Integer.valueOf(6);
        // 自动拆箱: 将包装类转成基本数据类型
        int i2 = ig2; // int i2 = ig2.intValue();
        // 面试题:自动装箱和自动拆箱的原理是什么?
        // 自动装箱: 调用Integer.valueOf(6);
        // 自动拆箱: 调用ig2.intValue();
             System.out.println("--------------------------------------");
        Integer ig3 = 8;
        ig3 += 2; // 底层是什么样的代码
        // ig3 = ig3 + 2;
        // ig3 = ig3.intValue() + 2; // int
        // ig3 = Integer.valueOf(ig3.intValue() + 2); // Integer
    }
}

5、 日期相关类

5.1、Date类

5.1.1、作用

  • Date类代表了一个特定的时间(2021年2月1日 11时59分 38秒),精确到毫秒
  • 参照:1970年1月1日 0时0分0秒
  • 1秒 = 1000毫秒

5.1.2、Date类构造器

  • Date()创建Date对象,时间为执行这行代码的时间(即当前时间)
  • Date(long date)创建Date对象,时间是在1970年1月1日 0时0分0秒 基础上增加参数指定的毫秒值

5.1.3、Date类普通方法

  • long getTime() //得到当前对象的时间 和 1970年1月1日 0时0分0秒 相差的毫秒值
  • void setTime(long time) //修改时间, 时间是在 1970年1月1日 0时0分0秒 基础上增加参数指定的毫秒值

5.2、DateFormat类

5.2.1、为什么要DateFormat类

  • Date主要表示1970年到某个时间的毫秒值,如果输出给用户看,用户是看不太懂的: "Thu Oct 25 21:41:33 CST 2018", 用户实际上比较喜欢"2018年10月25日 21时41分33秒"

5.2.2、DateFormat类的作用

5.2.2.1、格式化(日期文本)
  • 格式化即将Date对象转成文字
    • 将当前时间的Date对象 转成 "2018年9月15日 19时18分19秒"
5.2.2.2、解析(文本日期)
  • 将文字转换成Date对象
    • 将字符串 "2007-12-26 10:13:31" 转成 Date对象

5.2.3、示例代码

public class Demo06 {
    public static void main(String[] args) throws ParseException {
        // 需求:将当前时间的Date对象 转成 "2018年9月15日 19时18分19秒" (格式化)
        // 1.创建Date对象
        Date now = new Date(); // 现在时间
        // 2.创建SimpleDateFormat对象
        // 2.1指定时间格式: 确定目标格式, 不会变化的不动,会变化的使用特殊字符代替
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        // 3.调用格式化方法
        String format = sdf.format(now);
        System.out.println("格式化后的时间: " + format);
        // 6.需求:将字符串 "2007-12-26 10:13:31" 转成 Date对象 (解析)
        // 1.创建时间字符串
        String timeStr = "2007-12-26 10:13:31";
        // 2.创建SimpleDateFormat对象
        // 2.1指定时间格式: 确定目标格式, 不会变化的不动,会变化的使用特殊字符代替
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 3.调用解析方法
        Date parse = sdf2.parse(timeStr); // parse有红色波浪线,是我们后面要学的异常相关知识点,我们现在这样, 1.在红色波浪线上alt + 回车 2.选择第一项Add exception to method signature
        System.out.println("解析后: " + parse);
        System.out.println("解析后毫秒值:" + parse.getTime()); // 一般数据库中存储毫秒值
    }
}

5.3、Calendar类

5.3.1、Date类的缺点

  • Date主要是表示1970的毫秒值,Date类对单独获取年,月,日,时,分,秒,昨天,明天,上个星期,加上或减去一些时间不好处理

5.3.2、Calendar的作用

  • 方便调整时间

5.3.3、如何创建Calendar对象

  • Calendar rightNow = Calendar.getInstance();

5.3.4、Calendar常用方法

  • Calendar类常用方法

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

5.4、日期类练习1(秒杀案例)

  • public class Demo08 {
        public static void main(String[] args) throws ParseException {
            // "2021年11月11日 00:00:00"; -> Date -> getTime -> 毫秒值
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            // 把秒杀开始时间转成毫秒值
            long startTime = sdf.parse("2021年11月11日 00:00:00").getTime();
            // 把秒杀结束时间转成毫秒值
            long endTime = sdf.parse("2021年11月11日 00:10:00").getTime();
            // 把小贾下单时间转成毫秒值
            long xiaoJiaTime = sdf.parse("2021年11月11日 0:03:47").getTime();
            // 如果小贾下单时间毫秒值在开始和结束秒杀时间内,说明秒杀成功
            if (xiaoJiaTime >= startTime && xiaoJiaTime <= endTime) {
                System.out.println("小贾秒杀成功");
            } else {
                System.out.println("小贾秒杀失败");
            }
            // 把小皮下单时间转成毫秒值
            long xiaoPiTime = sdf.parse("2021年11月11日 0:10:11").getTime();
            // 如果小皮下单时间毫秒值在开始和结束秒杀时间内,说明秒杀成功
            if (xiaoPiTime >= startTime && xiaoPiTime <= endTime) {
                System.out.println("小皮秒杀成功");
            } else {
                System.out.println("小皮秒杀失败");
            }
        }
    }

5.5、日期类练习2(计算活了多少天)

public class Demo09 {
    public static void main(String[] args) throws ParseException {
        // 1.得到现在时间
        // 2.把现在时间转成毫秒值
        long nowTime = new Date().getTime();
        // 3.把小贾出生日期转成毫秒值
        // "2000年11月11日" -> 解析 -> Date -> 毫秒值
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Date birthday = sdf.parse("1985年09月23日");
        long birthdayTime = birthday.getTime();
        // 4.小贾活着的毫秒值 = 现在时间毫秒值 - 小贾出生毫秒值
        long liveTime = nowTime - birthdayTime;
        // 5.把小贾活着的毫秒值 转成天
        // 毫秒 秒 分钟 小时 天
        long liveDay = liveTime / 1000 / 60 / 60 / 24;
        System.out.println("活了: " + liveDay + "天");
    }
}
posted @   OnlyOnYourself-Lzw  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示