1.1 可变字符串类

image.png
image.png
image.png

  • 当字符串的长度超过字符串对象的初始容量时,该字符串对象会自动扩容
    • 默认扩容算法是:原始容量*2 + 2

image.png

  • StringBuilder类的源码分析

底层采用byte[]来存放所有内容。

  • indexOf查找
  • setCharAt() 修改单个字符
  • replace

笔试考点

//考点一: 既然StringBuilder类的对象本身可以修改, 那么为什么成员方法还有返回值呢?
//解析:为了连续调用
//str4.reverse().reverse();
//考点二:如何实现StringBuiler类型和String类型之间的转换呢?
//str4.toString();
//StringBuilder str5 = new StringBuilder(“1234”);
//
//考点3: 效率比较
//String < StringBuffer < StringBuilder

  1. package com.lagou.task13;
  2. /**
  3. * @author 西风月
  4. * @date 2020/7/27
  5. * @description
  6. */
  7. public class StringBuilderTest {
  8. public static void main(String[] args) {
  9. StringBuilder str1 = new StringBuilder();
  10. System.out.println("str1 = " + str1);
  11. System.out.println("str1.capacity() = " + str1.capacity());
  12. System.out.println("str1.length() = " + str1.length());
  13. System.out.println();
  14. StringBuilder str2 = new StringBuilder(20);
  15. System.out.println("str2 = " + str1);
  16. System.out.println("str2.capacity() = " + str2.capacity());
  17. System.out.println("str2.length() = " + str2.length());
  18. System.out.println();
  19. StringBuilder str3 = new StringBuilder("hello");
  20. System.out.println("str3 = " + str1);
  21. System.out.println("str3.capacity() = " + str3.capacity());
  22. System.out.println("str3.length() = " + str3.length());
  23. System.out.println(str3.insert(0, "abcd"));
  24. System.out.println(str3);
  25. System.out.println(str3.append("abcd"));
  26. System.out.println(str3.append("666777112"));
  27. System.out.println(str3.length());
  28. System.out.println(str3.capacity());
  29. StringBuilder str4 = new StringBuilder("012345678abcdefg87654321abcd");
  30. System.out.println(str4.deleteCharAt(8));
  31. for(int i = 8; i < 12; i++) {
  32. //System.out.println(str4.deleteCharAt(i));
  33. System.out.println(str4.deleteCharAt(8));
  34. }
  35. //01234567efg87654321abcd
  36. System.out.println(str4.delete(str4.length() -4 , str4.length()));
  37. //01234567efg87654321
  38. System.out.println(str4.replace(1,5,"abcd"));
  39. //0abcd567efg87654321
  40. str4.setCharAt(0, 'A');
  41. System.out.println(str4);
  42. //Aabcd567efg87654321
  43. System.out.println(str4.reverse());
  44. System.out.println(str4.indexOf("b"));
  45. System.out.println(str4.lastIndexOf("b"));
  46. //笔试考点
  47. //考点一: 既然StringBuilder类的对象本身可以修改, 那么为什么成员方法还有返回值呢?
  48. //解析:为了连续调用
  49. //str4.reverse().reverse();
  50. //考点二:如何实现StringBuiler类型和String类型之间的转换呢?
  51. //str4.toString();
  52. //StringBuilder str5 = new StringBuilder("1234");
  53. //
  54. //考点3: 效率比较
  55. //String < StringBuffer < StringBuilder
  56. }
  57. }

1.2 Java8之前的日期相关类

image.png

  1. package com.lagou.task13;
  2. /**
  3. * @author 西风月
  4. * @date 2020/7/28
  5. * @description
  6. */
  7. public class SystemTest {
  8. public static void main(String[] args) {
  9. //1. 获取当前系统时间距离1970年1月1日0分0秒的毫秒数
  10. System.out.println("Now timegap [197001010000]" + System.currentTimeMillis() + "毫秒");
  11. //通常用于测试某一段代码额执行效率 --> 计算两个时间点的时间差
  12. }
  13. }

image.png

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);
    }
}

image.png

image.png

image.png

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中的日期相关类

image.png

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));
    }
}

image.png
image.png

任务的总结

  • 可变字符串类(重点)
    • StringBuilder
    • StringBuffer
  • Java8之前的日期相关类(熟悉)
    • Date类
    • Calendar类
    • SimpleDateFormat类
  • Java8增加的日期相关类(熟悉)
    • LocalDateTime类
    • Instant类
    • DateTimeFormatter类
    • 熟悉概念与常用方法