How to convert java.util.Date to java.sql.Date?

后端 未结 18 2680
梦如初夏
梦如初夏 2020-11-22 01:44

I am trying to use a java.util.Date as input and then creating a query with it - so I need a java.sql.Date.

I was surprised to find that it couldn\'t do the conver

18条回答
  •  生来不讨喜
    2020-11-22 02:41

    If you are usgin Mysql a date column can be passed a String representation of this date

    so i using the DateFormatter Class to format it and then set it as a String in the sql statement or prepared statement

    here is the code illustration:

    private String converUtilDateToSqlDate(java.util.Date utilDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sqlDate = sdf.format(utilDate);
        return sqlDate;
    }
    

    String date = converUtilDateToSqlDate(otherTransaction.getTransDate());

    //then pass this date in you sql statement

提交回复
热议问题