T4under4
T4under4

Reputation: 155

I can't pass date variable to the constructor - parsing impossible

In a servlet that is used to add new record to a data base table I need to pass Date value to constructor however if I parse in constructor argument I get the following error

"unhandled exception:java.text.ParseException"

When I try to do parsing with try catch before I put the value in constructor then the value is being seen as not initialized.

This is the entity class

@Entity
@Table(name = "doctors")
@Data
public class Doctors implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Id")
    private Integer id;
    @Column(name = "Name")
    private String name;
    @Column(name = "Surname")
    private String surname;
    @Column(name = "Title")
    private String title;
    @Column(name = "LicenseNumber")
    private String licenseNumber;
    @Column(name = "Phone")
    private String phone;
    @Column(name = "Email")
    private String email;
    @Column(name = "Nationality")
    private String nationality;
    @Column(name = "Speciality")
    private String speciality;
    @Column(name = "DateofBirth")
    private LocalDate dateOfBirth;
    @Column(name = "IsaTeacher")
    private Boolean isATeacher;

    public Doctors() {
    }

    public Doctors(Integer id, String name, String surname, String title, String licenseNumber, String phone, String email, String nationality, String speciality, LocalDate dateOfBirth, Boolean isATeacher) {
        this.id = id;
        this.name = name;
        this.surname = surname;
        this.title = title;
        this.licenseNumber = licenseNumber;
        this.phone = phone;
        this.email = email;
        this.nationality = nationality;
        this.speciality = speciality;
        this.dateOfBirth = dateOfBirth;
        this.isATeacher = isATeacher;
    }
}

This is the value I get from http request

String dateOfBirth = req.getParameter("dateOfBirth");

These are my attempts to make it work. This one gives "unhandled exception:java.text.ParseException"

Doctors doctor = new Doctors(name, surname, title, licenseNumber, phone, email, nationality, speciality, new SimpleDateFormat("yyyy-MM-dd").parse(dateOfBirth), Boolean.valueOf(isATeacher));

and this one "variable not initialized"

DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date1;
        try {
            date1 = simpleDateFormat.parse(dateOfBirth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Doctors doctor = new Doctors(name, surname, title, licenseNumber, phone, email, nationality, speciality, date1, Boolean.valueOf(isATeacher));

I really don't know what to do. I have to handle the exception in order to parse String to date, but then the variable is not seen outside try block.

Upvotes: 0

Views: 461

Answers (1)

Anonymous
Anonymous

Reputation: 86276

Your Doctors constructor needs a LocalDate argument, not a Date (which is good because Date is poorly designed and long outdated while LocalDate belongs to java.time, the modern and much nicer Java date and time API).

Extra bonus, parsing a LocalDate throws an unchecked rather than a checked exception in case of parse failure, so the compiler doesn’t mind.

Extra extra bonus, LocalDate parses your format, yyyy-MM-dd, as its default, that is, without any explicit formatter. This is because the format conforms with ISO 8601, the standard.

    Doctors doctor = new Doctors(name, surname, title, licenseNumber, phone,
            email, nationality, speciality, LocalDate.parse(dateOfBirth),
            Boolean.valueOf(isATeacher));

Links

Upvotes: 2

Related Questions