第十三章 常用类(Math 类、Arrays 类、System类、Biglnteger 和BigDecimal 类、日期类)

一、Math 类(P481)

Math 类包含,用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

(1)abs:绝对值
(2)pow:求幂
(3)ceil:向上取整【返回 >= 该参数的最小整数】
(4)floor:向下取整【返回 <= 该参数的最大整数】
(5)round:四舍五入
(6)sqrt:求开方
(7)random:求随机数【返回的是 0 <= x < 1 之间的随机小数】
(8)max:求两个数的最大值
(9)min:求两个数的最小值

public class Demo {
   

    public static void main(String[] args) {
   

        // 求幂
        double pow = Math.pow(2, 4); // 2的4次方
        System.out.println(pow); // 16.0

        // ceil 向上取整,返回 >= 该参数的最小整数
        double ceil1 = Math.ceil(-3.2);
        double ceil2 = Math.ceil(3.2);
        System.out.println(ceil1); // -3.0
        System.out.println(ceil2); // 4.0

        // sqrt:求开方
        double sqrt = Math.sqrt(9.0);
        System.out.println(sqrt); // 3.0

        // random:求随机数【返回的是 0 <= x < 1 之间的随机小数】
        double random1 = Math.random();
        // 请写出获取a-b之间的一个随机整数a,b均为整数?2 <= x <= 7
        double random2 = 2 + Math.random() * 6;
    }
}

二、Arrays 类(P482)

Arrays 里面包含了一系列静态方法,用于管理或操作数组(比如排序和搜索)。

(1)tostring:返回数组的字符串形式
(2)sort排序:(自然排序和定制排序)
(3)binarySearch:通过二分搜索法进行查找,要求必须排好序的数组
(4)copyOf:数组元素的复制
(5)fill:数组元素的填充
(6)equals:比较两个数组元素内容是否完全一致
(7)asList:将一组值,转换成ist

public class Demo {
   

    public static void main(String[] args) {
   

        // tostring:返回数组的字符串形式
        Integer[] arr1 = {
   1, 20, 30};
        System.out.println(Arrays.toString(arr1)); // [1, 20, 30]

        // sort排序:(自然排序和定制排序)
        // 自然排序
        Integer[] arr2 = {
   1, -1, 7, 50};
        Arrays.sort(arr2);
        System.out.println(Arrays.toString(arr2)); // [-1, 1, 7, 50]

        // 定制排序
        Integer[] arr3 = {
   1, -1, 7, 50};
        // o1 - o2 :升序
        // o2 - o1 :降序
        Arrays.sort(arr3, new Comparator<Integer>() {
   
            @Override
            public int compare(Integer o1, Integer o2) {
   
                return o2 - o1;
            }
        });
        System.out.println(Arrays.toString(arr3)); // [50, 7, 1, -1]

        // binarySearch:通过二分搜索法进行查找,要求必须排好序的数组
        Integer[] arr4 = {
   -1, 1, 7, 50};
        int index1 = Arrays.binarySearch(arr4, 1);
        System.out.println(index1); // index1 = 1
        // 如果数组中不存在该元素,就返回 -(low +1)
        // low 为,如果存在的索引位置
        int index2 = Arrays.binarySearch(arr4, 5); // low:2
        System.out.println(index2); // index1 = -3

        // copyOf:数组元素的复制4
        Integer[] arr5 = {
   -1, 1, 7, 50};
        int len1 = arr5.length - 1;
        Integer[] newArr1 = Arrays.copyOf(arr5, len1); // [-1, 1, 7]
        System.out.println(Arrays.toString(newArr1));
        // 如果拷贝长度 > 原数组长度,后面添加 null
        int len2 = arr5.length + 1;
        Integer[] newArr2 = Arrays.copyOf(arr5, len2); // [-1, 1, 7, 50, null]
        System.out.println(Arrays.toString(newArr2));
        // 如果拷贝长度 < 0,抛出异常
        int len3 = -1;
        Integer[] newArr3 = Arrays.copyOf(arr5, len3); // [-1, 1, 7, 50, null]
        System.out.println(Arrays.toString(newArr3));

        // fill:数组元素的填充
        Integer[] arr6 = {
   -1, 1, 7, 50};
        // 用 99 替换原数组所有元素
        Arrays.fill(arr6,99);
        System.out.println(Arrays.toString(arr6)); // [99, 99, 99, 99]

        // equals:比较两个数组元素内容是否完全一致
        Integer[] arr7 = {
   -1, 1, 7, 50};
        Integer[] arr8 = {
   -1, 1, 7, 50};
        System.out.println(Arrays.equals(arr7,arr8)); // true

        // asList:将一组值,转换成ist
        Integer[] arr9 = {
   -1, 1, 7, 50};
        List<Integer> aslist = Arrays.asList(arr9);
        /*
            aslist 运行类型 class java.util.Arrays$ArrayList
            是 Arrays类的 静态内部类
            private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable
         */
        System.out.println(aslist.getClass());
    }
}

