可变字符串类
StringBuilder
构造方法
1.无参构造出来 容量为16 (16个字符)
2.字符串初始化出来 容量是16+字符串
3.也可以直接指定
常用成员方法
// 1.使用无参方式构造StringBuilder类型的对象并打印容量和长度
StringBuilder sb1 = new StringBuilder();
System.out.println("sb1 = " + sb1); // 自动调用toString方法 啥也没有
System.out.println("容量是:" + sb1.capacity()); // 16
System.out.println("长度是:" + sb1.length()); // 0
System.out.println("-----------------------------------------------------------");
// 2.使用参数指定的容量来构造对象并打印容量和长度
StringBuilder sb2 = new StringBuilder(20);
System.out.println("sb2 = " + sb2); // 自动调用toString方法 啥也没有
System.out.println("容量是:" + sb2.capacity()); // 20
System.out.println("长度是:" + sb2.length()); // 0
System.out.println("-----------------------------------------------------------");
// 3.根据参数指定的字符串内容来构造对象并打印容量和长度
StringBuilder sb3 = new StringBuilder("hello");
System.out.println("sb3 = " + sb3); // 自动调用toString方法 hello
System.out.println("容量是:" + sb3.capacity()); // 16 + 5 = 21
System.out.println("长度是:" + sb3.length()); // 5
System.out.println("-----------------------------------------------------------");
String str1 = new String("hello");
String str2 = str1.toUpperCase();
System.out.println("str2 = " + str2); // HELLO
System.out.println("str1 = " + str1); // hello 字符串本身是个常量不会改变
// 4.实现向字符串中插入和追加字符串内容
// 向下标为0的位置插入字符串内容"abcd",也就是向开头位置插入字符串内容
StringBuilder sb4 = sb3.insert(0, "abcd");
System.out.println("sb4 = " + sb4); // abcdhello 返回调用对象自己的引用,也就是返回值和调用对象是同一个字符串
System.out.println("sb3 = " + sb3); // abcdhello
// 向中间位置插入字符串"1234"
sb3.insert(4, "1234");
System.out.println("sb3 = " + sb3); // abcd1234hello
// 向末尾位置插入字符串"ABCD"
sb3.insert(sb3.length(), "ABCD");
System.out.println("sb3 = " + sb3); // abcd1234helloABCD
System.out.println("-----------------------------------------------------------");
// 向末尾位置追加字符串内容
sb3.append("world");
System.out.println("sb3 = " + sb3); // abcd1234helloABCDworld
// 当字符串的长度超过了字符串对象的初始容量时,该字符串对象会自动扩容,默认扩容算法是:原始容量*2 + 2 => 21*2 + 2 => 44
// 底层采用byte数组来存放所有的字符内容。
// ctrl+alt+左右方向键 表示回到上/下一个位置
System.out.println("容量是:" + sb3.capacity()); // 44
System.out.println("长度是:" + sb3.length()); // 22
System.out.println("-----------------------------------------------------------");
// 5.实现字符串中字符和字符串的删除
// 表示删除下标为8的单个字符
sb3.deleteCharAt(8);
System.out.println("sb3 = " + sb3); // abcd1234elloABCDworld
// 使用for循环删除多个字符
for (int i = 8; i < 12; i++) {
// 由结果可知:删除一个字符后就跳过一个字符继续删除,因为每当删除一个字符后后面的字符会向前补位,因为下标会发生变化
//sb3.deleteCharAt(i);
// 始终删除下标为8的字符
sb3.deleteCharAt(8);
}
System.out.println("删除后的字符串是:" + sb3); // abcd1234ABCDworld
System.out.println("-----------------------------------------------------------");
// 删除起始字符串abcd,包含0但不包含4
sb3.delete(0, 4);
System.out.println("sb3 = " + sb3); // 1234ABCDworld
// 删除中间字符串
sb3.delete(4, 8);
System.out.println("sb3 = " + sb3); // 1234world
// 删除末尾字符串
sb3.delete(4, sb3.length());
System.out.println("sb3 = " + sb3); // 1234
System.out.println("-----------------------------------------------------------");
// 6.实现字符串内容的修改、查找以及反转操作
// 表示将下标为0这个位置的字符修改为'a'
sb3.setCharAt(0, 'a');
System.out.println("修改单个字符后的内容是:" + sb3); // a234
// 修改"234"为"bcd"
sb3.replace(1, 4, "bcd");
System.out.println("修改字符串后的结果是:" + sb3); // abcd
// 实现查找的功能
int pos = sb3.indexOf("b");
System.out.println("从前向后查找的结果是:" + pos); // 1
pos = sb3.lastIndexOf("b");
System.out.println("从后向前查找的结果是:" + pos); // 1
// 实现字符串的反转功能
sb3.reverse();
System.out.println("反转后的结果是:" + sb3); // dcba
System.out.println("-----------------------------------------------------------");
// 7.笔试考点
// 考点一:既然StringBuilder类的对象本身可以修改,那么为什么成员方法还有返回值呢?
// 解析:为了连续调用
//sb3.reverse().append("1").append("2").insert(0, "3").delete(0, 1).reverse();
// 考点二:如何实现StringBuilder类型和String类型之间的转换呢?
//String str3 = sb3.toString();
//StringBuilder sb5 = new StringBuilder(str3);
// 考点三:String、StringBuilder、StringBuffer之间效率谁高?排列如何?
// String < StringBuffer < StringBuilder
StringBuilder类扩容
原容积*2+2
System
//获取当前系统时间距离19710 1 1 0 0
long l = System.currentTimeMillis();
System.out.println(l);
// 1.使用无参方式构造Date对象并打印
Date d1 = new Date();
System.out.println("d1 = " + d1); // 获取当前系统时间
System.out.println("------------------------------------");
// 2.使用参数指定的毫秒数来构造Date对象并打印 1秒 = 1000毫秒 东八区
Date d2 = new Date(1000);
System.out.println("d2 = " + d2); // 1970 1 1 8 0 1
System.out.println("------------------------------------");
// 3.获取调用对象距离1970年1月1日0时0分0秒的毫秒数
long msec = d2.getTime();
System.out.println("获取到的毫秒数是:" + msec); // 1000
// 4.设置调用对象所表示的时间点为参数指定的毫秒数
d2.setTime(2000);
System.out.println("修改后的时间是:" + d2); // 1970 1 1 8 0 2
SimpleDateFormat
// 1.获取当前系统时间并打印
Date d1 = new Date();
System.out.println("d1 = " + d1);
// 2.构造SimpleDateFormat类型的对象并指定格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 3.实现日期类型向文本类型的转换并打印
// alt+Enter 可以实现返回值的生成
String format = sdf.format(d1);
System.out.println("转换后的日期为:" + format);
// 4.实现文本类型到日期类型的转换并打印
Date parse = sdf.parse(format);
System.out.println("转回日期格式的结果为:" + parse);
// 1.使用过时的方法按照指定的年月日时间分来构造对象
Date d1 = new Date(2008-1900, 8-1, 8, 20, 8, 8);
// 2.设置日期对象的格式并打印
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf.format(d1);
System.out.println("获取到的时间是:" + format); // 2008 8 8 20 8 8
System.out.println("-----------------------------------------------------");
// 2.使用取代的方式按照指定的年月日时分秒来构造对象
// 2.1 获取Calendar类型的引用
// 考点:既然Calendar是个抽象类不能创建对象,那么下面的方法为啥可以获取Calendar类型的引用呢?
// 解析:由源码可知,返回的并不是Calendar类型的对象,而是Calendar类的子类GregorianCalendar等对象,形成了多态
// 多态的使用场合之三
Calendar instance = Calendar.getInstance();
// 2.2 设置指定的年月日时分秒信息
instance.set(2008, 8-1, 8, 20, 8, 8);
// 2.3 转换为Date类型的对象
Date d2 = instance.getTime();
String format1 = sdf.format(d2);
System.out.println("获取到的时间是:" + format1); // 2008 8 8 20 8 8
System.out.println("-----------------------------------------------------");
// 3.向指定的字段设置以及增加指定的数值
instance.set(Calendar.YEAR, 2018);
// 转换为Date类型并按照指定的格式打印
Date d3 = instance.getTime();
System.out.println("设置年份后的结果是:" + sdf.format(d3)); // 2018 8 8 20 8 8
instance.add(Calendar.MONTH, 2);
Date d4 = instance.getTime();
System.out.println("增加月份后的结果是:" + sdf.format(d4)); // 2018 10 8 20 8 8
// 1.获取当前日期信息并打印
LocalDate now = LocalDate.now();
System.out.println("获取到的当前日期是:" + now);
// 2.获取当前时间信息并打印
LocalTime now1 = LocalTime.now();
System.out.println("获取到的当前时间是:" + now1);
// 3.获取当前日期时间信息并打印,使用最多
LocalDateTime now2 = LocalDateTime.now();
System.out.println("获取到的当前日期时间是:" + now2);
System.out.println("-------------------------------------------------------");
// 4.使用参数指定的年月日时分秒信息来获取对象并打印
// 使用ctrl+F12来查找指定的方法
LocalDateTime of = LocalDateTime.of(2008, 8, 8, 20, 8, 8);
System.out.println("指定的日期时间是:" + of); // 自动调用toString方法
System.out.println("获取到的年是:" + of.getYear()); // 2008
System.out.println("获取到的月是:" + of.getMonthValue()); // 8
System.out.println("获取到的日是:" + of.getDayOfMonth()); // 8
System.out.println("获取到的时是:" + of.getHour()); // 20
System.out.println("获取到的分是:" + of.getMinute()); // 8
System.out.println("获取到的秒是:" + of.getSecond()); // 8
System.out.println("-------------------------------------------------------");
// 5.实现特征的设置并打印
// 与String类型相似,调用对象本身的数据内容不会改变,返回值相当于创建了一个新的对象,由此证明了不可变性
LocalDateTime localDateTime = of.withYear(2012);
System.out.println("localDateTime = " + localDateTime); // 2012-08-08T20:08:08
System.out.println("of = " + of); // 2008-08-08T20:08:08
LocalDateTime localDateTime1 = localDateTime.withMonth(12);
System.out.println("localDateTime1 = " + localDateTime1); // 2012 12 8 20 8 8
System.out.println("-------------------------------------------------------");
// 6.实现特征的增加并打印
LocalDateTime localDateTime2 = localDateTime1.plusDays(2);
System.out.println("localDateTime2 = " + localDateTime2); // 2012 12 10 20 8 8
System.out.println("localDateTime1 = " + localDateTime1); // 2012 12 8 20 8 8
LocalDateTime localDateTime3 = localDateTime2.plusHours(3);
System.out.println("localDateTime3 = " + localDateTime3); // 2012 12 10 23 8 8
System.out.println("-------------------------------------------------------");
// 7.实现特征的减少并打印
LocalDateTime localDateTime4 = localDateTime3.minusMinutes(1);
System.out.println("localDateTime4 = " + localDateTime4); // 2012 12 10 23 7 8
System.out.println("localDateTime3 = " + localDateTime3); // 2012 12 10 23 8 8
LocalDateTime localDateTime5 = localDateTime4.minusSeconds(3);
System.out.println("localDateTime5 = " + localDateTime5); // 2012 12 10 23 7 5
// 1.使用Instant类来获取当前系统时间 并不是当前系统的默认时区 本初子午线 差8小时 东八区
Instant now = Instant.now();
System.out.println("获取到的当前时间为:" + now);
// 2.加上时区所差的8个小时
OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
System.out.println("偏移后的日期时间为:" + offsetDateTime);
System.out.println("--------------------------------------------------------");
// 3.获取当前调用对象距离标准基准时间的毫秒数
long g1 = now.toEpochMilli();
System.out.println("获取到的毫秒差为:" + g1);
// 4.根据参数指定的毫秒数来构造对象
Instant instant = Instant.ofEpochMilli(g1);
System.out.println("根据参数指定的毫秒数构造出来的对象为:" + instant);
// 1.获取当前系统的日期时间并打印
LocalDateTime now = LocalDateTime.now();
System.out.println("now = " + now);
// 2.按照指定的格式准备一个DateTimeFormatter类型的对象
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 3.实现日期时间向字符串类型的转换并打印
String str = dateTimeFormatter.format(now);
System.out.println("调整格式后的结果是:" + str);
// 4.实现字符串类型到日期时间类型的转换并打印
TemporalAccessor parse = dateTimeFormatter.parse(str);
System.out.println("转回去的结果是:" + parse);