JDK8之前的日期时间API
System静态方法、Date类、Calendar类、SimpDateFormat类
缺少相应日期格式化、对象是可变的、有偏移量不好理解
JDK8新的日期时间API
LocalDateTime、LocalDate、LocalTime 、Instant、DateTimeFormatter等
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class LocalDateDemo01 {
public static void main(String[] args) {
//常用的有 LocalDateTime、LocalDate、LocalTime三个类
// 生成当前时间 ,调用静态方法now()
LocalDateTime ltd = LocalDateTime.now();
LocalTime lt = LocalTime.now();
LocalDate ld = LocalDate.now();
System.out.println(ltd);
System.out.println(lt);
System.out.println(ld);
//getXxx get相关方法 获取相关的时间属性
System.out.println("--- get ---------------");
System.out.println(ltd.getDayOfWeek());
System.out.println(ltd.getMonth());
System.out.println(ltd.getMonthValue());
//withXxx 设置相关时间
System.out.println("--- with --------------");
LocalDateTime ltd1 = ltd.withMonth(2);
System.out.println(ltd);
System.out.println(ltd1);
LocalDateTime ltd2 = ltd.withDayOfMonth(3);
System.out.println(ltd);
System.out.println(ltd2);
//plus 增加
System.out.println("--- plus --------------");
LocalDateTime ltd3 = ltd.plusDays(22);
System.out.println(ltd);
System.out.println(ltd3);
// minus
System.out.println("--- minus --------------");
LocalDateTime ltd4 = ltd.minusDays(100);
System.out.println(ltd);
System.out.println(ltd4);
//of 函数生成自定义日期对象
System.out.println("--- of -----------------");
LocalTime oflt = LocalTime.of(22, 30, 55);
LocalDate ofld = LocalDate.of(2022, 7, 20);
LocalDateTime ofltd = LocalDateTime.of(ofld, oflt);
System.out.println(ofltd);
}
}