gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 00:57:26 24 4
gpt4 key购买 nike

我有一个@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;
}

当我使用 VariableManager 插入数据时,它会正确地进入数据库

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

我可以在日志中看到这一点

 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

在数据库中mysql> 从变量中选择 *;

+----+------+------------+-------+--------------------------+---------------------+------------+------+
| 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)

但是当我使用 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"
}
]

这里发生了什么?我在这里做错了什么?

最佳答案

我认为日期值是正确的,但可能不是您想要的格式。您可以尝试创建自定义日期适配器以确保 JAXB 以正确的格式解码值。看看这个:jaxb unmarshal timestamp

关于java - 如何在 Java 中将 TemporalType.TIMESTAMP 转换为 Date()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14328255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com