java8新特性之-----
Date Time API:Date Time API 强化了Java对日期和时间的处理,它更容易使用、更安全,同时提供了更多的功能,如时区支持、日期时间计算等。
以前,Java的日期时间处理非常繁琐,而新的API使日期时间操作变得更加直观和易用。例如,你可以使用LocalDate来表示日期,LocalTime来表示时间,而不再需要混合使用Date和Calendar。
代码演示:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class dateTimeApi {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println("Formatted Date: " + localDate.format(dateFormatter));
System.out.println("Formatted Time: " + localTime.format(timeFormatter));
System.out.println("Formatted Date Time: " + localDateTime.format(dateTimeFormatter));
}
}