我有一个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: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;
}
}
答案 0 :(得分:0)
您可以使用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