解决插入日期少于8个小时的问题

这个问题困扰了很久,原来还以为是mysql 的数据库问题,直接修改配置文件my.cnf(在/etc/my.cnf或者/etc/mysql/my.cnf),但是修改后重启mysql也无效,后来在本地调式没有这个问题,后来怀疑是阿里云服务器安装的jdk 存在问题,经过测试果然是,解决方法如下:
/**

  • @Title: getCurrentServerTime
  • @Description: 获取当前服务器时间
  • @return
    */
    public static String getCurrentServerTime(){
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
    String currentServerTime = sdf.format(date);
    return currentServerTime;
    }
    直接加上这段代码即可sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
    重新部署项目到服务器,发现成功了,哦也。

猜你喜欢

转载自blog.51cto.com/7218743/2132441