java - Mysql表中的Java时间插入
问题描述
我有一个包含以下数据示例的简单字符串列表。我能够将数据从我的列表传递到具有类型字段的实体类,Date
我还在
SimpleDateFormat sTime = new SimpleDateFormat("HH:mm") this.itime = sTime.parse(itime);
将其插入数据库之前执行字符串到日期的转换。
问题:插入表中的时间值不正确!。我哪里错了?
希望我已经为您提供了足够的信息来制定答案。
样本数据
ZZZZ,T0001,00:15:39,15-NOV-2019
ZZZZ,T0002,00:30:39,15-NOV-2019
ZZZZ,T0003,00:45:39,15-NOV-2019
...
...
...
这是我的代码片段,它从列表中提取时间(第三个值)
map.entrySet().stream().forEach(v -> {
String[] items = v.split(",");
if (items[0].equals("ZZZZ")) {
slice[0] = items[1];
mTime[0] = items[2];
mDate[0] = items[3];
try {
// load data into nmon_interval table
IntervalData i = new IntervalData(hostId,slice[0],mDate[0],mTime[0]);
System.out.println("mTime[0] :"+mTime[0]+ ", TSLICE: "+ slice[0] );
System.out.println("ITIME: "+i.getItime()+", TSLICE: "+i.getTslice());
r.save(i);
这是 IntervalData 类...
@Entity
public class IntervalData {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "interval_generator")
@SequenceGenerator(name = "interval_generator", sequenceName = "interval_seq")
private Long id;
private String tslice;
@Temporal(TemporalType.DATE)
private Date idate;
@Temporal(TemporalType.TIME)
private Date itime;
@ManyToOne
//@JoinColumn(name = "id" , nullable = false)
private Host host;
@Transient
SimpleDateFormat sTime = new SimpleDateFormat("HH:mm");
@Transient
SimpleDateFormat sDate = new SimpleDateFormat("DD-MMM-yyyy");
public IntervalData() {
super();
}
public IntervalData(Long hostId, String tslice, String idate, String itime) throws ParseException {
this.tslice = tslice;
this.itime = sTime.parse(itime);
this.idate = sDate.parse(idate);
this.host = new Host(hostId,"");
}
日志输出...
mTime[0] :00:15:39, TSLICE: T0001
ITIME: Thu Jan 01 00:15:00 PST 1970, TSLICE: T0001
Hibernate: select next_val as id_val from interval_seq for update
Hibernate: update interval_seq set next_val= ? where next_val=?
Hibernate: select next_val as id_val from interval_seq for update
Hibernate: update interval_seq set next_val= ? where next_val=?
Hibernate: insert into nmon_interval (host_id, idate, itime, tslice, id) values (?, ?, ?, ?, ?)
Hibernate: select intervalda0_.id as id1_3_, intervalda0_.host_id as host_id5_3_, intervalda0_.idate as idate2_3_, intervalda0_.itime as itime3_3_, intervalda0_.tslice as tslice4_3_ from nmon_interval intervalda0_ left outer join nmon_host host1_ on intervalda0_.host_id=host1_.id where host1_.id=? and intervalda0_.idate=? and intervalda0_.itime=?
mTime[0] :00:30:39, TSLICE: T0002
ITIME: Thu Jan 01 00:30:00 PST 1970, TSLICE: T0002
select * from nmon_interval order by 4 desc;
解决方案
很可能是时区问题。您需要检查您的 MySQL 时区设置并将它们设置为 UTC
推荐阅读
- flow-project - 如何在 tutorial01 中将训练有素的 RL 控制车辆添加到系统中?
- mysql - 插入触发器后的Mysql给出错误
- python - 使用python3计算txt文件特定部分的单词出现次数
- node.js - heroku 和 node.js 没有找到带有请求模块的 php 文件
- python - 如何在 Python 中仅读取/选择输入的一部分?
- python - 如何使用 Python 在 Windows 中处理带有空格的长路径
- javascript - Browserify npm 库
- python - python .timestamp() 方法计算不同时区的错误(或相同)unix 时间
- android - 是否可以在调试模式下运行 Crashlytics?
- flutter - 哪个是更快和性能更好的 Provider、bloc 模式还是 flutter_redux?