news 2026/6/14 22:21:08

MySQL的日期时间类型

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
MySQL的日期时间类型

一 MySQL的日期时间类型

MySQL数据库的日期时间类型有date、time和datetime类型,还有timestamp类型,在Java代码中无论日期时间是什么样的格式,转换sql语句时统一为yyyy-MM-dd HH:mm:ss.S(Timestamp)的格式,创建案例简单演示。

1.1 创建数据库表

CREATE TABLE `apple` ( `id` varchar(255) DEFAULT NULL, `date_variable` date DEFAULT NULL, `time_variable` time DEFAULT NULL, `datetime_variable` datetime DEFAULT NULL )

1.2 创建apple表的基础代码

@Data @AllArgsConstructor @NoArgsConstructor @TableName("apple") public class Apple { private String id; private Date dateVariable; private Date timeVariable; private Date datetimeVariable; } @Mapper public interface AppleMapper extends BaseMapper<Apple> { } public interface IAppleService extends IService<Apple> { } @Service public class AppleServiceImpl extends ServiceImpl<AppleMapper, Apple> implements IAppleService { }

1.3 插入日期时间数据

@SpringBootTest @RunWith(SpringRunner.class) public class AppleTest { @Autowired private IAppleService appleService; @Test public void test1() throws ParseException { Apple apple = new Apple(); apple.setId("1001"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = simpleDateFormat.parse("2024-01-01 01:01:01"); apple.setDatetimeVariable(date); appleService.save(apple); } }

当代码中的日期时间格式是yyyy-MM-dd HH:mm:ss,没有指定毫秒值,sql语句解析如下:
==> Preparing: INSERT INTO apple ( id, datetime_variable ) VALUES ( , )
==> Parameters: 1001(String), 2024-01-01 01:01:01.0(Timestamp)
<== Updates: 1

@SpringBootTest @RunWith(SpringRunner.class) public class AppleTest { @Autowired private IAppleService appleService; @Test public void test1() throws ParseException { Apple apple = new Apple(); apple.setId("1002"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); Date date = simpleDateFormat.parse("2024年01月02日 01时01分01秒"); apple.setDatetimeVariable(date); appleService.save(apple); } }

当代码中的日期时间格式是yyyy年MM月dd日 HH时mm分ss秒,没有指定毫秒值,sql语句解析如下:
==> Preparing: INSERT INTO apple ( id, datetime_variable ) VALUES ( , )
==> Parameters: 1002(String), 2024-01-02 01:01:01.0(Timestamp)
<== Updates: 1
在Java代码中无论日期时间(不指定毫秒值)是什么样的格式,转换sql语句时统一为yyyy-MM-dd HH:mm:ss.S(Timestamp)的格式

1.4 MySQL的date类型

@Test public void test1() throws ParseException { List<Apple> list = new ArrayList<>(); Apple apple = new Apple(); apple.setId("1001"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = simpleDateFormat.parse("2023-12-06 17:59:27"); apple.setDateVariable(date); Apple apple2 = new Apple(); apple2.setId("1002"); SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd"); Date date2 = simpleDateFormat2.parse("2023-12-06"); apple2.setDateVariable(date2); Apple apple3 = new Apple(); apple3.setId("1003"); SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy"); Date date3 = simpleDateFormat3.parse("2023"); apple3.setDateVariable(date3); list.add(apple); list.add(apple2); list.add(apple3); appleService.saveBatch(list); }

在Java代码中无论日期时间(不指定毫秒值)是什么样的格式,转换sql语句时统一为yyyy-MM-dd HH:mm:ss.S(Timestamp)的格式,代码中的sql语句解析如下:
==> Preparing: INSERT INTO apple ( id, date_variable ) VALUES ( , )
==> Parameters: 1001(String), 2023-12-06 17:59:27.0(Timestamp)
==> Parameters: 1002(String), 2023-12-06 00:00:00.0(Timestamp)
==> Parameters: 1003(String), 2023-01-01 00:00:00.0(Timestamp)

apple表中的date_variable字段是date类型,数据库表存储date类型的格式是yyyy-MM-dd, 多余的日期时间数据会截取掉,缺省的日期默认取01月01日,上述三条记录入表如下:

1.5MySQL的time类型

@Test public void test1() throws ParseException { List<Apple> list = new ArrayList<>(); Apple apple4 = new Apple(); apple4.setId("1004"); SimpleDateFormat simpleDateFormat4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); Date date4 = simpleDateFormat4.parse("2023-12-06 17:59:27.123"); apple4.setTimeVariable(date4); Apple apple5 = new Apple(); apple5.setId("1005"); SimpleDateFormat simpleDateFormat5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date5 = simpleDateFormat5.parse("2023-12-06 17:59:27"); apple5.setTimeVariable(date5); Apple apple6 = new Apple(); apple6.setId("1006"); SimpleDateFormat simpleDateFormat6 = new SimpleDateFormat("yyyy-MM-dd HH"); Date date6 = simpleDateFormat6.parse("2023-12-06 17"); apple6.setTimeVariable(date6); Apple apple7 = new Apple(); apple7.setId("1007"); SimpleDateFormat simpleDateFormat7 = new SimpleDateFormat("yyyy-MM-dd"); Date date7 = simpleDateFormat7.parse("2023-12-06"); apple7.setTimeVariable(date7); list.add(apple4); list.add(apple5); list.add(apple6); list.add(apple7); appleService.saveBatch(list); }

