Java 常用工具类
字符串
String
不可变字符序列
注意事项
- 字符串不可变性导致频繁操作时性能低下
- 推荐场景:常量字符串、键值处理
常用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| String str = "Hello";
int len = str.length();
String sub = str.substring(1,3);
String[] arr = "a,b,c".split(",");
String newStr = str.replace('l', 'w');
String result = str.concat(" World");
|
StringBuffer
线程安全的可变字符序列
注意事项
- 线程安全但性能较低
- 推荐场景:多线程环境下的字符串操作
常用方法
1 2 3 4 5 6 7 8 9 10 11 12 13
| StringBuffer sb = new StringBuffer();
sb.append("Hello"); sb.append(123);
sb.insert(5, " World");
sb.reverse();
sb.toString();
|
StringBuilder
非线程安全的可变字符序列
常用方法
1 2 3 4
| StringBuilder sb = new StringBuilder(); sb.append("Java"); sb.delete(1,3); sb.replace(0,2, "Py");
|
对比
特性 |
String |
StringBuffer |
StringBuilder |
可变性 |
❌ |
✔️ |
✔️ |
线程安全 |
❌ |
✔️ |
❌ |
性能 |
低 |
中 |
高 |
1
日期时间
Date
(已过时,建议使用 java.time
包)
基本使用
1 2
| Date now = new Date(); long time = now.getTime();
|
日期格式化类
(可以使用 java.time 中的 DateTimeFormatter 替代)
使用示例
1 2 3 4 5 6 7
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = sdf.format(new Date());
Date date = sdf.parse("2023-01-01 12:00:00");
|
注意事项
- 非线程安全,多线程环境需配合 ThreadLocal 使用
- 格式符号:
yyyy
-年,MM
-月,dd
-日,HH
-小时(24h 制)
Calendar
日期操作类
常用方法
1 2 3 4 5 6 7 8 9 10
| Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
cal.set(2023, Calendar.JANUARY, 1);
cal.add(Calendar.DAY_OF_MONTH, 5);
|
注意事项
- 月份从 0 开始(0=January)
- 推荐使用
Calendar.getInstance()
获取实例
java.time
参考:https://javaguidepro.com/blog/datetime-java/
基本使用
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
| LocalDate currentDate = LocalDate.now();
LocalDate specificDate = LocalDate.of(2024, 1, 1);
LocalTime currentTime = LocalTime.now();
LocalTime specificTime = LocalTime.of(12, 30, 0);
LocalDateTime currentDateTime = LocalDateTime.now();
LocalDateTime specificDateTime = LocalDateTime.of(2024, 1, 1, 12, 30, 0);
ZonedDateTime currentZonedDateTime = ZonedDateTime.now();
ZoneId zoneId = ZoneId.of("Asia/Shanghai"); ZonedDateTime specificZonedDateTime = ZonedDateTime.of(2024, 1, 1, 12, 30, 0, 0, zoneId);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.now(); String formattedDateTime = dateTime.format(formatter);
LocalDate startDate = LocalDate.of(2024, 1, 1); LocalDate endDate = LocalDate.of(2024, 12, 31); boolean isBefore = startDate.isBefore(endDate);
Period period = Period.between(startDate, endDate); System.out.println("两个日期之间的差值: " + period.getYears() + " 年 " + period.getMonths() + " 月 " + period.getDays() + " 天"); String dateStr = "2024-01-01"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate parsedDate = LocalDate.parse(dateStr, formatter); System.out.println("解析后的日期: " + parsedDate);
LocalDate specifiedDate = LocalDate.of(2023, 10, 1);
LocalDate resultDate = specifiedDate.plusDays(5);
|
数学
Number
数值包装类的基类
主要子类
1 2
| Integer num1 = Integer.valueOf("100"); Double num2 = Double.parseDouble("3.14");
|
Math
数学计算工具类
常用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| Math.abs(-10);
Math.max(5, 10);
Math.pow(2, 3);
Math.sqrt(9);
double rand = Math.random();
Math.round(3.6);
|
进阶方法
1 2 3 4 5 6 7 8
| Math.sin(Math.PI/2);
Math.log(Math.E);
Math.toRadians(180);
|
参考资料