MySQL日期时间< - > Java日期 [英] MySQL Datetime <-> Java Date

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

问题描述

我使用Hibernate,并且在MySQL数据库中有一个日期时间。
现在我想将一个java.util.Date的日期/时间存储到数据库中。

所以我可以这样写:

  SimpleDateFormat df = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss); 
testObject.setCreationDate(df.parse(2010-06-06 13:20:15));

但是如何设置当前日期/时间?因为这是一个数据字段,它应该存储此对象/表格行的创建日期时间。



最好的问候。

解决方案

  Date date = new java.sql.Date(); 

会以当前日期/时间创建日期对象。


I use Hibernate and I have a datetime in my MySQL database. Now I want to store the date/time which is a java.util.Date into the database.

So I can write:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
testObject.setCreationDate(df.parse("2010-06-06 13:20:15"));

But how can I set the current date/time? Because this is a data field which should store the creation datetime of this object/table row.

Best Regards.

解决方案

Date date = new java.sql.Date();

will create the date object with the current date/time.

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

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