在Java代码中无论日期时间(不指定毫秒值)是什么样的格式,转换sql语句时统一为yyyy-MM-dd HH:mm:ss.S(Timestamp)的格式,如果日期时间格式带有毫秒值,那么转换sql语句时将带有对应的毫秒值,代码中的sql语句解析如下:
==> Preparing: INSERT INTO apple ( id, time_variable ) VALUES ( , )
==> Parameters: 1004(String), 2023-12-06 17:59:27.123(Timestamp)
==> Parameters: 1005(String), 2023-12-06 17:59:27.0(Timestamp)
==> Parameters: 1006(String), 2023-12-06 17:00:00.0(Timestamp)
==> Parameters: 1007(String), 2023-12-06 00:00:00.0(Timestamp)

apple表中的time_variable字段是time类型,数据库表存储time类型的格式是HH:mm:ss, 多余的日期时间数据会截取掉,缺省的时间默认取00时00分00秒,上述四条记录入表如下:

1.6MySQL的datetime类型

@Test public void test1() throws ParseException { List<Apple> list = new ArrayList<>(); Apple apple8 = new Apple(); apple8.setId("1008"); SimpleDateFormat simpleDateFormat8 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); Date date8 = simpleDateFormat8.parse("2023-12-06 17:59:27.123"); apple8.setDatetimeVariable(date8); Apple apple9 = new Apple(); apple9.setId("1009"); SimpleDateFormat simpleDateFormat9 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date9 = simpleDateFormat9.parse("2023-12-06 17:59:27"); apple9.setDatetimeVariable(date9); Apple apple10 = new Apple(); apple10.setId("10010"); SimpleDateFormat simpleDateFormat10 = new SimpleDateFormat("yyyy-MM-dd HH"); Date date10 = simpleDateFormat10.parse("2023-12-06 17"); apple10.setDatetimeVariable(date10); Apple apple11 = new Apple(); apple11.setId("10011"); SimpleDateFormat simpleDateFormat11 = new SimpleDateFormat("yyyy-MM-dd"); Date date11 = simpleDateFormat11.parse("2023-12-06"); apple11.setDatetimeVariable(date11); Apple apple12 = new Apple(); apple12.setId("10012"); SimpleDateFormat simpleDateFormat12 = new SimpleDateFormat("yyyy"); Date date12 = simpleDateFormat12.parse("2023"); apple12.setDatetimeVariable(date12); list.add(apple8); list.add(apple9); list.add(apple10); list.add(apple11); list.add(apple12); appleService.saveBatch(list); }

