mysql时间戳的Joda-Time日期格式中的当前datetime


问题内容

我需要将当前日期和时间导入格式为TimeStamp的MySQL数据库字段中。从检查样本数据来看,MySQL
TimeStamp数据类型的格式似乎是“ yyyy-mm-dd hh:mm:ss”。我在springhibernate应用程序中使用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的相关部分,以供讨论:

<properties>
    <jodatime-hibernate.version>1.3</jodatime-hibernate.version>
    <jodatime-jsptags.version>1.1.1</jodatime-jsptags.version>
    <jodatime.version>2.3</jodatime.version>
    <jadira-usertype-core.version>3.1.0.CR8</jadira-usertype-core.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.jadira.usertype</groupId>
        <artifactId>usertype.core</artifactId>
        <version>${jadira-usertype-core.version}</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>${jodatime.version}</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time-hibernate</artifactId>
        <version>${jodatime-hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time-jsptags</artifactId>
        <version>${jodatime-jsptags.version}</version>
    </dependency>
</dependencies>

问题答案:

因为您似乎需要它:

我的实体班

@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库

<dependency>
    <groupId>org.jadira.usertype</groupId>
    <artifactId>usertype.jodatime</artifactId>
    <version>2.0.1</version>
</dependency>

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