The time and date types of MySQL (version: 5.1.50) are as follows:
datetime 8bytes xxxx-xx-xx xx:xx:xx 1000-01-01 00:00:00 to 9999-12-31 23:59:59
timestamp 4bytes xxxx-xx-xx xx:xx:xx 1970-01-01 00:00:01 to 2038
date 3bytes xxxx-xx-xx 1000-01-01 to 9999-12-31
year 1bytes xxxx 1901 to 2155
time 3bytes xx:xx:xx -838:59:59 to 838:59:59 (in order to meet the addition and subtraction of time)
The classes that can save time and date types in Java (1.6) mainly include
java.util.Date
java.util.Calendar
java.sql.Date
java.sql.Time
java.sql.Timestamp
The time and date types previously queried from mysql are placed in the java.util.Date type. This brings a series of problems. First of all, this class provides too few time operation functions, and generally needs to be converted into java.util.Calendar to operate; secondly, even if java.util.Calendar is used , it is not very convenient. A simple idea requires writing a lot of code to realize it; the data content of java.util.Date is xxxx-xx-xx xx:xx:xx, sometimes no time is needed, only the date is needed. When the date type obtained from the database is placed in this class, the current time will be automatically added to the time position. This makes the two dates originally equal in the database, but they are no longer equal when they are taken out and placed in this class. It is a headache to consider the time error.
java provides three data types for easy interaction with mysql
java.sql.Date
java.sql.Time
java.sql.Timestamp
They all inherit from java.util.Date, which can be regarded as a simplification of the class and are very suitable for interacting with the database.
===========java injection into the database ==========
java type mysql type success or not
date date yes
date time no
date timestamp no
date datetime no
time date no
time time yes time
timestamp no
time datetime no
timestamp date yes
timestamp time yes timestamp timestamp
yes
timestamp datetime yes
======= ===end java injection database ========
General rule, if A completely contains B, then A can inject data into B, otherwise an error will be reported
========== Extract from database to java ==========
mysql类型 java类型 成与否
date date yes
date time yes --------------缺少的部分使用历元
date timestamp yes --------------缺少的部分使用历元
time date yes --------------缺少的部分使用历元
time time yes
time timestamp yes --------------缺少的部分使用历元
timestamp date yes
timestamp time yes
timestamp timestamp yes
datetime date yes
datetime time yes
datetime timestamp yes
==========end 从数据库提取到java=======
不会出错,缺少的部分使用历元,而不是当前日期时间
null to db(null) =====> 也是null
null to db(not null)=======> 数据库报错
db(null) to java==========> 如果单字段出来,则整个entity都是null,如果带着其他不是null的字段出来,则可以实例化entity,本身字段依然是null
db(not null) to java==========> 如果包含日期,则报错,否则为000
最优解决方案,定义成可以为null
java.sql时间系统的运算系列
after,before
compareTo原小于参数返回<0,等于返回=0,大于返回>0
优点:于数据库同类型,可以方便传输(无论是从DB到src还是反方向),方便比较大小
缺点:缺少运算单元,不适合时间跳跃的运算和间隔的运算
总结:calendar具有强大的跳跃运算和间隔运算能力,在需要的时候,可以将sql系列的时间转成calendar。
先设置calendar为历元,然后从sql系列时间中转换,最后再转回sql系列时间。
calendar只用于时间有跳跃的转换,对比运算统一使用sql系统,这样代码将更清晰
How to initialize date and calendar to GMT
new date(0)
calendar.setTimeInMillis(0)
SQL series time
static valueOf
new XX(0) to get epoch
new XX(year+1900, month+1,day,hour,minute ,second,nano) are obsolete, and creating
toString or SimpleDateFormat
Article source: http://blog.csdn.net/weinianjie1/article/details/6310770