存储日期

@Entity
@Table(name="test")
public class Test{
    private String id;
    private Date createTime;

    @Id
    @Column(name = "ID_", unique = true, nullable = false)
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    @Column(name="CreateTime_")
    @Temporal(value=TemporalType.TIMESTAMP)  //指明数据库用TIMESTAMP  
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

}

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
test.setCreateTime(sdf.parse(date));
//如果日期类型是Timestamp
//Timestamp ts = Timestamp.valueOf(date);
//test.setCreateTime(ts)
//或者直接使用test.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));

你可能感兴趣的:(日期)