简体   繁体   English

将日期插入日期时间类型列

[英]Insert date to datetime type column

I want to insert a date to a mysql database table. 我想在mysql数据库表中插入日期。 The datatype of the related column in mysql database table is datetime. mysql数据库表中相关列的数据类型是datetime。 I used the following code. 我使用了以下代码。

String date="2013.05.15";
Timestamp timestamp = new Timestamp(Long.parseLong(date));

when I am inserting timestamp value, it throws numberformat exception. 当我插入时间戳值时,它会抛出numberformat异常。 Anybody please tell me how to insert the value to the database. 有人请告诉我如何将值插入数据库。 Thank You . 谢谢 。

You're not parsing the date correctly. 您没有正确解析日期。 Actually you're trying to 其实你正在尝试
parse it as long which is a number, not a date. 解析它是一个数字,而不是一个日期。 You can use this code. 您可以使用此代码。

    String date="2013.05.15";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
    Date dt = sdf.parse(date);
    Timestamp timestamp = new Timestamp(dt.getTime());

Either use NOW() function of mysql or use timestamp to insert into mysql. 要么使用mysql的NOW()函数,要么使用timestamp插入mysql。

and make sure datatype of mysql field datetime 并确保mysql字段datetime数据类型

Timestamp timestamp = new Timestamp(new Date().getTime());
Timestamp dateForDb= timestamp;

insert dateForDb into your database. dateForDb插入数据库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将日期从表单插入到数据库中类型为Datetime的列中 - How to insert a date from form to a column whose type is Datetime in database 如何在日期时间列中插入日期(SQLServer) - How to insert a Date in a column datetime (sqlserver) 如何在数据库的类型日期列中插入值? - How to insert a value into a type date column of database? 休眠-使mysql中的列类型只有日期而不是datetime - hibernate - making type of column in mysql only date and not datetime 在mysql数据库的datetime列中插入日期和时间时,它将插入日期,但时间为00-00-00 - when inserting date and time in datetime column in mysql database it will insert date but time is 00-00-00 在Hibernate中创建列类型Datetime - Create column type Datetime in Hibernate 如何使用Spring Data Cassandra将java.util.Date值插入Cassandra日期类型列中? - How to insert java.util.Date values into Cassandra date type column using Spring Data Cassandra? Liquibase将列类型从Date更改为DateTime而不删除包含的值 - Liquibase change column type from Date to DateTime without deleting contained values Hibernate将默认日期时间插入不为空的列 - Hibernate insert default datetime to a not null column 当我在 Jooq 中插入日期时,出现此错误:column creation_date is of type timestamp with time zone but expression is of type character varying - When I insert a date in Jooq, I get this error: column creation_date is of type timestamp with time zone but expression is of type character varying
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM