I want the user to enter a birth date, but it has to work if they put it in this pattern dd-MM-yyyy but also if they put it in this pattern d-M-yyyy. I know I have to use integers or something, but I don't know where and how. See my code below:
public class Student {
private int studentnr;
private String voornaam;
private String achternaam;
private LocalDate geboortedatum;
private Adres adres;
/**
* Default Constructor maakt een student aan
* @param studentnr
* Studentnr wordt meegegeven in main
* @param voornaam
* Voornaam wordt meegegeven in main
* @param achternaam
* Achternaam wordt meegegeven in main
* @param geboortedatum
* Geboortedatum wordt meegegeven in main
* @param adres
* Adres wordt meegegeven in main
*/
public Student(int studentnr, String voornaam, String achternaam, String geboortedatum, Adres adres) {
this.studentnr = studentnr;
this.voornaam = voornaam;
this.achternaam = achternaam;
this.geboortedatum = LocalDate.parse(geboortedatum, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
this.adres = adres;
}
d-M-yyyy pattern will work regardless of leading zeros in day and month:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d-M-yyyy");
System.out.println(LocalDate.from(dtf.parse("1-2-2001"))); // 2001-02-01
System.out.println(LocalDate.from(dtf.parse("01-02-2001"))); // 2001-02-01
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With