LoslegenKostenlos loslegen

Validating date ranges

You have validated the numeric ranges of the video game sales quantities. Now you need to validate the ranges of the video game release dates. The dates should fall between 1/1/1985 and 12/31/2024. Sample dates have been extracted from the dataset below. Set up date range checks on this data.

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

The necessary packages such as LocalDate, and DateTimeFormatter have been imported for you.

Diese Übung ist Teil des Kurses

Cleaning Data in Java

Kurs anzeigen

Anleitung zur Übung

  • Check that the release date occurs after startDate.
  • Verify that the release date is before endDate.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

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);
        }
    }
}
Code bearbeiten und ausführen