今天遇到一个是非常诡异的现象,就是把Java的Date数据插入到MySQL的datetime类似字段中,值竟然改变了好是奇怪。
DBUG 信息如下:
17-05-16 19:46:34.623 DEBUG [pool-5-thread-2] UPDATE gb_user_info SET open_id=?,nick_name=?,sex=?,head_img=?,country=?,province=?,city=?,lang=?,follow_time=?,update_time=?,subscribe=? WHERE u_id=?
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|------------------------------|-----|---|----------------------------------------------------------------------------------------------------------------------------------------|----|----|----|-------|---------------------|---------------------|---|---|
| oa_djt3VhApMZ3EFXf9B0jGIqznQ | 小灰灰 | 1 | http://wx.qlogo.cn/mmopen/ffoh28JwicBbsicUFiaia2bI6M0ALicxicmryThgb23cr6nBx9IfeqkuBLGWgG4I9nj0PiaOnbJibbtIGFsSRuLbiaticUbPJhHvIAU7OH/0 | 中国 | 广东 | 梅州 | zh_CN | 2017-05-16 19:46:33 | 2017-05-16 19:46:34 | 1 | 1 |
For example:> "UPDATE gb_user_info SET open_id='oa_djt3VhApMZ3EFXf9B0jGIqznQ',nick_name='小灰灰',sex='1',head_img='http://wx.qlogo.cn/mmopen/ffoh28JwicBbsicUFiaia2bI6M0ALicxicmryThgb23cr6nBx9IfeqkuBLGWgG4I9nj0PiaOnbJibbtIGFsSRuLbiaticUbPJhHvIAU7OH/0',country='中国',province='广东',city='梅州',lang='zh_CN',follow_time='2017-05-16 19:46:33',update_time='2017-05-16 19:46:34',subscribe='1' WHERE u_id=1"
17-05-16 19:46:34.694 INFO [pool-5-thread-2] Add openId[oa_djt3VhApMZ3EFXf9B0jGIqznQ] subscribe success.
Java 代码:
public boolean saveSubscribeUser(UserInfo userInfo) {
boolean subscribed = false;
UserInfo userInfoTmp = get(userInfo.getOpenId());
try {
if (userInfoTmp != null && userInfoTmp.getUserId() != null) {
userInfo.setUserId(userInfoTmp.getUserId());
userInfo.setUpdateTime(new Date());
int rows = _updateIgnoreNull(userInfo);
subscribed = (rows > 0);
} else {
userInfo.setUpdateTime(new Date());
UserInfo insertUITmp = _insert(userInfo);
subscribed = (insertUITmp!=null);
}
} catch (Exception e) {
logger.error("Save subscribe user info failure!!!", e);
}
return subscribed;
}
/***一个关键的方法***/
public void cloneFromFollower(Follower follower) {
this.openId = follower.getOpenid();
this.nickName = follower.getNickname();
this.sex = Integer.toString(follower.getSex());
this.headImg = follower.getHeadimgurl();
this.lang = follower.getLanguage();
this.country = follower.getCountry();
this.province = follower.getProvince();
this.city = follower.getCity();
this.followTime = new Date(follower.getSubscribeTime() * 1000L);
this.subscribe = Integer.toString(follower.getSubscribe());
}
SQL客户端查询的结果:
mysql> SELECT nick_name,follow_time,update_time,subscribe FROM gb_user_info WHERE open_id='oa_djt3VhApMZ3EFXf9B0jGIqznQ'
-> ;
+-----------+---------------------+---------------------+-----------+
| nick_name | follow_time | update_time | subscribe |
+-----------+---------------------+---------------------+-----------+
| 小灰灰 | 2017-05-16 06:46:33 | 2017-05-16 06:46:35 | 1 |
+-----------+---------------------+---------------------+-----------+
1 row in set (0.00 sec)
为何这里的时间会不一样的呢? 可以确定的是服务器的时间是北京+8时区,然后程序也没有做别的操作,对了这里使用的JDBC驱动是:mysql-connector-java-6.0.5.jar
,驱动类为:com.mysql.cj.jdbc.Driver
, 各们有何建议呢?