简体   繁体   English

如何在Java中将TemporalType.TIMESTAMP转换为Date()?

[英]How to convert TemporalType.TIMESTAMP to Date() in Java?

I have an @Entity 我有一个@Entity

@Entity
@Table(name = "variable")
@XmlRootElement
public class Variable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(unique = true, nullable = false)
    private String name;

    @Column
    @Enumerated(EnumType.STRING)
    private VariableType type;

    @Column(nullable = false)
    private String units;

    @Column
    private String description;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_on", nullable = false)
    private Date createdOn;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "retired_on", nullable = true)
    private Date retiredOn;

    @Column(nullable = false)
    private boolean core;
}

When I insert data using VariableManager , it goes correctly inside database 当我使用VariableManager插入数据时,它在数据库中正确进行

variableCrudService.create(new Variable(name, type, units, description, new Date(), core));

and I can see that in logs 我可以在日志中看到

 added variable id=13 name=v4 type=String units=u4 description=created with web service createdOn=2013-01-14 14:43:57.0 retiredOn=null core=true

and in database mysql> select * from variable; 并在数据库mysql> select * from variable;

+----+------+------------+-------+--------------------------+---------------------+------------+------+
| id | name | type       | units | description              | created_on          | retired_on | core |
+----+------+------------+-------+--------------------------+---------------------+------------+------+
|  1 | v1   | Double     | u1    | created via migration    | 2013-01-14 13:15:19 | NULL       |    1 |
|  2 | v2   | String     | u2    | created via migration    | 2013-01-14 13:15:19 | NULL       |    1 |
|  3 | v3   | BigDecimal | u3    | created via migration    | 2013-01-14 13:15:19 | NULL       |    0 |
| 12 | v4   | String     | u4    | created with web service | 2013-01-14 14:41:31 | NULL       |    1 |
+----+------+------------+-------+--------------------------+---------------------+------------+------+
4 rows in set (0.00 sec)  

but when I test this using curl, I get timestamps in weird format 但是当我使用curl测试时,我得到了奇怪格式的时间戳

[
    {
        "core": true, 
        "createdOn": 1358198119000, 
        "description": "created via migration", 
        "id": 1, 
        "name": "v1", 
        "retiredOn": null, 
        "type": "Double", 
        "units": "u1"
    }, 
    {
        "core": true, 
        "createdOn": 1358198119000, 
        "description": "created via migration", 
        "id": 2, 
        "name": "v2", 
        "retiredOn": null, 
        "type": "String", 
        "units": "u2"
    }, 
    {
        "core": false, 
        "createdOn": 1358198119000, 
        "description": "created via migration", 
        "id": 3, 
        "name": "v3", 
        "retiredOn": null, 
        "type": "BigDecimal", 
        "units": "u3"
    }, 
    {
        "core": true, 
        "createdOn": 1358203437000, 
        "description": "created with web service", 
        "id": 13, 
        "name": "v4", 
        "retiredOn": null, 
        "type": "String", 
        "units": "u4"
    }
]

What is going around here? 这里到底发生了什么? What is that I am doing wrong here? 我在这做错了什么?

I think the date value is right, but perhaps not in the format you want. 我认为日期值是正确的,但可能不是您想要的格式。 You can try to create a custom date adapter to ensure JAXB unmarshal the value in the right format. 您可以尝试创建自定义日期适配器,以确保JAXB以正确的格式解组值。 Take a look at this: jaxb unmarshal timestamp 看看这个: jaxb unmarshal时间戳

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM