1.1 可变字符串类
- 当字符串的长度超过字符串对象的初始容量时,该字符串对象会自动扩容
- 默认扩容算法是:原始容量*2 + 2
- StringBuilder类的源码分析
底层采用byte[]来存放所有内容。
- indexOf查找
- setCharAt() 修改单个字符
- replace
笔试考点
//考点一: 既然StringBuilder类的对象本身可以修改, 那么为什么成员方法还有返回值呢?
//解析:为了连续调用
//str4.reverse().reverse();
//考点二:如何实现StringBuiler类型和String类型之间的转换呢?
//str4.toString();
//StringBuilder str5 = new StringBuilder(“1234”);
//
//考点3: 效率比较
//String < StringBuffer < StringBuilder
package com.lagou.task13;
/**
* @author 西风月
* @date 2020/7/27
* @description
*/
public class StringBuilderTest {
public static void main(String[] args) {
StringBuilder str1 = new StringBuilder();
System.out.println("str1 = " + str1);
System.out.println("str1.capacity() = " + str1.capacity());
System.out.println("str1.length() = " + str1.length());
System.out.println();
StringBuilder str2 = new StringBuilder(20);
System.out.println("str2 = " + str1);
System.out.println("str2.capacity() = " + str2.capacity());
System.out.println("str2.length() = " + str2.length());
System.out.println();
StringBuilder str3 = new StringBuilder("hello");
System.out.println("str3 = " + str1);
System.out.println("str3.capacity() = " + str3.capacity());
System.out.println("str3.length() = " + str3.length());
System.out.println(str3.insert(0, "abcd"));
System.out.println(str3);
System.out.println(str3.append("abcd"));
System.out.println(str3.append("666777112"));
System.out.println(str3.length());
System.out.println(str3.capacity());
StringBuilder str4 = new StringBuilder("012345678abcdefg87654321abcd");
System.out.println(str4.deleteCharAt(8));
for(int i = 8; i < 12; i++) {
//System.out.println(str4.deleteCharAt(i));
System.out.println(str4.deleteCharAt(8));
}
//01234567efg87654321abcd
System.out.println(str4.delete(str4.length() -4 , str4.length()));
//01234567efg87654321
System.out.println(str4.replace(1,5,"abcd"));
//0abcd567efg87654321
str4.setCharAt(0, 'A');
System.out.println(str4);
//Aabcd567efg87654321
System.out.println(str4.reverse());
System.out.println(str4.indexOf("b"));
System.out.println(str4.lastIndexOf("b"));
//笔试考点
//考点一: 既然StringBuilder类的对象本身可以修改, 那么为什么成员方法还有返回值呢?
//解析:为了连续调用
//str4.reverse().reverse();
//考点二:如何实现StringBuiler类型和String类型之间的转换呢?
//str4.toString();
//StringBuilder str5 = new StringBuilder("1234");
//
//考点3: 效率比较
//String < StringBuffer < StringBuilder
}
}
1.2 Java8之前的日期相关类
package com.lagou.task13;
/**
* @author 西风月
* @date 2020/7/28
* @description
*/
public class SystemTest {
public static void main(String[] args) {
//1. 获取当前系统时间距离1970年1月1日0分0秒的毫秒数
System.out.println("Now timegap [197001010000]" + System.currentTimeMillis() + "毫秒");
//通常用于测试某一段代码额执行效率 --> 计算两个时间点的时间差
}
}
package com.lagou.task13;
import java.util.Date;
/**
* @author 西风月
* @date 2020/7/28
* @description
*/
public class DateTest {
public static void main(String[] args) {
Date d1 = new Date();
System.out.println("d1 = " + d1);
System.out.println("=======================================");
Date d2 = new Date(86400000);
System.out.println("d2 = " + d2); //d2 = Fri Jan 02 08:00:00 CST 1970 东八区时差
System.out.println(d2.getTime());
d2.setTime(d2.getTime()+86400000*3);
System.out.println("d2 = " + d2);
}
}
package com.lagou.task13;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author 西风月
* @date 2020/7/28
* @description
*/
public class CalendarTest {
public static void main(String[] args) {
Date d1 = new Date(2020-1900,7-1,28,20,30);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(d1));
System.out.println("==================================================");
Calendar instance = Calendar.getInstance();
instance.set(2020,7-1,28,20,30, 00);
Date d2 = instance.getTime();
System.out.println("获取到的时间是: " + sdf.format(d2));
System.out.println("==================================================");
instance.set(Calendar.YEAR, 2018);
System.out.println(sdf.format(instance.getTime()));
instance.add(Calendar.MONTH, 12);
System.out.println(sdf.format(instance.getTime()));
//考点:既然Calendar是抽象类,不能创建对象,下面的方法为什么可以获取Calendar的引用变量 解析:由源码可知,返回的并不是Calendar类型的对象,而是子类对象,形成了多态
}
}
1.3 Java8中的日期相关类
package com.lagou.task13;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
/**
* @author 西风月
* @date 2020/7/28
* @description
*/
public class LocalDateTimeTest {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println(now);
LocalTime now1 = LocalTime.now();
System.out.println(now1);
//使用最多的方法是:LocalDateTime
LocalDateTime now2 = LocalDateTime.now();
System.out.println(now2);
System.out.println("===============================================");
LocalDateTime of = LocalDateTime.of(2020, 7, 28, 21, 11, 12);
System.out.println(of); //自动调用toString()方法
System.out.println(of.getYear());
System.out.println(of.getMonthValue());
System.out.println(of.getDayOfMonth());
System.out.println(of.getHour());
System.out.println(of.getMinute());
System.out.println(of.getSecond());
System.out.println("===============================================");
LocalDateTime localDateTime = of.withYear(2019);
System.out.println(localDateTime); //调用对象本身的内容不会改变
System.out.println(of.withMonth(12));
System.out.println("===============================================");
System.out.println(of.plusDays(2)); //调用对象本身的内容不会改变
System.out.println(of.plusHours(2));
System.out.println(of.minusMinutes(1));
System.out.println(of.minusSeconds(60));
}
}
任务的总结
- 可变字符串类(重点)
- StringBuilder
- StringBuffer
- Java8之前的日期相关类(熟悉)
- Date类
- Calendar类
- SimpleDateFormat类
- Java8增加的日期相关类(熟悉)
- LocalDateTime类
- Instant类
- DateTimeFormatter类
- 熟悉概念与常用方法