gpt4 book ai didi

Java时间在Mysql表中插入

转载 作者:行者123 更新时间:2023-11-29 15:26:13 26 4
gpt4 key购买 nike

我有一个简单的字符串列表,其中包含以下数据示例。我能够将列表中的数据传递到具有 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

Mysql数据库表定义和查询结果... enter image description here

select * from nmon_interval order by 4 desc;

enter image description here enter image description here

最佳答案

很可能是时区问题。您需要检查您的 MySQL 时区设置并将其设置为 UTC

关于Java时间在Mysql表中插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59074163/

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