旧的日期时间的缺点:
-
设计比较乱: Date日期在java.util和java.sql也有,而且它的时间格式转换类在java.text包。
-
线程不安全:
新增加了哪些类?
LocalDate: 表示日期类。yyyy-MM-dd
LocalTime: 表示时间类。 HH:mm:ss
LocalDateTime: 表示日期时间类 yyyy-MM-dd t HH:mm:ss sss
DatetimeFormatter:日期时间格式转换类。
Instant: 时间戳类。
Duration: 用于计算两个日期
package demo10;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
LocalDate now = LocalDate.now(); //获取当前日期
LocalDate date = LocalDate.of(2022, 8, 23);//指定日期
LocalTime now1 = LocalTime.now();//当前时间
LocalTime of = LocalTime.of(17, 30, 20, 600);
LocalDateTime now2 = LocalDateTime.now();//获取当前日期时间
LocalDateTime now3 = LocalDateTime.of(2022,6,20,17,45,20);
Duration between = Duration.between(now2, now3);
System.out.println(between.toHours());
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate parse = LocalDate.parse("1999-12-12", dateTimeFormatter);//把字符串转换为日期格式
String format = parse.format(dateTimeFormatter);
}
}