驗證日期範圍
你已經驗證了電玩遊戲銷售數量的數值範圍。現在你需要驗證電玩遊戲的發行日期範圍。日期應介於 1/1/1985 和 12/31/2024 之間。下方從資料集擷取了一些範例日期。請在這些資料上設定日期範圍檢查。
| Game | Platform | Release date | Quantity (millions of units) |
|---|---|---|---|
| Wii Sports | Wii | 11/19/2006 | 41.49 |
| Super Mario Bros. | NES | 11/13/1985 | 29.08 |
| Mario Kart Wii | Wii | 4/10/2008 | 15.85 |
必要的套件,例如 LocalDate 和 DateTimeFormatter,已經為你匯入。
本練習屬於課程
使用 Java 進行資料清理
練習說明
- 檢查發行日期是否在
startDate之後。 - 驗證發行日期是否早於
endDate。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
public class RangeValidation {
public static void main(String[] args) {
List releaseDates = Arrays.asList("11/19/2006", "11/13/1985", "4/10/2008");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/yyyy");
LocalDate startDate = LocalDate.of(1985, 1, 1);
LocalDate endDate = LocalDate.of(2024, 12, 31);
for (String dateStr : releaseDates) {
LocalDate releaseDate = LocalDate.parse(dateStr, formatter);
// Check that the release date occurs after startDate
boolean isValid = ____.____(startDate)
// Verify that the release date is before endDate
&& ____.____(endDate);
System.out.println(dateStr + " is valid: " + isValid);
}
}
}