Math
Arrays
toString
- 将数组转为字符串
- 示例
1
2int[] 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
46public static void main(String[] args) {
Integer[] arr = {1,2,3,4,5};
Arrays.sort(arr, new Comparator() {
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 | public static void main(String[] args) { |
大数处理
1 | public static void main(String[] args) { |
日期类
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
23package 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
14import 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
30import 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));
}
}