三、System类(P486)

(1)exit:退出当前程序
(2)arraycopy:复制数组元素,比较适合底层调用。一般使用 Arrays.copyOf() 完成复制数组
(3)currentTimeMillens:返回当前时间距离1970-1-1的毫秒数
(4)gc:运行垃圾回收机制 System.gc();

public class Demo {
   

    public static void main(String[] args) {
   
        Integer[] arr = {
   -1, 1, 7, 50};
        Integer[] destArr = new Integer[4]; // {0,0,0,0};
 
        /*
            五个参数:
            参数一:src【源数组】
            参数二:srcPos【源数组开始拷贝的索引位置】
            参数三:dest【目标数组】
            参数四:destPos【目标数组开始拷贝的索引位置】
            参数五:length【源数组拷贝的数据长度】
         */
        System.arraycopy(arr, 0, destArr, 0, arr.length);
    }
}

四、Biglnteger 和 BigDecimal 类(P487)

(1)Biglnteger 适合保存比较大的整型
(2)BigDecimal 适合保存精度更高的浮点型(小数)

public class Demo {
   

    public static void main(String[] args) {
   
        BigInteger bigInteger = new BigInteger("10000");
        BigDecimal bigDecimal = new BigDecimal("20.88");
    }
}

1. Biglnteger 和 BigDecimal 常见方法

(1)add加
(2)subtract减
(3)multiply乘
(4)divide除【divide 可以指定精度:BigDecimal.ROUND_CEILING等等】

五、日期类(P488)

1. 第一代日期类 Date

(1)Date:精确到毫秒,代表特定的瞬间
(2)SimpleDateFormat:格式和解析日期的类
在这里插入图片描述

public class Demo {
   

    public static void main(String[] args) throws ParseException {
   
        Date date = new Date(); // 获取当前系统时间
        System.out.println(date); // Mon Jul 25 20:42:17 CST 2022

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
        String format = sdf.format(date);
        System.out.println(format); // 2022年07月25日 08:42:17 星期一

        Date parse = sdf.parse(format);
        System.out.println(parse); // Mon Jul 25 08:42:17 CST 2022
    }
}

2. 第二代日期类 Calendar (日历)

public abstract class Calendar implements Serializable, Cloneable, Comparable {

(1)Calendar类是一个抽象类,并且构造器是 protected。只能通过 getInstance() 来获取实例
(2)它为特定瞬间与一组诸如YEAR、MONTH、DAY_OF_MONTH、HOUR等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法

public class Demo {
   

    public static void main(String[] args) throws ParseException {
   
        Calendar instance = Calendar.getInstance();
        // 获取日历对象的某个日历字段
        System.out.println("年:"+instance.get(Calendar.YEAR));
        System.out.println("月:"+(instance.get(Calendar.MONTH)+1));
        System.out.println("日:"+instance.get(Calendar.DAY_OF_MONTH));
        System.out.println("小时(12):"+instance.get(Calendar.HOUR));
        System.out.println("小时(24):"+instance.get(Calendar.HOUR_OF_DAY));
        System.out.println("分钟:"+instance.get(Calendar.MINUTE));
        System.out.println("秒:"+instance.get(Calendar.SECOND));
    }
}

3. 第三代日期类

3.1 前面两代日期类的不足分析

JDK1.0中包含了一个 java.util.Date 类,但是它的大多数方法已经在JDK1.1引入 Calendar 类之后被弃用了。
而 Calendar 也存在问题是:
(1)可变性:像日期和时间这样的类应该是不可变的
(2)偏移性:Date中的年份是从1900开始的,而月份都从0开始
(3)格式化:格式化只对Date有用,Calendar则不行
(4)此外,它们也不是线程安全的;不能处理闰秒等(每隔2天,多出1s)。

3.2 第三代日期类常见方法

LocalDate(日期/年月日)、LocalTime(时间/时分秒)、LocalDateTime(日期时间/年月日时分秒)JDK8加入

LocalDate只包含日期,可以获取日期字段
LocalTime只包含时间,可以获取时间字段
LocalDateTime包含日期+时间,可以获取日期和时间字段

public class Demo {
   

