解析MySql与Java的时间类型


Posted in PHP onJune 22, 2013

MySql的时间类型有          Java中与之对应的时间类型
date                                           java.sql.Date
Datetime                                    java.sql.Timestamp
Timestamp                                  java.sql.Timestamp
Time                                          java.sql.Time
Year                                           java.sql.Date

对其进行分析
参考MySql 的reference manual
Date:
A date. The supported range is '1000-01-01' to '9999-12-31'. MySQL displays DATE values in 'YYYY-MM-DD' format, but allows you to assign values to DATE columns using either strings or numbers.
只记录日期信息,表示范围为1000-01-01 至 9999-12-31。
MySql 按照YYYY-MM-DD 的方式进行该类字段的显示。添加该类字段数据,即可以使用字符串类型,也可以使用数字类型

由于Date类型的字段只记录日期信息,所以如果添加的数据中包含了时间信息,该时间信息将会自动被截断。
如果要保存时间信息,可以考虑使用DateTime类型。
经过测试,发现如下2种方式可以对Date类型字段进行填充:
按字符串:
insert into time_table(CreateDate) values(‘2007-04-09')
按数字:
insert into time_table(CreateDate) values(20070409)
获取可以用java.sql.Date类型获取
代码为:
Date dtDate =rsBuffer.getDate("CreateDate");
测试代码如下:(其中,IDBFace 是自己基于JDBC封装的一个简单类, 接受Sql对数据库进行操作)

public void testDate()throws SQLException
{
       IDBFace DBFace =DBFactory.createMySqlFace();
       DBFace.connect();
       //清空表
       String strDelete ="delete from time_table";
       DBFace.update(strDelete);
       //添加       String strInsert ="insert into time_table(CreateDate) values(20070409)";
       DBFace.update(strInsert);
              
       //获取
       String strSelect ="select * from time_table";
       ResultSet rsBuffer =DBFace.select(strSelect);
       while(rsBuffer.next())
       {
              Date dtDate =rsBuffer.getDate("CreateDate");
              System.out.println(dtDate.toString());
       }
       DBFace.close();
}

执行结果: 2007-04-09

DateTime
A date and time combination. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'. MySQL displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format, but allows you to assign values to DATETIME columns using either strings or numbers.
DateTime 与Date最主要的区别在于:DateTime 可以记录日期和时间信息。而Date只记录日期信息。表示范围为: 1000-01-01 00:00:00 至 9999-12-31 23:59:59 MySql的按照YYYY-MM-DD HH:MM:SS对数据进行格式化,允许以字符串和数字的方式提交。

例如以数字的方式进行提交:
insert into time_table(CreateDate) values(20070409132013)
获取该类型的数据可以使用:java.sql.Timestamp类型
代码如下:

public void testDateTime() throws SQLException
{
       IDBFace DBFace =DBFactory.createMySqlFace();
DBFace.connect();
       //清空表
       String strDelete ="delete from time_table";
       DBFace.update(strDelete);
       //添加       String strInsert ="insert into time_table(CreateDateTime) values(20070409132013)";
       DBFace.update(strInsert);
       //获取
       String strSelect ="select * from time_table";
       ResultSet rsBuffer =DBFace.select(strSelect);
       while(rsBuffer.next())
       {
              Timestamp tsBuffer =rsBuffer.getTimestamp("CreateDateTime");
              System.out.println(tsBuffer.toString());
       }
       DBFace.close();
}

执行结果: 2007-04-09 13:20:13.0
TimeStamp
A timestamp. The range is '1970-01-01 00:00:00' to partway through the year 2037. A TIMESTAMP column is useful for recording the date and time of an INSERT or UPDATE operation. The first TIMESTAMP column in a table is automatically set to the date and time of the most recent operation if you don't assign it a value yourself. You can also set any TIMESTAMP column to the current date and time by assigning it a NULL value.
与DateTime类型非常相似
范围为1970-01-01 ?2037年,精度为1秒/
如果在Sql中未对Timestamp类型的列赋值,该列将被构造成当前时间。
提交NULL值也会使该列以当前时间录入。
如果时间提交错误,该列将被填入0.
Timestamp比DateTime 类型所需的存储空间更小,只需要4个字节,而DateTime需要8个字节。
但是有一点需要特别注意。Timestamp只能表示时间范围为1970 -2037.
使用Timestamp一定要确保提交的时间数据一定不会超过这个范围。
代码与DateTime类是,而且我不喜欢用,所以略掉了。
Time:
A time. The range is '-838:59:59' to '838:59:59'. MySQL displays TIME values in 'HH:MM:SS' format, but allows you to assign values to TIME columns using either strings or numbers.

Time只记录时间信息,不包含日期信息。
范围为-838:59:59 到 838:59:59, MySql 以HH:MM:SS格式化该数据,允许输入为字符串或者数字。
代码:

public void testTime() throws SQLException
       {
              IDBFace DBFace =DBFactory.createMySqlFace();
              DBFace.connect();
              //清空表
              String strDelete ="delete from time_table";
              DBFace.update(strDelete);
              //添加              String strInsert ="insert into time_table(CreateTime) values(131211)";
              DBFace.update(strInsert);
              //获取
              String strSelect ="select * from time_table";
              ResultSet rsBuffer =DBFace.select(strSelect);
              while(rsBuffer.next())
              {
                     Time tmBuffer =rsBuffer.getTime("CreateTime");
                     System.out.println(tmBuffer.toString());
              }
              DBFace.close();
       }

执行结果: 13:12:11
Year
A year in two-digit or four-digit format. The default is four-digit format. In four-digit format, the allowable values are 1901 to 2155, and 0000. In two-digit format, the allowable values are 70 to 69, representing years from 1970 to 2069. MySQL displays YEAR values in YYYY format, but allows you to assign values to YEAR columns using either strings or numbers. The YEAR type is unavailable prior to MySQL 3.22.

Year可以有2种表示方式,4位的和2位的。
默认情况是4位。其范围为1901-2155
2位的表述法只记录后2位 。其范围为1970-2069
允许以字符串或者数字的方式插入。
代码:

       public void testYear() throws SQLException
       {
              IDBFace DBFace =DBFactory.createMySqlFace();
              DBFace.connect();
              //清空表
              String strDelete ="delete from time_table";
              DBFace.update(strDelete);
              //添加              String strInsert ="insert into time_table(CreateYear) values(2007)";
              DBFace.update(strInsert);
              //获取
              String strSelect ="select * from time_table";
              ResultSet rsBuffer =DBFace.select(strSelect);
              while(rsBuffer.next())
              {
                     Date dtBuffer =rsBuffer.getDate("CreateYear");
                     System.out.println(dtBuffer.getYear()+1900);
              }
              DBFace.close();
       }

执行结果:2007
需要说明的是:
Date.getYear()方法返回至1900年起经过了多少年。所以为了显示正确的时间,必须加上1900.
该方法已经被废弃。

另外。
有一种方法是:不使用上述任何一种类型来记录时间。
而是以char(或vchar)的方式来记录时间。
这样做在插入数据和显示记录的时候固然不用进行任何转换而比较方便。
但是要承担2个重要的缺陷。
(1) 要单独开发方法验证时间数据的合法性。例如ajidjieoa字符串不是一个时间信息,但仍然可以正常插入。
(2) 如果系统需将时间范围做为条件进行记录检索。这也会是一个大麻烦。用字符串记录时间将无法使用MySql为时间提供的API.对时间范围检索的代码可能与数据库剥离。这样对性能必然造成影响。例如,要从100万条数据中查询时间范围为1992-3-12 ?1992-3-13日的区区100条数据,你可能不得不将100万条数据都查出来,再开发新的方法进行过滤。

另外,MySql到4.1时间精度貌若只到秒。
要记录更细的时间粒度。可以考虑构造DateTime.
记录DateTime.trick().
这只是一个想法,有没有额外的问题尚未证明。/

PHP 相关文章推荐
一个简单的MySQL数据浏览器
Oct 09 PHP
php SQL防注入代码集合
Apr 25 PHP
php strrpos()与strripos()函数
Aug 31 PHP
迅速确定php多维数组的深度的方法
Jan 07 PHP
php实现有趣的人品测试程序实例
Jun 08 PHP
php需登录的文件上传管理系统
Mar 21 PHP
php无法连接mysql数据库的正确解决方法
Jul 01 PHP
jquery+thinkphp实现跨域抓取数据的方法
Oct 15 PHP
详解PHP原生DOM对象操作XML的方法
Oct 17 PHP
yii框架搜索分页modle写法
Dec 19 PHP
Yii框架引用插件和ckeditor中body与P标签去除的方法
Jan 19 PHP
PHP PDOStatement::errorInfo讲解
Jan 31 PHP
解析mysql 表中的碎片产生原因以及清理
Jun 22 #PHP
解析thinkphp中的M()与D()方法的区别
Jun 22 #PHP
AJAX的跨域访问-两种有效的解决方法介绍
Jun 22 #PHP
浅析PHP substr,mb_substr以及mb_strcut的区别和用法
Jun 21 #PHP
PHP中mb_convert_encoding与iconv函数的深入解析
Jun 21 #PHP
解析php获取字符串的编码格式的方法(函数)
Jun 21 #PHP
浅析PHP页面局部刷新功能的实现小结
Jun 21 #PHP
You might like
为查询结果建立向后/向前按钮
2006/10/09 PHP
PHP 地址栏信息的获取代码
2009/01/07 PHP
php中ob_flush函数和flush函数用法分析
2015/03/18 PHP
PHP通过微信跳转的Code参数获取用户的openid(关键代码)
2016/07/06 PHP
Yii视图操作之自定义分页实现方法
2016/07/14 PHP
Laravel 使用查询构造器配合原生sql语句查询的例子
2019/10/12 PHP
javascript 事件查询综合 推荐收藏
2010/03/10 Javascript
jQuery的12招常用技巧分享
2011/08/08 Javascript
基于jquery实现的树形菜单效果代码
2015/09/06 Javascript
JavaScript实战之带收放动画效果的导航菜单
2016/08/16 Javascript
浅析JavaScript异步代码优化
2019/03/18 Javascript
深入浅出 Vue 系列 -- 数据劫持实现原理
2019/04/23 Javascript
vue项目中将element-ui table表格写成组件的实现代码
2019/06/12 Javascript
基于form-data请求格式详解
2019/10/29 Javascript
python获取Linux下文件版本信息、公司名和产品名的方法
2014/10/05 Python
Python yield 使用浅析
2015/05/28 Python
Python图像灰度变换及图像数组操作
2016/01/27 Python
Python实现合并同一个文件夹下所有txt文件的方法示例
2018/04/26 Python
基于python框架Scrapy爬取自己的博客内容过程详解
2019/08/05 Python
python NumPy ndarray二维数组 按照行列求平均实例
2019/11/26 Python
python 爬取马蜂窝景点翻页文字评论的实现
2020/01/20 Python
Python基于xlrd模块处理合并单元格
2020/07/28 Python
Python Flask异步发送邮件实现方法解析
2020/08/01 Python
记一次python 爬虫爬取深圳租房信息的过程及遇到的问题
2020/11/24 Python
路易威登和香奈儿手袋:LuxeDH
2017/01/12 全球购物
澳大利亚最好的在线时尚精品店:Princess Polly
2018/01/03 全球购物
Love, Bonito国际官网:新加坡女装品牌
2021/03/13 全球购物
火山动力Java笔试题
2014/06/26 面试题
安全生产责任书
2014/03/12 职场文书
工人先锋号申报材料
2014/12/29 职场文书
优质护理心得体会
2016/01/22 职场文书
关于python类SortedList详解
2021/09/04 Python
php png失真的原因及解决办法
2021/11/17 PHP
mysql 联合索引生效的条件及索引失效的条件
2021/11/20 MySQL
MySQL中order by的执行过程
2022/06/05 MySQL
Go结合Gin导出Mysql数据到Excel表格
2022/08/05 Golang