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

后端 未结 18 2322
梦如初夏
梦如初夏 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:31

    try with this

    public static String toMysqlDateStr(Date date) {
        String dateForMySql = "";
        if (date == null) {
            dateForMySql = null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dateForMySql = sdf.format(date);
        }
    
        return dateForMySql;
    }
    

提交回复
热议问题