使用Hibernate轻松解决java中date与datetime类型不兼容的问题
2008-01-29 04:25
1011 查看
今天在没事,跟着尚学堂写一个论坛的程序。在写入时间时,发现了问题。在建立MySQL表使,发帖时间用的datetime类型。并且用系统函数now()来实现。在java实体类中用的java.util.date类型。发现这是犯愁了,众所周至,java中时间类型一直是让人头疼的一个问题。
硬着头皮上,终于发现原来Hibernate已经帮我们解决了问题。
MySQL简表语句如下:
create database bbs;
use bbs;
create table article
(
id int primary key auto_increment,
pid int,
rootid int,
title varchar(255),
cont text,
pdate datetime,
isleaf int #1-not leaf 0-leaf
);
insert into article values (null, 0, 1, '蚂蚁大战大象', '蚂蚁大战大象', now(), 1);
insert into article values (null, 1, 1, '大象被打趴下了', '大象被打趴下了',now(), 1);
insert into article values (null, 2, 1, '蚂蚁也不好过','蚂蚁也不好过', now(), 0);
insert into article values (null, 2, 1, '瞎说', '瞎说', now(), 1);
insert into article values (null, 4, 1, '没有瞎说', '没有瞎说', now(), 0);
insert into article values (null, 1, 1, '怎么可能', '怎么可能', now(), 1);
insert into article values (null, 6, 1, '怎么没有可能', '怎么没有可能', now(), 0);
insert into article values (null, 6, 1, '可能性是很大的', '可能性是很大的', now(), 0);
insert into article values (null, 2, 1, '大象进医院了', '大象进医院了', now(), 1);
insert into article values (null, 9, 1, '护士是蚂蚁', '护士是蚂蚁', now(), 0);
实体类如下:
package database;
import java.util.Date;
/**
* Article entity.
*
* @author smartcat86
*/
public class Article implements java.io.Serializable {
// Fields
private Integer id;
private Integer pid;
private Integer rootid;
private String title;
private String cont;
private Date pdate;
private Integer isleaf;
// Constructors
/** default constructor */
public Article() {
}
/** full constructor */
public Article(Integer pid, Integer rootid, String title, String cont,
Date pdate, Integer isleaf) {
this.pid = pid;
this.rootid = rootid;
this.title = title;
this.cont = cont;
this.pdate = pdate;
this.isleaf = isleaf;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPid() {
return this.pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public Integer getRootid() {
return this.rootid;
}
public void setRootid(Integer rootid) {
this.rootid = rootid;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCont() {
return this.cont;
}
public void setCont(String cont) {
this.cont = cont;
}
public Date getPdate() {
return this.pdate;
}
public void setPdate(Date pdate) {
this.pdate = pdate;
}
public Integer getIsleaf() {
return this.isleaf;
}
public void setIsleaf(Integer isleaf) {
this.isleaf = isleaf;
}
}
测试类
package database;
public class Test ...{
public static void main(String[] args)...{
DBOperate dbo = new DBOperate(HibernateSessionFactory.getSession());//数据库操作类
Article a = new Article();
a.setId(null);//Hibernate中使native使其自动增加
a.setPid(pid);
a.setRootid(rootId);
a.setTitle(title);
a.setCont(cont);
a.setPdate(new Date());
a.setIsleaf(0);
dbo.insert(a);
}
}
后经过测试,插入时间为:2008-01-029 04:04:36.0,和使用 new java.text.SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new java.util.date())得出的结果一样。
当然从数据库读出来了可以使用SimpleDateFormat类来实现。
硬着头皮上,终于发现原来Hibernate已经帮我们解决了问题。
MySQL简表语句如下:
create database bbs;
use bbs;
create table article
(
id int primary key auto_increment,
pid int,
rootid int,
title varchar(255),
cont text,
pdate datetime,
isleaf int #1-not leaf 0-leaf
);
insert into article values (null, 0, 1, '蚂蚁大战大象', '蚂蚁大战大象', now(), 1);
insert into article values (null, 1, 1, '大象被打趴下了', '大象被打趴下了',now(), 1);
insert into article values (null, 2, 1, '蚂蚁也不好过','蚂蚁也不好过', now(), 0);
insert into article values (null, 2, 1, '瞎说', '瞎说', now(), 1);
insert into article values (null, 4, 1, '没有瞎说', '没有瞎说', now(), 0);
insert into article values (null, 1, 1, '怎么可能', '怎么可能', now(), 1);
insert into article values (null, 6, 1, '怎么没有可能', '怎么没有可能', now(), 0);
insert into article values (null, 6, 1, '可能性是很大的', '可能性是很大的', now(), 0);
insert into article values (null, 2, 1, '大象进医院了', '大象进医院了', now(), 1);
insert into article values (null, 9, 1, '护士是蚂蚁', '护士是蚂蚁', now(), 0);
实体类如下:
package database;
import java.util.Date;
/**
* Article entity.
*
* @author smartcat86
*/
public class Article implements java.io.Serializable {
// Fields
private Integer id;
private Integer pid;
private Integer rootid;
private String title;
private String cont;
private Date pdate;
private Integer isleaf;
// Constructors
/** default constructor */
public Article() {
}
/** full constructor */
public Article(Integer pid, Integer rootid, String title, String cont,
Date pdate, Integer isleaf) {
this.pid = pid;
this.rootid = rootid;
this.title = title;
this.cont = cont;
this.pdate = pdate;
this.isleaf = isleaf;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPid() {
return this.pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public Integer getRootid() {
return this.rootid;
}
public void setRootid(Integer rootid) {
this.rootid = rootid;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCont() {
return this.cont;
}
public void setCont(String cont) {
this.cont = cont;
}
public Date getPdate() {
return this.pdate;
}
public void setPdate(Date pdate) {
this.pdate = pdate;
}
public Integer getIsleaf() {
return this.isleaf;
}
public void setIsleaf(Integer isleaf) {
this.isleaf = isleaf;
}
}
测试类
package database;
public class Test ...{
public static void main(String[] args)...{
DBOperate dbo = new DBOperate(HibernateSessionFactory.getSession());//数据库操作类
Article a = new Article();
a.setId(null);//Hibernate中使native使其自动增加
a.setPid(pid);
a.setRootid(rootId);
a.setTitle(title);
a.setCont(cont);
a.setPdate(new Date());
a.setIsleaf(0);
dbo.insert(a);
}
}
后经过测试,插入时间为:2008-01-029 04:04:36.0,和使用 new java.text.SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new java.util.date())得出的结果一样。
当然从数据库读出来了可以使用SimpleDateFormat类来实现。
相关文章推荐
- 使用Hibernate轻松解决java中date与datetime类型不兼容的问题
- 使用Hibernate轻松解决java中date与datetime类型不兼容的问题
- hibernate 中使用oracle数据库 与java Date数据类型 只显示到天 不显示时分秒的问题
- Hibernate datetime 与 JAVA date 不兼容的解决方法
- hibernate对象映射Date数据类型和input datetime使用注意事项
- 使用struts bean:write标签时不能显示Integer,Date等类型的属性问题的解决
- Java中使用Hibernate存储Date类型及Boolean类型到Orcale数据库中的心得
- 解决Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题
- Oracle Date类型字段使用Hibernate插入后没有时分秒的解决
- 解决2008系统中使用java的Date获取时间总是比实际时间差了8个小时的问题
- [原创]DateTime在使用 format Custom Date and Time Format Strings时遇到的问题和解决方法
- 使用Navicat Premium将Oracle数据库中的表和数据迁移到MySQL数据库中,遇到的Date类型出现精度问题及解决方法
- JSON(3):Java的Date类型转换为符合json语法的字符串遇到的问题及其解决办法
- 使用Newtonsoft.json 解决 Asp.Net MVC DateTime类型数据Json格式化问题
- ActionForm使用java.util.Date数据类型解决方法(转)
- 实验过程中遇到的mysql DateTime类型与java Calendar问题与解决过程记录
- hibernate对象映射Date数据类型和input datetime使用注意事项
- 解决mysql 数据库中日期类型00:00:00 的问题 设置xml数据类型:java.util.Date
- Java中使用Hibernate存储Date类型及Boolean类型到Orcale数据库中的心得
- 解决hibernate中不能正常使用blog类型字段上传数据的问题