MySQL的日期时间类型

打印 上一主题 下一主题

主题 1013|帖子 1013|积分 3039

一 MySQL的日期时间类型

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

  1. CREATE TABLE `apple` (
  2.   `id` varchar(255) DEFAULT NULL,
  3.   `date_variable` date DEFAULT NULL,
  4.   `time_variable` time DEFAULT NULL,
  5.   `datetime_variable` datetime DEFAULT NULL
  6. )
复制代码
1.2 创建apple表的底子代码

  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. @TableName("apple")
  5. public class Apple {
  6.     private String id;
  7.     private Date dateVariable;
  8.     private Date timeVariable;
  9.     private Date datetimeVariable;
  10. }
  11. @Mapper
  12. public interface AppleMapper extends BaseMapper<Apple> {
  13. }
  14. public interface IAppleService extends IService<Apple> {
  15. }
  16. @Service
  17. public class AppleServiceImpl extends ServiceImpl<AppleMapper, Apple>
  18. implements IAppleService {
  19. }
复制代码
1.3 插入日期时间数据

  1. @SpringBootTest
  2. @RunWith(SpringRunner.class)
  3. public class AppleTest {
  4.     @Autowired
  5.     private IAppleService appleService;
  6.     @Test
  7.     public void test1() throws ParseException {
  8.         Apple apple = new Apple();
  9.         apple.setId("1001");
  10.         SimpleDateFormat simpleDateFormat =
  11.                 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  12.         Date date = simpleDateFormat.parse("2024-01-01 01:01:01");
  13.         apple.setDatetimeVariable(date);
  14.         appleService.save(apple);
  15.     }
  16. }
复制代码
今世码中的日期时间格式是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
  1. @SpringBootTest
  2. @RunWith(SpringRunner.class)
  3. public class AppleTest {
  4.     @Autowired
  5.     private IAppleService appleService;
  6.     @Test
  7.     public void test1() throws ParseException {
  8.         Apple apple = new Apple();
  9.         apple.setId("1002");
  10.         SimpleDateFormat simpleDateFormat =
  11.                 new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
  12.         Date date = simpleDateFormat.parse("2024年01月02日 01时01分01秒");
  13.         apple.setDatetimeVariable(date);
  14.         appleService.save(apple);
  15.     }
  16. }
复制代码
今世码中的日期时间格式是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类型

  1. @Test
  2. public void test1() throws ParseException {
  3.     List<Apple> list = new ArrayList<>();
  4.     Apple apple = new Apple();
  5.     apple.setId("1001");
  6.     SimpleDateFormat simpleDateFormat =
  7.             new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  8.     Date date = simpleDateFormat.parse("2023-12-06 17:59:27");
  9.     apple.setDateVariable(date);
  10.     Apple apple2 = new Apple();
  11.     apple2.setId("1002");
  12.     SimpleDateFormat simpleDateFormat2 =
  13.             new SimpleDateFormat("yyyy-MM-dd");
  14.     Date date2 = simpleDateFormat2.parse("2023-12-06");
  15.     apple2.setDateVariable(date2);
  16.     Apple apple3 = new Apple();
  17.     apple3.setId("1003");
  18.     SimpleDateFormat simpleDateFormat3 =
  19.             new SimpleDateFormat("yyyy");
  20.     Date date3 = simpleDateFormat3.parse("2023");
  21.     apple3.setDateVariable(date3);
  22.     list.add(apple);
  23.     list.add(apple2);
  24.     list.add(apple3);
  25.     appleService.saveBatch(list);
  26. }
复制代码
在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.5 MySQL的time类型

  1. @Test
  2. public void test1() throws ParseException {
  3.     List<Apple> list = new ArrayList<>();
  4.     Apple apple4 = new Apple();
  5.     apple4.setId("1004");
  6.     SimpleDateFormat simpleDateFormat4 =
  7.             new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
  8.     Date date4 = simpleDateFormat4.parse("2023-12-06 17:59:27.123");
  9.     apple4.setTimeVariable(date4);
  10.     Apple apple5 = new Apple();
  11.     apple5.setId("1005");
  12.     SimpleDateFormat simpleDateFormat5 =
  13.             new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  14.     Date date5 = simpleDateFormat5.parse("2023-12-06 17:59:27");
  15.     apple5.setTimeVariable(date5);
  16.     Apple apple6 = new Apple();
  17.     apple6.setId("1006");
  18.     SimpleDateFormat simpleDateFormat6 =
  19.             new SimpleDateFormat("yyyy-MM-dd HH");
  20.     Date date6 = simpleDateFormat6.parse("2023-12-06 17");
  21.     apple6.setTimeVariable(date6);
  22.     Apple apple7 = new Apple();
  23.     apple7.setId("1007");
  24.     SimpleDateFormat simpleDateFormat7 =
  25.             new SimpleDateFormat("yyyy-MM-dd");
  26.     Date date7 = simpleDateFormat7.parse("2023-12-06");
  27.     apple7.setTimeVariable(date7);
  28.     list.add(apple4);
  29.     list.add(apple5);
  30.     list.add(apple6);
  31.     list.add(apple7);
  32.     appleService.saveBatch(list);
  33. }
复制代码
在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.6 MySQL的datetime类型

  1. @Test
  2. public void test1() throws ParseException {
  3.     List<Apple> list = new ArrayList<>();
  4.     Apple apple8 = new Apple();
  5.     apple8.setId("1008");
  6.     SimpleDateFormat simpleDateFormat8 =
  7.             new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
  8.     Date date8 = simpleDateFormat8.parse("2023-12-06 17:59:27.123");
  9.     apple8.setDatetimeVariable(date8);
  10.     Apple apple9 = new Apple();
  11.     apple9.setId("1009");
  12.     SimpleDateFormat simpleDateFormat9 =
  13.             new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  14.     Date date9 = simpleDateFormat9.parse("2023-12-06 17:59:27");
  15.     apple9.setDatetimeVariable(date9);
  16.     Apple apple10 = new Apple();
  17.     apple10.setId("10010");
  18.     SimpleDateFormat simpleDateFormat10 =
  19.             new SimpleDateFormat("yyyy-MM-dd HH");
  20.     Date date10 = simpleDateFormat10.parse("2023-12-06 17");
  21.     apple10.setDatetimeVariable(date10);
  22.     Apple apple11 = new Apple();
  23.     apple11.setId("10011");
  24.     SimpleDateFormat simpleDateFormat11 =
  25.             new SimpleDateFormat("yyyy-MM-dd");
  26.     Date date11 = simpleDateFormat11.parse("2023-12-06");
  27.     apple11.setDatetimeVariable(date11);
  28.     Apple apple12 = new Apple();
  29.     apple12.setId("10012");
  30.     SimpleDateFormat simpleDateFormat12 =
  31.             new SimpleDateFormat("yyyy");
  32.     Date date12 = simpleDateFormat12.parse("2023");
  33.     apple12.setDatetimeVariable(date12);
  34.     list.add(apple8);
  35.     list.add(apple9);
  36.     list.add(apple10);
  37.     list.add(apple11);
  38.     list.add(apple12);
  39.     appleService.saveBatch(list);
  40. }
复制代码
在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类型更合适;

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

三尺非寒

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表