Validating Dates

Learn how to validate date values in the past and in a specific format.

We'll cover the following...

In this lesson, we will show how to validate a date field. Say, we want to store the date when the player won the last title. We will add a field lastWon to the Athlete class. This value must be in the past and must follow a specific pattern (dd-mm-yyyy). Any date value violating these constraints should result in a validation error.

We will create a Date field lastWon and generate getter and setter methods.

public class Athlete {
//...
private Date lastWon;
//...
public Date getLastWon() {
return lastWon;
}
public void setLastWon(Date lastWon) {
this.lastWon = lastWon;
}
}
Adding the lastWon field to Athlete class

We will use @Past and ...

Access this course and 1400+ top-rated courses and projects.