    public static void main(String[] args) throws ParseException {
   
        LocalDateTime now = LocalDateTime.now();
        LocalDate.now();
        LocalTime.now();
        System.out.println(now); // 2022-07-26T00:04:00.395

        System.out.println(now.getYear()); // 2022
        System.out.println(now.getMonth()); // JULY
        System.out.println(now.getMonthValue()); // 7
        System.out.println(now.getDayOfMonth()); // 26
        System.out.println(now.getHour()); // 0
        System.out.println(now.getMinute()); // 4
        System.out.println(now.getSecond()); // 0
    }
}

3.3 DateTimeFormatter格式日期类

类似于 SimpleDateFormat

public class Demo {
   

    public static void main(String[] args) throws ParseException {
   
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now); // 2022-07-26T00:38:30.801

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dtf.format(now);
        System.out.println(format); // 2022-07-26 00:38:30
    }
}

3.4 Instant时间戳

类似于 Date

public class Demo {
   

    public static void main(String[] args) throws ParseException {
   
        Instant now = Instant.now();
        System.out.println(now); // 2022-07-25T16:43:09.732Z

        Date date = Date.from(now);
        Instant instant = date.toInstant();
    }
}

3.5 第三代日期类更多方法

提供plus和minus方法可以对当前时间进行加或者减

public class Demo {
   

    public static void main(String[] args) throws ParseException {
   
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now); // 2022-07-26T00:50:49.265

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(dtf.format(now)); // 2022-07-26 00:50:49

        // 890 天后
        LocalDateTime ldt1 = now.plusDays(890);
        System.out.println(dtf.format(ldt1)); // 2025-01-01 00:50:49

        // 180 分钟前
        LocalDateTime ldt2 = now.minusMinutes(180);
        System.out.println(dtf.format(ldt2)); // 2022-07-25 21:50:49
    }
}

相关推荐

  1. BigDecimal

    2023-12-25 08:00:02       18 阅读
  2. Math

    2023-12-25 08:00:02       12 阅读
  3. <span style='color:red;'>Arrays</span><span style='color:red;'>类</span>

    Arrays

    2023-12-25 08:00:02      11 阅读
  4. <span style='color:red;'>Arrays</span><span style='color:red;'>类</span>

    Arrays

    2023-12-25 08:00:02      11 阅读
  5. ——包装

    2023-12-25 08:00:02       15 阅读
  6. C++PrimerPlus:继承:抽象基

    2023-12-25 08:00:02       5 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-25 08:00:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-25 08:00:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-25 08:00:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-25 08:00:02       18 阅读

热门阅读

  1. 力扣56.合并区间

    2023-12-25 08:00:02       32 阅读
  2. ARM AArch64的TrustZone架构详解(上)

    2023-12-25 08:00:02       35 阅读
  3. 阿里云公有云平台

    2023-12-25 08:00:02       35 阅读
  4. Iceberg: COW模式下的MERGE INTO的执行流程

    2023-12-25 08:00:02       35 阅读
  5. 设计模式之工厂方法模式

    2023-12-25 08:00:02       41 阅读
  6. 手写爬虫框架

    2023-12-25 08:00:02       45 阅读
  7. FFmpeg常见命令行

    2023-12-25 08:00:02       36 阅读
  8. Vue3和Vue2的区别

    2023-12-25 08:00:02       32 阅读
  9. JVM介绍

    JVM介绍

    2023-12-25 08:00:02      25 阅读
  10. Spring中的组合模式

    2023-12-25 08:00:02       35 阅读
  11. 前端八股文(vue篇)

    2023-12-25 08:00:02       33 阅读