java版本12计算2000年1月到2023年6月相差几年
JDK12版本
import java.time.YearMonth; import java.time.temporal.ChronoUnit; public class YearsBetweenDates { public static void main(String[] args) { YearMonth startYearMonth = YearMonth.of(2000, 1); YearMonth endYearMonth = YearMonth.of(2023, 6); long yearsBetween = ChronoUnit.YEARS.between(startYearMonth, endYearMonth); System.out.println("Years between 2000-01 and 2023-06: " + yearsBetween); } }
JDK之前的版本
import java.time.YearMonth; import java.time.temporal.ChronoUnit; public class YearsBetweenDates { public static void main(String[] args) { YearMonth startYearMonth = YearMonth.of(2000, 1); YearMonth endYearMonth = YearMonth.of(2023, 6); long yearsBetween = ChronoUnit.YEARS.between(startYearMonth, endYearMonth); System.out.println("Years between 2000-01 and 2023-06: " + yearsBetween); } }