0

I am creating a web app using Java (Javalin and Maven) for university.

We have to make a website for movie bookings and I am struggling to find out how to properly convert dates so that they are readable by SQL and Java.

I have to store date values in a database and to this point I have just been storing them as a string but I want it to have a more specific date meaning. It is built using the MVC model.

This is the code in my sessions model.

    public void setSessionDate(Date sessionDate) {
        java.sql.Date date = new java.sql.Date(sessionDate.getTime() );
        this.sessionDate = sessionDate;

That is the code in my SessionsDao file.

    stm.setDate(3, new java.sql.Date(sessions.getSessionDate().getTime()));

And finally this is the code in my SQL create field.

   sessionDate DATE not null,

When I try to create a new movie session this is the error the console prints.

[qtp198903030-30] WARN io.javalin.Javalin - Uncaught exception
io.javalin.core.validation.MissingConverterException: Can't convert to Date. Register a converter using JavalinValidation#register.
    at io.javalin.core.validation.Validator$Companion.create(Validator.kt:35)

I am unsure how to convert the date properties correctly.

Any help or other methods on how to proceed would be greatly helpful!

rafipin
  • 3
  • 3
  • 3
    Both of those terrible date-time classes were supplanted years ago by the modern *java,time* classes with the adoption of JSR 310. – Basil Bourque May 28 '20 at 06:40
  • Like Basil said, you can look for LocalDate, LocalTime, LocalDateTime and Instant classes from java.time package. It will make your task easier. The new API has more features than the old java.sql. You can find some insights here https://stackoverflow.com/questions/28730136/should-i-use-java-util-date-or-switch-to-java-time-localdate – lubrum Jun 12 '20 at 14:27

0 Answers0