Java中的日期时间 [英] Datetime in java

查看:75
本文介绍了Java中的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将JDBC当前的日期和时间插入到Mysql数据库中.

I have a JDBC current date and time insert into Mysql Database.

它将当前日期和固定时间作为2012/05/25 00:00:00提交到Mysql日期类型字段中.日期部分可以很好地工作,但是时间没有任何价值.

It submits the current date and a fixed time into the Mysql date type field as 2012/05/25 00:00:00. The date part works very fine but the time doesn't shows any value.

我要使用以下代码插入值:

I'm inserting the value with the following code:

java.util.Date myDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());
PreparedStatement PStmt = con.prepareStatement(Sql query);
PStmt.setDate(1,sqlDate);

推荐答案

1)在Java类中使用java.util.Date.

1) Use java.util.Date in your java class.

2)在mysql中设置datatye TIMESTAMP.

2) Set datatye TIMESTAMP in mysql.

Date CreatedDate= new Date(System.currentTimeMillis());

对于PreparedStatement:可能是这样的:

For PreparedStatement: It may be like this:

PreparedStatement PStmt = con.prepareStatement(Sql query);
PStmt.setDate(1,CreatedDate);

这篇关于Java中的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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