Joda的当前日期时间 – mysql时间戳的时间日期格式

我需要将当前日期和时间导入MySQL数据库字段,其格式为TimeStamp 。 从检查样本数据来看,MySQL TimeStamp数据类型的格式似乎是“yyyy-mm-dd hh:mm:ss”。 我在我的spring hibernate应用程序中使用Joda-Time格式。 如何以基础MySQL TimeStamp格式化字段接受的格式获取当前日期时间?

这是我当前的代码,它不会编译,因为eclipse说.parseDateTime()需要一个字符串参数而不是DateTime参数:

public void setCreated(){ DateTime now = new org.joda.time.DateTime(); DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd hh:mm:ss"); created = fmt.parseDateTime(now); System.out.println("'''''''''''''''''''''' created is: "+created); } 

我试图坚持的实体定义如下:

 @Entity @Table(name = "documents") public class Document { @Id @GeneratedValue @Column(name="id") private Integer id; @ManyToOne @JoinColumn(name = "client_id") private Patient patient; @ManyToOne @JoinColumn(name = "type_id") private DocumentType type; @Column(name="name") private String name; @Column(name="description") private String description; @Column(name="filename") private String filename; @Column(name="content") @Lob private Blob content; @Column(name="content_type") private String contentType; @Column(name = "created") private DateTime created; public Integer getId(){return id;} public void setId(Integer i){id=i;} protected void setPatient(Patient patient) {this.patient = patient;} public Patient getPatient(){return this.patient;} public void setType(DocumentType type) {this.type = type;} public DocumentType getType() {return this.type;} public String getName(){return name;} public void setName(String nm){name=nm;} public String getDescription(){return description;} public void setDescription(String desc){description=desc;} public String getFileName(){return filename;} public void setFileName(String fn){filename=fn;} public Blob getContent(){return content;} public void setContent(Blob ct){content=ct;} public String getContentType(){return contentType;} public void setContentType(String ctype){contentType=ctype;} public void setCreated(){ DateTime now = new org.joda.time.DateTime(); DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd hh:mm:ss"); created = fmt.parseDateTime(now); System.out.println("''''''''''''''''''''''''''' created is: "+created); } public DateTime getCreated() {return this.created;} @Override public String toString() {return this.getName();} public boolean isNew() {return (this.id == null);} } 

如何更改上述内容以便以正确的格式保存数据以插入MySQL TimeStamp字段?


编辑:

与Sotirios的建议相关,这里是我当前pom.xml的相关部分,用于讨论目的:

  1.3 1.1.1 2.3 3.1.0.CR8    org.jadira.usertype usertype.core ${jadira-usertype-core.version}   joda-time joda-time ${jodatime.version}   joda-time joda-time-hibernate ${jodatime-hibernate.version}   joda-time joda-time-jsptags ${jodatime-jsptags.version}   

因为你似乎需要它:

我的实体类

 @Entity @Table(name = "time_fields") public class TimeFields { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) private Long timeId; @Column @Temporal(TemporalType.DATE) private Date date; @Column @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime") private DateTime dateTime; 

与适当的吸气剂和二传手。

客户端代码

 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PersistenceContext.class); SessionFactory sessionFactory = context.getBean(SessionFactory.class); Session session = sessionFactory.openSession(); TimeFields timeFields = new TimeFields(); Date date = new Date(); DateTime dateTime = new DateTime(); System.out.println(date); System.out.println(dateTime); timeFields.setDate(date); timeFields.setDateTime(dateTime); session.beginTransaction(); session.persist(timeFields); session.getTransaction().commit(); System.out.println(timeFields.getTimeId()); System.out.println(timeFields.getDate()); System.out.println(timeFields.getDateTime()); 

这打印

 Tue Dec 17 00:22:35 EST 2013 2013-12-17T00:22:35.843-05:00 3 Tue Dec 17 00:22:35 EST 2013 2013-12-17T00:22:35.843-05:00 

除了joda-time和hibernate,你还需要jadira libs

  org.jadira.usertype usertype.jodatime 2.0.1  

您应该阅读有关Hibernate UserType的更多信息。

日期的字符串表示纯粹供人类使用。 时间戳在内部表示为与时区无关的数值。 由于yyyy-mm-dd hh:mm:ss不包含时区,尝试自己进行转换会让您遇到时区错误。

Hibernate将直接处理java.util.Datejava.sql.Date ,因此您可以将数据类型更改为其中之一,然后执行映射到getter / setter中的joda对象,以便它可以通过呼叫者。

最好是IMO,你可以为Hibernate提供joda对象的映射。 已经为此准备了一个图书馆:

https://github.com/JodaOrg/joda-time-hibernate

或通过maven:

http://mvnrepository.com/artifact/joda-time/joda-time-hibernate

注意:我个人的经验法则是,如果你看到代码操纵日期为字符串,它就会被破坏……不是任何科学指标,但我发现它远远好于50/50。 🙂

编辑:文档在这里: http : //www.joda.org/joda-time-hibernate/userguide.html

使用类路径上的那个,您应该能够注释您的列,如:

 @Column(name = "created") @Type(type="org.joda.time.contrib.hibernate.PersistentDateTime") private DateTime created;