匿名内部类
new Animal() {
@Override
public void eat() {
System.out.println("猫咪舔猫条");
}
}.eat();
Date 类
将时间格式的字符串 转成 date对象
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat.parse("1970-01-01 08:00:00");
date对象 转 时间戳
Date date = new Date();
long time = date.getTime();
System.out.println("time = " + time);
时间戳 转 date对象
Date date = new Date(System.currentTimeMillis() + 60000);
System.out.println("date = " + date);
Local家族
Local家族主要是为了解决线程安全问题(后面讲解),
Local家族的类中的方法基本都是静态方法调用
//创建当前日期对象
//Time是时间, 16:52:05
LocalTime localTime = LocalTime.now();
System.out.println("localTime = " + localTime);
// 2024-08-12
LocalDate localDate = LocalDate.now();
System.out.println("localDate = " + localDate);
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime = " + localDateTime);
Local家族的 转 指定格式时间字符串的方法
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String timeStr = localDateTime.format(timeFormatter);
System.out.println("timeStr = " + timeStr);
指定格式时间字符串 转 Local家族
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse("2024-08-12 16:58:41", timeFormatter);
System.out.println("localDateTime = " + localDateTime);
immutable(不可变)对象 所有的不可变对象, 绝对的代码安全(线程安全)
LocalDateTime now = LocalDateTime.now();
LocalDateTime dayBefore20 = now.minusDays(20);
System.out.println("now = " + now);
System.out.println("dayBefore20 = " + dayBefore20);
字符串拼接 (含StringBuilder)
package day0812;
/**
* 字符串拼接
*/
public class Demo10 {
public static void main(String[] args) {
//java中的String immutable 不可变
StringBuilder sb = new StringBuilder();
sb.append("张三");
sb.append("爱小美");
String sb = "张三";
String sb2 = "爱";
String sb3 = "小美";
//张三爱
//张三爱小美
String finalStr = sb + sb2 + sb3;
System.out.println(sb);
String str = "张三";
for (int i = 0; i < 100000; i++) {
str = str + i;
}
System.out.println(str);
//使用StringBuilder优化
StringBuilder sb = new StringBuilder();
sb.append("张三");
for (int i = 0; i < 10000; i++) {
sb.append(i);
}
System.out.println(sb);
//StringBuffer和StringBuilder的作用一样, 减少拼接过程中的临时字符串的产生, 从而间接的提升程序运行速度
StringBuffer sb = new StringBuffer("张三");
sb.append("爱");
sb.append("小美");
System.out.println(sb);
}
}
class Demo11 {
public static void main(String[] args) {
String str = "张三";
String str2 = "爱";
String str3 = "小美";
String finalStr = str + str2 + str3;
System.out.println("finalStr = " + finalStr);
String finalStr = "zhangsan" + "love" + "xiaomei";
System.out.println("finalStr = " + finalStr);
StringBuilder sb = new StringBuilder();
sb.append("张三")
.append("爱")
.append("小美");
System.out.println(sb);
Student mengXin = new Student();
mengXin.hobby = "孟馨";
mengXin.name = "植物大战僵尸";
System.out.println(mengXin);
}
}
class Student {
public String name;
public String hobby;
@Override
public String toString() {
return hobby + name;
}
}
class Demo12 {
public static void main(String[] args) {
String str = "张三";
for (int i = 0; i < 10; i++) {
StringBuilder sb = new StringBuilder();
sb.append(str).append(i);
str = sb.toString();
}
System.out.println("str = " + str);
}
}