Java基础(八) -- Math/Arrays/System/大数据/日期类

Math

Arrays

  • toString

    • 将数组转为字符串
    • 示例
      1
      2
      int[] arr = {1,2,3};
      System.out.println(Arrays.toString(arr));
  • sort

    • 排序数组
    • 示例
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
          public static void main(String[] args) {
      Integer[] arr = {1,2,3,4,5};
      Arrays.sort(arr, new Comparator() {
      @Override
      public int compare(Object o1, Object o2) {
      Integer i1 = (Integer) o1;
      Integer i2 = (Integer) o2;
      return i2 - i1;
      }
      });
      System.out.println(Arrays.toString(arr));
      }
      ```
      <!-- more -->
      * binarySearch
      + 二叉查找
      + 示例
      ```java
      Integer[] arr = {1,2,3,4,5};
      // 数组必须是有序的 小于0表示没找到
      System.out.println(Arrays.binarySearch(arr,4));
      ```

      * copyOf/fill/equals/asList
      + 复制/填充/比较/转为list
      + 示例
      ```java
      public static void main(String[] args) {
      Integer[] arr = {1,2,3,4,5};
      // 拷贝 length 个元素到arr2中
      Integer[] arr2 = Arrays.copyOf(arr,arr.length);
      System.out.println(Arrays.toString(arr2));

      Integer[] nums = {1,2,3};
      // 使用10替换原数组所有值
      Arrays.fill(nums,10);
      System.out.println(Arrays.toString(nums));

      Integer[] arr1 = {1,2,3,5,4};
      // 比较两个数组是否一致,完全一致包括顺序
      System.out.println(Arrays.equals(arr,arr1));

      // 将数组转为list
      List<Integer> list = Arrays.asList(arr);
      System.out.println(list);
      }

System

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void main(String[] args) {
// 获取当前毫秒时间戳
System.out.println(System.currentTimeMillis());

Integer[] arr = {1,2,3,4};
Integer[] arr2 = new Integer[3];
// 复制数组
System.arraycopy(arr,0,arr2,1,2);
System.out.println(Arrays.toString(arr2));

// 主动触发垃圾回收
System.gc();

// 退出
System.exit(0);
}

大数处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static void main(String[] args) {
BigInteger bigInteger = new BigInteger("32234234121231232234234212");
System.out.println(bigInteger);
// 加
bigInteger = bigInteger.add(new BigInteger("20"));
System.out.println(bigInteger);
// 减
bigInteger = bigInteger.subtract(new BigInteger("20"));
System.out.println(bigInteger);
// 乘
bigInteger = bigInteger.multiply(new BigInteger("20"));
System.out.println(bigInteger);
// 除
bigInteger = bigInteger.divide(new BigInteger("20"));
System.out.println(bigInteger);

// 大精度小数
BigDecimal bigDecimal = new BigDecimal("1231023.12312312312");
// 不指定精度操作时可能会出现无限循环所以抛出异常
// RoundingMode.CEILING 保留和分子相同的精度
bigDecimal = bigDecimal.divide(new BigDecimal("11.22"), RoundingMode.CEILING);
System.out.println(bigDecimal);
}

日期类

  • java.util.Date

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    package com.gphper.date;

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class DateClass {
    public static void main(String[] args) throws ParseException {
    Date date = new Date();
    System.out.println(date);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
    System.out.println(dateFormat.format(date));

    //根据毫秒数计算时间
    Date date1 = new Date(953453453);
    System.out.println(date1);

    // 将字符串转为时间
    String s = "2023年06月01日 10:12:22 星期四";
    Date pdate = dateFormat.parse(s);
    System.out.println(pdate);
    }
    }
  • Calendar

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    import java.util.Calendar;

    public class DateClass {
    public static void main(String[] args) {
    // 获取年月日信息需要单独获取
    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.HOUR));
    System.out.println(instance.get(Calendar.HOUR_OF_DAY));
    }
    }
  • LocalDate / locaTime / LocalDateTime

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
     import java.time.Instant;
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    import java.util.Date;

    public class DateClass {
    public static void main(String[] args) {

    LocalDateTime now = LocalDateTime.now();
    System.out.println(now);
    System.out.println(now.getMinute());
    // 格式化时间格式
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm:ss E");
    System.out.println(now.format(dateTimeFormatter));

    // 获取当前时间戳
    Instant now1 = Instant.now();
    System.out.println(now1);
    Date date = Date.from(now1);
    System.out.println(date);
    // Date 转 Instance
    now1 = date.toInstant();
    // 获取一天后的日期
    LocalDateTime m = now.plusDays(1);
    System.out.println(m.format(dateTimeFormatter));
    // 获取100天前
    LocalDateTime m1 = now.minusDays(100);
    System.out.println(m1.format(dateTimeFormatter));
    }
    }