简体   繁体   English

将数据保存到MySql时日期时间值不正确

[英]Incorrect datetime value while saving data to MySql

I faced a strange problem about saving java Date object to MySql db. 我遇到了一个有关将Java Date对象保存到MySql db的奇怪问题。 Here is the error I get: 这是我得到的错误:

Incorrect datetime value: '1970-01-01 02:55:00' for column 'start_time' at row 1

My sql script 我的SQL脚本

CREATE TABLE `schedules` (
            `id` bigint(20) NOT NULL AUTO_INCREMENT,
            `start_time` timestamp NOT NULL,
            `end_time` timestamp NOT NULL,
            PRIMARY KEY (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8

Part of my entity: 我实体的一部分:

 @Column(name = "start_time")
 private Date startTime;

When I try to do something like 当我尝试做类似的事情

String time = "02:55"
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
scheduleEntity.setStartTime(sdf.parse(time));

repository.save(scheduleEntity);

It falls with error. 它落入错误。 But when I use "16:00" string for parsing it works perfectly. 但是,当我使用"16:00"字符串进行解析时,它可以完美地工作。 I use Spring Boot, Spring Data with Hibernate. 我使用Spring Boot,Spring Data和Hibernate。 Can someone help me? 有人能帮我吗?

Apparently, you want to store only a time portion in your table, yet you use timestamp data type, which stores both date and time and has other special features as well. 显然,您只想在表中存储一个时间部分,但是您使用了timestamp数据类型,该数据类型既存储日期和时间,又具有其他特殊功能。

If you want to store time only, then use MySQL's time data type : 如果只想存储时间,则使用MySQL的时间数据类型

MySQL retrieves and displays TIME values in 'HH:MM:SS' format (or 'HHH:MM:SS' format for large hours values). MySQL检索并以'HH:MM:SS'格式显示TIME值(或对于大型小时值显示为'HHH:MM:SS'格式)。 TIME values may range from '-838:59:59' to '838:59:59'. TIME值的范围可能是'-838:59:59'到'838:59:59'。 The hours part may be so large because the TIME type can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative). 小时部分可能是如此之大,因为“时间”类型不仅可以用来表示一天中的某个时间(必须少于24小时),而且可以用来表示经过的时间或两个事件之间的时间间隔(可能远大于24小时,甚至是负面的)。

The linked documentation contains a link that explains how time literals are interpreted . 链接的文档包含一个解释时间字面量如何解释的链接。

Your column type is TIMESTAMP 您的列类型为TIMESTAMP

11.3.1 The DATE, DATETIME, and TIMESTAMP Types 11.3.1 DATE,DATETIME和TIMESTAMP类型

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP数据类型用于包含日期和时间部分的值。 TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. TIMESTAMP的UTC范围为'1970-01-01 00:00:01'至UTC'2038-01-19 03:14:07'。

Solution you can change start_time and end_time columns type to DATETIME directly in DB or drop table and change you entity like this 解决方案,您可以直接在数据库或删除表中将start_time和end_time列类型更改为DATETIME,并像这样更改您的实体

@Column(name = "start_time", columnDefinition="DATETIME")
@Temporal(TemporalType.TIMESTAMP)
private Date startTime;

hope it will help 希望对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM