ComeçarComece de graça

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

Este exercício faz parte do curso

Cleaning Data in Java

Ver curso

Instruções do exercício

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

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

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);
        }
    }
}
Editar e executar o código