简体   繁体   English

为什么这个查询结果中会出现时间

[英]Why does the time appear in this query result

In the following code, I use trunc(date) in the query, because I am only interested in the date, not the time.在下面的代码中,我在查询中使用了trunc(date) ,因为我只对日期感兴趣,而不对时间感兴趣。

However, I get this printed :但是,我得到了这个打印:

[2019-11-28 00:00:00=653] instead of [2019-11-28=653] . [2019-11-28 00:00:00=653]而不是[2019-11-28=653]

Here is the code :这是代码:

Map<String, Integer> map = jdbcTemplate.query("select trunc(closing_date) as closing_date, count(*) as total from some_table group by closing_date", new ResultSetExtractor<Map>() {
    @Override
    public Map extractData(ResultSet rs) {
        HashMap<String, Integer> mapRet = new HashMap<>();
        while(rs.next()) {
            mapRet.put(rs.getString("closing_date"), rs.getInt("total"));
        }
        return mapRet;
    }
});
LOGGER.info(Arrays.toString(map.entrySet().toArray()));

Why ?为什么 ?

TRUNC() removes the time element of a date in the sense that it rounds the time down to midnight (in the same way that trunc(23.42) gives us 23 ). TRUNC()删除日期的时间元素,因为它将时间向下trunc(23.42)到午夜(与trunc(23.42)给我们23方式相同)。 But Oracle DATE is still actually a datetime and so there's a time element to display, unless we use a format mask which only shows the date element.但是 Oracle DATE 实际上仍然是一个日期时间,因此需要显示一个时间元素,除非我们使用仅显示日期元素的格式掩码。

Trunc function only truncate the date. Trunc 函数只截断日期。 It do not change the display format.它不会改变显示格式。

You can use to_char(date, 'yyyy-mm-dd') to display date according to your requirement.您可以使用to_char(date, 'yyyy-mm-dd')根据您的要求显示日期。

Cheers!!干杯!!

While connecting from Java to Oracle database, If you can pass below Alter session command, then it can resolve your issue.从 Java 连接到 Oracle 数据库时,如果您可以通过下面的 Alter session 命令,那么它可以解决您的问题。

Alter Session Set NLS_DATE_FORMAT='DD-MON-YYYY';更改会话集 NLS_DATE_FORMAT='DD-MON-YYYY';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM