How to map sql DATE to LocalDate

前端 未结 2 2069
深忆病人
深忆病人 2021-01-12 20:21

I want to store a LocalDate in a DATE column and retrieve it unchanged. Both DATE and LocalDate are \"local\" types by de

2条回答
  •  耶瑟儿~
    2021-01-12 21:25

    I just tried the following modification to your retrieve method and it worked for me:

    The H2 documentation for the DATE Type says that it is

    The date data type. The format is yyyy-MM-dd.

    So, instead of your ...

    java.sql.Date retrieved = (java.sql.Date) rs.getObject("born");
    return retrieved.toLocalDate();
    

    ... I just used ...

    return LocalDate.parse(rs.getString("born"));
    

    ... and my code produced

    Inserted:  2015-05-20
    Retrieved: 2015-05-20
    Retrieved: 2015-05-20
    Retrieved: 2015-05-20
    

提交回复
热议问题