如何将日期变量转换为java.sql.date [英] How to covert date variable to java.sql.date

查看:140
本文介绍了如何将日期变量转换为java.sql.date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试将日期变量插入数据库表时遇到了困难。我的变量叫做:

I am facing difficulties while trying to insert a date variable into a database table. My variable is called:

Date date4;

从具有日历选择器的文本框中读取date4变量值。为了插入日期列,我将字段类型设置为日期:

The date4 variable value is read from a textbox which has a calendar picker. For inserting in a date column, I am setting the field type to date:

preparedstmt.setDate(4,date4);

但是,我在提交表格后收到以下信息:

However, I got the below message after submitting the form:

javax.faces.component.UpdateModelException:java.lang.IllegalArgumentException:无法将类型为java.util.Date的4/1/15 12:00 AM转换为类java.sql.Date

javax.faces.component.UpdateModelException: java.lang.IllegalArgumentException: Cannot convert 4/1/15 12:00 AM of type class java.util.Date to class java.sql.Date

另外,有没有办法以日期格式插入字段(dd-MON-YYYY)?

Also, is there a way to insert the field in the format of date ("dd-MON-YYYY")?

推荐答案

将工具日期转换为sql日期

Convert util date to sql date

    java.sql.Date sqlDate = new java.sql.Date(date4.getTime());

这篇关于如何将日期变量转换为java.sql.date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