How to convert LocalDate to SQL Date Java?

后端 未结 3 1340
我寻月下人不归
我寻月下人不归 2020-11-28 06:44

How do I convert a LocalDate to a java.sql.Date?

Attempt:

Record r = new Record();
LocalDate date = new Date(1967, 06, 22);
r.setDateOfB         


        
3条回答
  •  忘掉有多难
    2020-11-28 07:06

    The answer is really simple;

    import java.sql.Date;
    ...
    LocalDate locald = LocalDate.of(1967, 06, 22);
    Date date = Date.valueOf(locald); // Magic happens here!
    r.setDateOfBirth(date);
    

    If you would want to convert it the other way around, you do it like this:

    Date date = r.getDate();
    LocalDate localD = date.toLocalDate();
    

    r is the record you're using in JOOQ and .getDate() is the method for getting the date out of your record; let's say you have a date column called date_of_birth, then your get method should be called getDateOfBirth().

提交回复
热议问题