Java时间在Mysql表中插入

标签 java mysql hibernate spring-boot jpa

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

相关文章:

mySQL 连接来自不同表的数据

java - 没有为 java hibernate 中的实体指定标识符

java - 我使用 Math.floor() 的 Java 数学表达式结果为 0 而不是预期的数字,即使我的论文计算表明它应该不是

java - 使用jackson-mapper解析json文件错误

Mysql - 查找未使用的表和列

MYSQL 如何 "Join"两次查询

java - 在 JPA 中使用继承来实现数据类型目的

java - 为什么我不能将 Collection<GenericFoo> 转换为 Collection<GenericFoo<?>>

java - 如何将 Java 对象从一个类传递到另一个类

java - hibernate 映射 "rules"