Frank_Adam
程序员小辰
2019-05-07 11:03
采纳率: 50%
浏览 1.6k
已结题

java 查数据库时间不对

cars表里有如下数据:

mysql> select * from cars;
+------+--------+--------+---------------------+---------------------+
| id | name | vendor | ctime | utime |
+------+--------+--------+---------------------+---------------------+
| 801 | China1 | China | 2019-05-07 09:45:32 | 2019-05-07 09:45:32 |
| 802 | China2 | China | 2019-05-07 09:45:37 | 2019-05-07 09:45:37 |
| 803 | Japan | Japan | 2019-05-07 09:45:37 | 2019-05-07 09:45:37 |
| 1001 | China1 | China | 2019-05-07 10:18:21 | 2019-05-07 10:18:21 |
| 1002 | China2 | China | 2019-05-07 10:18:22 | 2019-05-07 10:18:22 |
| 1003 | Japan | Japan | 2019-05-07 10:18:22 | 2019-05-07 10:18:22 |
| 1004 | China1 | China | 2019-05-07 10:18:56 | 2019-05-07 10:18:56 |
| 1005 | China2 | China | 2019-05-07 10:18:56 | 2019-05-07 10:18:56 |
| 1006 | Japan | Japan | 2019-05-07 10:18:56 | 2019-05-07 10:18:56 |
| 1201 | China1 | China | 2019-05-07 10:49:44 | 2019-05-07 10:49:44 |
| 1202 | China2 | China | 2019-05-07 10:49:44 | 2019-05-07 10:49:44 |
| 1203 | Japan | Japan | 2019-05-07 10:49:44 | 2019-05-07 10:49:44 |
+------+--------+--------+---------------------+---------------------+
12 rows in set (0.00 sec)

java中执行查询,
SELECT * FROM cars WHERE vendor='China'
根据resultSet遍历获取各列的值,ctime和utime和数据库里的有差别。
[
{
utime: "2019-05-07 10:45:32",
vendor: "China",
name: "China1",
ctime: "2019-05-07 10:45:32",
id: 801
},
{
utime: "2019-05-07 10:45:37",
vendor: "China",
name: "China2",
ctime: "2019-05-07 10:45:37",
id: 802
},
{
utime: "2019-05-07 11:18:21",
vendor: "China",
name: "China1",
ctime: "2019-05-07 11:18:21",
id: 1001
},
{
utime: "2019-05-07 11:18:22",
vendor: "China",
name: "China2",
ctime: "2019-05-07 11:18:22",
id: 1002
},
{
utime: "2019-05-07 11:18:56",
vendor: "China",
name: "China1",
ctime: "2019-05-07 11:18:56",
id: 1004
},
{
utime: "2019-05-07 11:18:56",
vendor: "China",
name: "China2",
ctime: "2019-05-07 11:18:56",
id: 1005
},
{
utime: "2019-05-07 11:49:44",
vendor: "China",
name: "China1",
ctime: "2019-05-07 11:49:44",
id: 1201
},
{
utime: "2019-05-07 11:49:44",
vendor: "China",
name: "China2",
ctime: "2019-05-07 11:49:44",
id: 1202
}
]

贴上代码部分:

            Statement statement = mysqlConnection.createStatement();
            ResultSet resultSet = statement.executeQuery(realSql);
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            int columnCount = resultSetMetaData.getColumnCount();
            List<String> columnNames = new ArrayList<>();
            for(int i=1;i<=columnCount;i++) {
                columnNames.add(resultSetMetaData.getColumnName(i));
            }
            while(resultSet.next()) {
                Map<String, Object> valuesMap = new HashMap<>();
                for(String col:columnNames) {
                    if(col.equals("ctime") || col.equals("utime")) {
                        String s = simpleDateFormat.format(resultSet.getObject(col));
                        valuesMap.put(col, s);
                    } else {
                        valuesMap.put(col, resultSet.getObject(col));
                    }
                }
                resultList.add(valuesMap);
            }

请大佬们看看问题在哪,是resultSet.getObject有什么问题还是什么。。


补充:

图片说明图片说明
这一条数据数据库里是9:45:32

  • 点赞
  • 收藏

2条回答 默认 最新

  • qq_33427422
    子系天下 2019-05-07 11:09
    已采纳

    首先确认是不是同一个库,其次确认是时区是否一致,最后写一个demo,单独拿出对时间处理的逻辑看是否有问题;如果会debug的话可以打个断点看下在处理日期之前的值是什么,处理之后的值是什么来锁定错误位置

    点赞 评论
  • Frank_Adam
    程序员小辰 2019-05-07 11:28

    Java 查MySQL timestamp类型时间差13小时。
    时区问题可参考:https://blog.csdn.net/CHS007chs/article/details/81348291

    点赞 评论

相关推荐