3

I have been looking and trying solutions provided on the web (and SO) for the last hour on how to use the Date type in Java but I can't seem to get it working. I am using NetBeans 11.2 and I am not used to Java and it is giving me a hard time (no pun intended). It seems that LocalDateTime, Date and Time are deprecated and that I should be using java.time.

To be honest, I don't know what to do anymore. I am trying to build a query with inputs value to save in mySQL database.

The source of the Date is from <input type="date">

SignIn.java (servlet) :

String birthDate = request.getParameter("data_birthdate");
        
UserDto userDto = null;
UserDao userDao = new UserDao(); 
        
try 
{ 
// Tried this 
    userDto = userDao.CreateUser(LocalDateTime.parse(birthDate));
// Tried that
    userDto = userDao.CreateUser(Date.parse(birthDate));
// Tried this 
    userDto = userDao.CreateUser(Time.parse(birthDate));
}

userDao.java (Dao) :

public void CreateUser(Date dateBirth) throws SQLException {
try {
        connect = db.getConnect();
        ps = connect.prepareStatement(SQL_CreateUser);
        ps.setDate(1, dateBirth);
        ps.executeUpdate();
    }
}
pensum
  • 980
  • 1
  • 11
  • 25
  • `PreparedStatement.setDate()` expects `java.sql.Date` instead of `java.util.Date`. The former is the subclass of the latter, so passing in a `java.util.Date` wouldn't work. – Jai Nov 08 '19 at 03:20
  • 1
    Re _"It seems that LocalDateTime, Date and Time are depreciated..."_, `LocalDateTime` isn't deprecated, and there's nothing wrong with using it, but steer clear of `Date` and `Time` if possible. At the risk of confusing you further, the SO question [Should I use java.util.Date or switch to java.time.LocalDate](https://stackoverflow.com/questions/28730136/should-i-use-java-util-date-or-switch-to-java-time-localdate) has some good answers which provide a lot of detail on this issue. Also see [Migrating to the New Java 8 Date Time API](https://www.baeldung.com/migrating-to-java-8-date-time-api). – skomisa Nov 08 '19 at 04:35
  • Related: [Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2) – Ole V.V. Nov 08 '19 at 06:36

1 Answers1

1

You may use LocalDateTime along with PreparedStatement#setTimestamp(). Here is roughly how you would do that:

String birthDate = request.getParameter("data_birthdate");
// assuming your text birthdates look like 1980-12-30 00:30:05
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dt = LocalDateTime.parse(birthDate, formatter);

try {
    connect = db.getConnect();
    ps = connect.prepareStatement(SQL_CreateUser);
    ps.setTimestamp(3, Timestamp.valueOf(dt));
}
catch (Exception e) {
    // handle exception
}

Note carefully the format mask being used in the call to DateTimeFormatter#ofPattern. You need to replace that with whatever mask actually fits your string dates.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    (1) If the datatype in SQL is `timestamp with time zone` (recommended), use `offsetDateTime` and/or `Instant` in Java. (2) Even if not, avoid `Timestamp` and use `ps.setObject(3, dt);`. – Ole V.V. Nov 08 '19 at 06:35