在Java中,获取年份和时间的API主要是通过java.time包中的类实现的。其中,LocalDate类用来表示日期,LocalTime类用来表示时间,LocalDateTime类用来表示日期和时间。
import java.time.LocalDate;import java.time.LocalTime;import java.time.LocalDateTime;// 获取当前日期LocalDate currentDate = LocalDate.now();System.out.println("Current Date: " + currentDate);// 获取当前时间LocalTime currentTime = LocalTime.now();System.out.println("Current Time: " + currentTime);// 获取当前日期和时间LocalDateTime currentDateTime = LocalDateTime.now();System.out.println("Current Date and Time: " + currentDateTime);// 获取年份int year = currentDateTime.getYear();System.out.println("Year: " + year);通过上述代码,可以获取当前日期、时间、日期和时间,以及从当前日期和时间中提取出年份。这些API提供了便捷的方法来处理日期和时间,并且遵循ISO标准。