在Java代码中无论日期时间(不指定毫秒值)是什么样的格式,转换sql语句时统一为yyyy-MM-dd HH:mm:ss.S(Timestamp)的格式,如果日期时间格式带有毫秒值,那么转换sql语句时将带有对应的毫秒值,代码中的sql语句解析如下:
==> Preparing: INSERT INTO apple ( id, datetime_variable ) VALUES ( , )
==> Parameters: 1008(String), 2023-12-06 17:59:27.123(Timestamp)
==> Parameters: 1009(String), 2023-12-06 17:59:27.0(Timestamp)
==> Parameters: 10010(String), 2023-12-06 17:00:00.0(Timestamp)
==> Parameters: 10011(String), 2023-12-06 00:00:00.0(Timestamp)
==> Parameters: 10012(String), 2023-01-01 00:00:00.0(Timestamp)

apple表中的datetime_variable字段是datetime类型,数据库表存储datetime类型的格式是yyyy-MM-dd HH:mm:ss, 多余的日期时间数据会截取掉,缺省的日期默认取01月01日 00时00分00秒,上述五条记录入表如下:

二 MySQL的datetime和timestamp

MySQL数据库的datetime和timestamp类型比较:
datetime类型需要8个字节的存储空间,timestamp类型需要4个字节的存储空间;
datetime类型的取值范围1001-01-01 00:00:00到9999-12-31 23:59:59;
timestamp类型的取值范围1970-01-01 00:00:00到2037-12-31 23:59:59 utc世界统一时间;
datetime类型存储的是本地时区(东八区)的日期时间,其他时区的用户查看数据也是东八区的日期时间,存在必然的误差,datetime类型存储数据基本上是原样输入和输出;
timestamp类型存储的是毫秒值,当前时间距1970-01-01 00:00:00的毫秒值,存储数据的时候需要对当前时间所在的时区进行转换,查询数据的时候再将时间转换为当前的时区,so使用timestamp类型存储的同一个时间值,在不同的时区查询时会显示不同的时间;
一般地,MySQL数据库存储日期时间使用datetime类型,用于日期时间函数计算使用timestamp类型,还有跨时区的业务用timestamp类型更合适;

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 9:17:09

提示工程架构师:自动化测试框架助力突破技术瓶颈

提示工程的“质检流水线”:用自动化测试框架突破AI应用的技术瓶颈 关键词 提示工程(Prompt Engineering)、自动化测试框架、AI应用质量、Prompt有效性、测试用例生成、结果评估、持续集成(CI/CD) 摘要 当我们谈论AI应用的“可靠性”时,往往忽略了一个关键环节——提示…

作者头像 李华
网站建设 2026/6/15 7:48:07

【课程设计/毕业设计】基于python_CNN卷积神经网络对甜点识别

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/6/13 14:33:07

深度学习毕设选题推荐:基于python_CNN卷积神经网络对甜点识别

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/6/8 23:27:08

mysql数据被误删的恢复方案

文章目录 一、使用备份恢复二、使用二进制日志&#xff08;Binary Log&#xff09;三、使用InnoDB表空间恢复四、使用第三方工具预防措施 数据误删是一个严重的数据库管理问题&#xff0c;但通过合理的备份策略和使用适当的恢复工具&#xff0c;可以有效地减少数据丢失的风险…

作者头像 李华
网站建设 2026/6/6 3:07:41

原来写英语论文涉及到这些问题都会被认定为学术不端啊!

近几年学术圈对于学术不端行为是严重的打压&#xff0c;无论是毕业论文还是小论文发表&#xff0c;只要出现了学术不端的行为&#xff0c;就将受到相应的惩罚。 同学们在写英文论文过程中&#xff0c;具体哪些行为属于学术不端行为呢&#xff1f; 主要表现有以下几个方面&…

作者头像 李华
网站建设 2026/6/13 7:36:16

最新APP导航下载页系统源码 带后台

非常好看的一款App导航推荐页源码&#xff0c;带后台&#xff0c;这是一款PHP源码&#xff0c;Thinkphp框架&#xff0c;这款源码安装非常便捷干净&#xff0c;后台添加应用及轮播广告也非常方便&#xff0c;网站自适应PC手机自适应&#xff0c;喜欢的自行部署吧&#xff01;安…

作者头像 李华