挑战
开发一个 Java 程序来计算两个日期之间的天数使用德语符号“dd mm yyyy”指定的日期。该程序应考虑闰年和夏令时。
代码
符合以下指定要求的综合代码:
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Scanner; public class DaysBetweenDates { public static void main(String[] args) { // Create a formatter for the German date format DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MM yyyy"); // Get the first and second dates from the user Scanner scanner = new Scanner(System.in); System.out.print("Enter the first date (dd MM yyyy): "); String firstDateInput = scanner.nextLine(); System.out.print("Enter the second date (dd MM yyyy): "); String secondDateInput = scanner.nextLine(); // Parse the dates into LocalDate objects LocalDate firstDate = LocalDate.parse(firstDateInput, formatter); LocalDate secondDate = LocalDate.parse(secondDateInput, formatter); // Calculate the number of days between the dates long daysBetween = ChronoUnit.DAYS.between(firstDate, secondDate); // Print the result System.out.println("Days between the dates: " + Math.abs(daysBetween)); } }
说明
以上是Java 中使用德语日期格式的两个日期之间有多少天?的详细内容。更多信息请关注PHP中文网其他相关文章!