1、API-StringBuilder
对于频繁地拼接字符串的操作,建议用StringBuilder,代表可变字符串对象,是用来操作字符串的
好处:拼接字符串性能好,代码优雅
注意:代码较少或字符串不需要操作时时,使用String
基本API
2、API-StringBuilder拼接案例
package com.itheima.d1_stringBuilder;
public class Test3 {
public static void main(String[] args) {
// 目标:完成遍历数组内容,并拼接成指定格式的案例。
System.out.println(getArrayData(new int[]{11, 22, 33}));
System.out.println(getArrayData(new int[]{}));
}
public static String getArrayData(int[] arr) {
//将数组内容,按照格式进行拼接返回字符串 [元素, 元素, 元素]
// 1、判断arr是否为null
if (arr == null) {
return null;// 返回结果,结束方法
}
if (arr.length==0) {
return "[]";
}
// 2、arr数组对象存在。 arr = [11, 22, 33]
//为了提升性能,使用StringBuilder来拼接
StringBuilder sb = new StringBuilder("[");
//遍历数组
for (int i = 0; i < arr.length; i++) {
//if-else 实现
/* if (i == arr.length - 1) {
sb.append(arr[i]).append("]");
} else {
sb.append(arr[i]).append(", ");
}*/
//三元运算符实现
sb.append(arr[i]).append(i == arr.length - 1 ? "]" : ", ");
}
return sb.toString();
}
}
3、API-StringJoiner
更方便的拼接工具
构造器直接提供了可以定义分隔符,起始符号,结束符号
常用方法:
4、API-Math、System
Math: 内容全是静态方法,提供的对数的操作(需要记一下round中的Round类型 常用Round_Up -- 四舍五入 ,实在看不懂 直接看提示 Math.round() )
System:
常用API:System.currentTimeMillis();
5、API-Runtime
Runtime: 代表程序所在的运行环境;是一个单例类
6、API-BigDecimal讲解
用于解决浮点型运算时,出现结果失真的问题。
常见方法
7、传统时间:Date日期类
Date
常用方法:
8、传统时间:SimpleDateFormat
SimpleDateFormat
代表简单日期格式化,可以用来把日期对象,时间毫秒值格式化成我们想要的形式
时间格式常见符号:
常用方法:
9、传统时间:秒杀案例
思路:
1.创建对应形式的SimpleDateFormat 日期解析器,将字符串放入其中解析
2.获取秒杀时间的开始,结束时间的Date对象
3.获取所有人买东西时的Date对象
4.将所有的Date对象转换为相应的long值
5.对比差值,如果购买人的long值大于start时间的long值并且小于end的long值
10、传统时间:Calendar
代表的系统此刻时间对应的日历,可以单独的修改时间中的年,月,日,时,分,秒,等
注意获取到的值是符合国外的日期习惯的
由于国外的月份和国内的月份不一样,所以
,当我们设置2月的时候其实代表的是我们国内的3月年份:
我们输入的年份月份:2月(实际上是我国3月份)日:1日 (周也是同理)
11、为啥要学习JDK8新增的时间
12、JDK8新时间:LocalDate、LocalTime、LocalDateTime
LocateDate
LocalTime
LocalDateTime
转换的API
13、JDK8新时间:ZoneId、ZonedDateTime
ZoneId 和 ZoneDateTime