1.StringBuilder
a.概述
b.方法
c.StringBuilder的存储原理
d.链式编程
e.StringBuilder 与 + 号的联系
f.StringBuilder类与StringBuffer类
2.Date类
a.Date的构造方法
b.Date类的成员方法
c.计算和判断功能
3.LocalDate
4.LocalTime类
5.LocalDateTime:
6.日期字符串与日期对象的转换
1.StringBuilder
a.概述
StringBuilder概述:
一个可变的字符序列
String类
字符串常量(双引号括起来的)一经初始化,不可改变“abc”
b.方法
1.构造方法
StringBuilder()
构造一个不带任何字符的字符串生成器,其初始容量为 16 个字符。
StringBuilder(String str)
构造一个字符串生成器,并初始化为指定的字符串内容。
public class StringBuilderDemo02 {
public static void main(String[] args) {
//method01();
StringBuilder sb = new StringBuilder("def");//在创建这个容器的时候,里面初始化内容
System.out.println(sb);//def //取出容器中的内容,默认调用sb.toString
System.out.println(sb.toString());//def //StringBuilder中已近重写了toString()方法,
// 就是为了取出StringBuilder容器中的内容
}
private static void method01() {
StringBuilder sb = new StringBuilder();//StringBuilder可以看作一个容器
//刚开始这个容器是空的
System.out.println(sb); //打印的时候取出容器的内容,因为是空的,所以什么都不打印
}
}
2.成员方法
append()方法:不断向容器的末尾追加任意类型的数据
StringBuilder append(boolean b)
将 boolean 参数的字符串表示形式追加到序列。
StringBuilder append(char c)
将 char 参数的字符串表示形式追加到此序列。
StringBuilder append(char[] str)
将 char 数组参数的字符串表示形式追加到此序列。
int length()
返回长度(字符数)。
String substring(int start, int end)
返回一个新的 String,它包含此序列当前所包含字符的子序列。
StringBuilder delete(int start, int end)
移除此序列的子字符串中的字符。
StringBuilder reverse() //反转容器中的内容
将此字符序列用其反转形式取代。
public class StringBuilderDemo03 {
public static void main(String[] args) {
//method01();
//method02();
//method03();
//method04();
//method05();
StringBuilder sb = new StringBuilder("a2mcd");
sb.reverse();
System.out.println(sb);//dcm2a
}
private static void method05() {
StringBuilder sb = new StringBuilder("losnk");
sb.delete(1, 3); //含头不含尾
System.out.println(sb);//lnk 删除范围 [starIndex,endIndex-1]
//delete 直接删除容器中的字符,改变了容器的内容
}
private static void method04() {
StringBuilder sb = new StringBuilder("afkln");
String subStr = sb.substring(1, 3);// 含头不含尾
System.out.println(subStr);//fk 截取范围 [starIndex,endIndex-1]
}
private static void method03() {
StringBuilder sb = new StringBuilder();
sb.append("def");
sb.append("123");
System.out.println(sb.length());//"def123"6
}
//append方法
private static void method02() {
StringBuilder sb = new StringBuilder("cd");
char[] chars = {'3', 'a'};
sb.append(chars);
sb.append(false);
System.out.println(sb);//cd3afalse
}
//append方法
private static void method01() {
StringBuilder sb = new StringBuilder();//空的容器
sb.append(5); //append()方法其实就是向容器中追加数据
System.out.println(sb);//5
sb.append("abc");
System.out.println(sb);//5abc
}
}
c.StringBuilder的存储原理
StringBuilder的存储原理
1.append()添加的所有数据类型的数据都会转变成一个个字符,追加到底层value字符数组中
2.最终当调用sb.toString()时候,会把字符数组中的所有字符转换为一个字符串
//扩容思想
1.如果追加的内容超过了默认长度16,此时会再创建一个新的字符数组
new char[2*原始长度+2]
2.接着吧原字符数组中的内容拷贝到新的字符数组中
例如:把原来的16个字符’c‘字符拷贝到新数组中去
3.再把新添加的字符追加到新数组的末尾
例如:把’f‘追加到最后一个’c‘字符的末尾
4.把新数组的地址值赋值给value
char[] value=new char[2*原始长度+2]
public class StringBuilderDemo04 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();//当创建一个StringBuilder容器的时候,底层会初始化一个数组:char[] value=new char[16]
sb.append(9);//将9转换成字符类型‘9’ 追加到value字符数组中
sb.append(5.7); //将5.7转化为字符:‘5’ ‘.‘ ’7‘ 追加到字符末尾
sb.append("abc");//将“abc”转换为’a' ‘b’ ‘c'追加到’7‘字符后面
System.out.println(sb);//sb.toString() 将value字符中存储的字符转换为一个字符串输出
}
}
d.链式编程
StringBuilder的append方法的返回值:它的返回值返回的是原容器对象
public class StringBuilderDemo07 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
/* sb.append(7);
sb.append("def");*/
StringBuilder sb2 = sb.append(7).append("def").append(3.14);//链式编程
System.out.println(sb);
System.out.println(sb2);
}
}
e.StringBuilder 与 + 号的联系
String str = "a" + "b"; //如果通过+号拼接的都是常量,那么底层会有常量优化机制
//在编译后,直接把这句代码替换为:String str="ab";
System.out.println(str);
String str = "c";
String str2 =str+ "efg"; //如果通过+号拼接的内容中含有变量,那么底层就会创建StringBuilder对象来拼接
//在编译后这句代码会替换为:
// StringBuilder sb=new StringBuilder();
// sb.append(str);
// sb.append("efg");
// String str2 = sb.toString();
// 最终的代码: String str2 = new StringBuilder().append(str).append("efg").toString();
System.out.println(str2);
public class StringBuilderDemo08 {
public static void main(String[] args) {
//method01();
String str = "c";
String str2 =str+ "efg"; //如果通过+号拼接的内容中含有变量,那么底层就会创建StringBuilder对象来拼接
//在编译后这句代码会替换为:
// StringBuilder sb=new StringBuilder();
// sb.append(str);
// sb.append("efg");
// String str2 = sb.toString();
// 最终的代码: String str2 = new StringBuilder().append(str).append("efg").toString();
System.out.println(str2);
}
private static void method01() {
String str = "a" + "b"; //如果通过+号拼接的都是常量,那么底层会有常量优化机制
//在编译后,直接把这句代码替换为:String str="ab";
System.out.println(str);
}
}
f.StringBuilder类与StringBuffer类
相同点:
StringBuilder和StringBuffer里面的构造方法和成员方法基本一致,使用方法也一致
不同点:
StringBuilder是线程不安全,效率高
StringBuffer是线程安全的,效率低
2.Date类
a.Date的构造方法
Date()
分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)。
Date(long date)
分配 Date 对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数。
public class DateDemo01 {
public static void main(String[] args) {
//method01();
Date date = new Date(3000);
System.out.println(date);//Thu Jan 01 08:00:03 CST 1970
//在1970年8:00的基础上偏移3秒
}
private static void method01() {
Date date = new Date();//封装的是当前日期(系统日期)
System.out.println(date);//Thu Nov 02 19:13:28 CST 2023
}
}
b.Date类的成员方法
long getTime()
返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
public class DateDemo02 {
public static void main(String[] args) {
//method01();
long startTime = new Date().getTime();//new Date() 当前时间(b) - 1970年1月1日00:00:00 的毫秒值
for (int i = 0; i < 10000; i++) {
}
long endTime = new Date().getTime();//new Date() 当前时间(c)- 1970年1月1日00:00:00 的毫秒值
System.out.println(endTime - startTime);//startTime和endTime之间的代码执行的时间
}
private static void method01() {
Date date = new Date();
System.out.println(date.getTime());//1698923928937
//getTime()获取的是从1970年1月1日00:00:00 到 当前系统时间(new Date())所经过的毫秒值
}
}
3.LocalDate
a.获取功能
LocalDate类
它里面封装的只有年月日,而且默认格式为年-月-日
获取功能:
static LocalDate now()
public int getYear()
Month getMonth()
int getDayOfYear()
public class LocalDateDemo01 {
public static void main(String[] args) {
//method01();
LocalDate now = LocalDate.now();
System.out.println(now);
int year = now.getYear();
System.out.println(year);
System.out.println(now.getMonth());
System.out.println(now.getDayOfMonth());
}
private static void method01() {
LocalDate now = LocalDate.now();//获取当前的年月日
System.out.println(now); //2023-11-02
}
}
b.设置功能
LocalDate的设置功能
static LocalDate of(int year, int month, int dayOfMonth)
LocalDate withYear(int year) //设置指定日期的年
LocalDate withMonth(int month) //设置月
LocalDate withDayOfYear(int dayOfYear) //设置这一年的第几天
LocalDate withDayOfMonth(int dayOfMonth) //设置日
public class LocalDateDemo02 {
public static void main(String[] args) {
//method01();
LocalDate newDate = LocalDate.now().withYear(2015);
System.out.println(newDate);
LocalDate newDate2 = newDate.withMonth(3);
System.out.println(newDate2);
System.out.println(newDate2.withDayOfMonth(11));
System.out.println(newDate2.withDayOfYear(32));
}
private static void method01() {
System.out.println(LocalDate.now());
LocalDate newDate = LocalDate.of(2016, 10, 23);//of方法可以设置指定的年月日
System.out.println(newDate);
}
}
c.计算和判断功能
LocalDate的计算和判断功能:
LocalDate plusYears(long yearsToAdd) //在原有的年份基础上,加上指定的年份
LocalDate minusMonths(long monthsToSubtract) //在原有的月份的基础上,减去指定的月份
boolean isLeapYear() //如果当前年份是闰年,返回true,不是就返回平年
boolean isEqual(ChronoLocalDate other)
public class LocalDateDemo03 {
public static void main(String[] args) {
//method01();
//method02();
LocalDate now = LocalDate.now();
System.out.println(now.isEqual(now));
LocalDate now2 = LocalDate.of(2023, 11, 3);
System.out.println(now.isEqual(now2));
}
private static void method02() {
LocalDate now = LocalDate.now();
System.out.println(now);
System.out.println(now.isLeapYear());
System.out.println(now.withYear(2000).isLeapYear());
}
private static void method01() {
LocalDate now = LocalDate.now();
System.out.println(now);
LocalDate now2 = now.plusYears(3);
System.out.println(now2);
LocalDate now3 = now.minusMonths(4);
System.out.println(now3);
}
}
4.LocalTime类
LocalTome:
这个类中封装的是 时 分 秒 毫秒 纳秒
默认格式: 时:分:秒
1.获取功能
static LocalTime now() //获取当前的时分秒以及毫秒
int getHour() //获取小时
int getMinute() //获取分钟
int getSecond() //获取秒
int getNano() //获取纳秒
2.设置功能
static LocalTime of(int hour, int minute, int second) //将时间设置成指定的时分秒
LocalTime withHour(int hour) //设置小时
LocalTime withMinute(int minute) //设置分钟
LocalTime withSecond(int second) //设置秒
LocalTime withNano(int nanoOfSecond) //设置纳秒,如果设置为0,毫秒就不显示
3.计算和判断功能
LocalTime plusHours(long hoursToAdd) //在原有时间基础上加指定小时数
boolean isBefore(LocalTime other) //判断 调用isBefore方法的LocalTime对象中封装的时间 是否在 传入的时间的前面
//在前面,返回true,否则返回false
public class LocalTimeDemo01 {
public static void main(String[] args) {
//method01();
//method02();
LocalTime now1 = LocalTime.of(11, 20, 30);
System.out.println(now1.plusHours(3));
LocalTime now2 = LocalTime.of(13, 11, 33);
System.out.println(now1.isBefore(now2));
System.out.println(now2.isBefore(now2));
}
private static void method02() {
System.out.println(LocalTime.of(12, 20, 30));
System.out.println(LocalTime.now().withHour(11).withMinute(30).withSecond(20));
System.out.println(LocalTime.now().withHour(13).withNano(0));
}
private static void method01() {
LocalTime now = LocalTime.now();
System.out.println(now); //20:19:03.496257300
System.out.println(now.getHour());
System.out.println(now.getMinute());
System.out.println(now.getSecond());
System.out.println(now.getNano());
}
}
5.LocalDateTime:
LocalDateTime
封装的是年月日时分秒,毫秒,纳秒
2007-12-03T10:15:30。
1.获取功能
static LocalDateTime now()
int getYear()
Month getMonth()
int getDayOfMonth()
int getHour() //获取小时
int getMinute() //获取分钟
int getSecond() //获取秒
int getNano() //获取纳秒
2.设置功能
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)
LocalDateTime withHour(int hour)
LocalDateTime withMinute(int minute)
3.判断功能
public boolean isEqual(ChronoLocalDateTime<?> other) //判断两个日期是否相等
public class LocalDateTimeDemo01 {
public static void main(String[] args) {
//method01();
//method02();
LocalDateTime now1 = LocalDateTime.of(2013, 11, 12, 14, 20);
LocalDateTime now2 = LocalDateTime.of(2014, 11, 12, 14, 20);
System.out.println(now1.isEqual(now1));
System.out.println(now1.isEqual(now2));
}
private static void method02() {
LocalDateTime dateTime = LocalDateTime.of(2013, 11, 12, 14, 20);
System.out.println(dateTime);
System.out.println(LocalDateTime.now().withHour(15).withMinute(33));
}
private static void method01() {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);//2023-11-02T21:59:26.37049
System.out.println(now.getYear());
System.out.println(now.getMonth());
System.out.println(now.getDayOfMonth());
System.out.println(now.getHour());
System.out.println(now.getMinute());
System.out.println(now.getSecond());
}
}
6.日期字符串与日期对象的转换
JDK1.8之前日期对象和日期字符串转换
SimpleDateFormat 构造函数
SimpleDateFormat(String pattern)
继承的方法
String format(Date date) //将一个日期对象格式化为日期字符串
Date parse(String source) //将一个日期字符串解析为为日期对象
JDK1.8 对日期对象和日期字符串相互转换
1.将日期对象转换为日期字符串
DateTimeFormatter类
static DateTimeFormatter ofPattern(String pattern)
String format(TemporalAccessor temporal)
public class ConverDateDemo01 {
public static void main(String[] args) throws ParseException {
//method01();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date parseDate = sdf.parse("2013-12-11");//将传入的日期解析为日期对象
// Date parseDate = sdf.parse("2013/12/11");//传入的日期字符串需要与日期格式对应,否则无法解析
System.out.println(parseDate.getTime());
}
private static void method01() {
Date date = new Date();
System.out.println(date);//Thu Nov 02 22:16:26 CST 2023
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//指定一个格式
String formatStr = sdf.format(date);//把传入的日期对象转换为指定格式的字符串
System.out.println(formatStr);//2023-11-02 22:31:17
}
}
2.将日期字符串转换为日期对象
LocalDate LocalTime LocalDateTime中都含有一下两个方法
static LocalDateTime parse(CharSequence text).//将一个日期字符串按一个默认格式解析为日期对象
static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) //可以按照指定格式解析以恶搞日期字符串
public class ConvertDateDemo02 {
public static void main(String[] args) {
//method01();
//method02();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse("2013/12/13 12:10:11", df);
System.out.println(parse.getYear());
System.out.println(parse.getHour());
}
private static void method02() {
LocalDate now = LocalDate.now();
System.out.println(now);//2023-11-02
LocalDate parse = LocalDate.parse("2012-10-12");
System.out.println(parse.getYear());
/* LocalDate parse2 = LocalDate.parse("2013/11/11");//Text '2013/11/11' could not be parsed
System.out.println(parse2);*/
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate parse2 = LocalDate.parse("2012/12/13", df);
System.out.println(parse2.getDayOfMonth());
}
private static void method01() {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);//2023-11-02T22:54:38.160458100
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(now));
}
}