【问题标题】:Java Time insertion in Mysql tableMysql表中的Java时间插入
【发布时间】:2019-11-27 16:03:34
【问题描述】:

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

这是我的代码的 sn-p,它从列表中提取时间(第三个值)

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数据库表定义和查询结果...

select * from nmon_interval order by 4 desc;

【问题讨论】:

  • 你的日期也是错误的。它应该是2019-11-15。可能会帮助您使用LocalDateLocalTime
  • 伟大的收获!...我什至没有看到...我全神贯注于解决时间问题

标签: java mysql hibernate spring-boot jpa


【解决方案1】:

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

【讨论】:

  • 选择@@GLOBAL.time_zone,@@SESSION.time_zone;返回系统,系统。基于mysql docs...'SYSTEM',表示服务器时区与系统时区一致。
  • 更多信息... 显示全局变量,例如 '%time_zone%'; system_time_zone 设置为 UTC
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多