【问题标题】:How to get the UTC time in HH:MM:SS:ms format in Oracle SQL如何在 Oracle SQL 中以 HH:MM:SS:ms 格式获取 UTC 时间
【发布时间】:2023-04-04 15:52:01
【问题描述】:

我有一个 Oracle SQL 语句,它通过从 `enddate' 列中减去 startdate 来获取执行时间。这两列都以 UTC 格式存储日期时间,我将它们转换为 EST,然后通过从另一列中减去一个来获取执行时间。

当我在 Oracle SQL Developer 工具中执行查询时,我看到结果为“+00 00:00:02.951000”,但是当我使用 java 从 ResultSet 对象中获取数据时,我看到数据存储在 java 字符串对象中作为'0 0:0:2.951'

如何将字符串从 '+00 00:00:02.951000' 转换为 '00:00:02.95' 以表示格式 HH:MM:SS.ms 格式?

Oracle SQL

select      
  uuid, name, 
  to_char(from_tz(startdate, 'UTC') at time zone 'America/New_York', 'yyyy-mm-dd hh24:mi:ss') as startdate,      
  to_char(from_tz(enddate, 'UTC') at time zone 'America/New_York', 'yyyy-mm-dd hh24:mi:ss') as enddate,      
  (enddate - startdate) as executiontime
from      
  process  
where      
  name = (select name from jobconfiguration where currentprocessid = 'bGd_AAABNaMAAAFQHvY0UyTa');

用于从Resultset 对象中获取数据的Java 代码:

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import com.ebayenterprise.ecp.jobs.domain.JobHistory;
import com.ebayenterprise.ecp.jobs.utils.GenericUtils;
import org.apache.commons.dbutils.DbUtils;

public final class JobHistoryQueryExecutor extends BaseQueryExecutor {

    private static final Logger LOG = Logger.getLogger(JobHistoryQueryExecutor.class);

    public JobHistoryQueryExecutor(Connection cn, String sql) {
        super(cn, sql);
    }

    public List<JobHistory> getJobExecutionHistory() {
        Statement stmt = null;
        ResultSet rs = null;
        List<JobHistory> jobsHistory = new ArrayList<>();
        try {
            stmt = cn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs != null && rs.next()) {
                String uuid = StringUtils.defaultString(rs.getString("UUID"));
                String name = StringUtils.defaultString(rs.getString("NAME"));
                String startDate = StringUtils.defaultString(rs.getString("STARTDATE"));
                String endDate = StringUtils.defaultString(rs.getString("ENDDATE"));
                String executionTime = GenericUtils.getDateToString(rs.getTimestamp("EXECUTIONTIME"));
                String state = StringUtils.defaultString(rs.getString("STATE")).toUpperCase();
                String type = StringUtils.defaultString(rs.getString("TYPE"));
                String parentUuid = StringUtils.defaultString(rs.getString("PARENTUUID"));
                String domainId = StringUtils.defaultString(rs.getString("DOMAINID"));
                String lastModified = StringUtils.defaultString(rs.getString("LASTMODIFIED"));

                //create JobHistory
                JobHistory jobHistory = new JobHistory(uuid, name, startDate, endDate, executionTime, state, type, parentUuid, domainId, lastModified);
                jobsHistory.add(jobHistory);
            }
            LOG.debug("jobsHistory.size() = " + jobsHistory.size());
        } catch (SQLException ex) {
            LOG.error("Exception while fetching historical data for all jobs.......", ex);
            return null;
        } finally {
            DbUtils.closeQuietly(cn, stmt, rs);
        }
        return jobsHistory;
    }

}

【问题讨论】:

  • 您应该计算 UTC 而不是本地时间的差异,以便夏令时不会影响结果。在 Oracle 中,这些列是否声明为 TIMESTAMP 或 DATE?
  • 这些列在Oracle中被声明为TIMESTAMP

标签: java sql oracle datetime datetime-format


【解决方案1】:

你可以使用SimpleDateFormat

String executionTime = getDateToString(rs.getTimestamp("EXECUTIONTIME"));

public String getDateToString(final Timestamp date) {
    final SimpleDateFormat df = new SimpleDateFormat();
    df.applyPattern("HH:mm:ss.SSS");
    return df.format(date.getTime());
}

您需要根据您感兴趣的结果更改applyPattern...

HH:mm:ss.SSS 上面的示例打印了一个时间,即 22:59:59.999

如果你想要日期时间,你可以使用:dd/MM/yyyy HH:mm:ss.SSS

您可以在此处检查 pattern 值: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

【讨论】:

  • 将我的代码库更改为:String executionTime = GenericUtils.getDateToString(rs.getTimestamp("EXECUTIONTIME"));,但出现以下异常:` java.sql.SQLException:无效的列类型:getTimestamp 未为类 oracle.jdbc.driver.T4CIntervaldsAccessor 实现`
  • 请用一些 java 代码/导入更新您的问题,谢谢
猜你喜欢
  • 1970-01-01
  • 2018-12-18
  • 2015-10-17
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 2020-06-02
  • 2014-01-27
  • 2012-10-29
相关资源
最近更新 更多