开始使用免费开始使用

验证日期范围

您已经验证了电子游戏销量的数值范围。现在需要验证电子游戏发行日期的范围。日期应介于 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

所需的包,例如 LocalDateDateTimeFormatter,已为您导入。

本练习是课程的一部分

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);
        }
    }
}
编辑并运行代码