挑戰
開發一個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中文網其他相關文章!