如何在Java中的预期时区中检索Oracle Timestamp列?

瓦达

我在oracle表中有一个timestamp列。在存储时间的同时,我将此时间存储在UTC中。为了获取此时间戳,我使用Spring的JdbcTemplate,同时返回类型为TimeStamp的对象。

我想获取当前时区中“ dd-MM-YYYY HH:mm:ss”格式的日期时间字符串。为了实现这一点,我正在尝试以下代码:

new LocalDateTime(<retrieved TimeStamp>, <current user DateTimeZone>).toString("yyyy-MM-dd HH:mm:ss")

Joda库中的LocalDateTime和DateTimeZone。

但是,这却没有按预期工作。上面代码代替了当前用户的时区,只给我提供了UTC日期时间字符串。

我想念什么?

svr

从数据库中获取时间戳时添加utc日历,因此jdbc驱动程序可以使用此日历时区而不是默认系统时区。

//Assign utc calendar
Calendar utc= Calendar.getInstance();
utc.setTimeZone(TimeZone.getTimeZone("UTC"));
Timestamp timestamp = rs.getTimestamp("timestampcolumn", utc);
//Convert to client date time
DateTime dateTime = new DateTime(timestamp.getTime(), DateTimeZone.forID("Asia/Kolkata"));
//Format
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MM-YYYY HH:mm:ss");
//Change to client wall clock time
LocalDateTime localDateTime = dateTime.toLocalDateTime();
String formattedlocalDateTime = formatter.print(localDateTime)

String utcTime = "2016-06-17 14:22:02Z";
DateTimeFormatter parser = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ssZ");
DateTime dateTime = parser.parseDateTime(utcTime).withZone(DateTimeZone.forID("Asia/Kolkata"));
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MM-YYYY HH:mm:ss");
System.out.println(dateTime);
LocalDateTime localDateTime = dateTime.toLocalDateTime();
String formattedlocalDateTime = formatter.print(localDateTime);
System.out.println(formattedlocalDateTime);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Codeigniter中检索列值?

如何在PetaPoco中检索xml列

如何检索实木复合地板文件中的TIMESTAMP列的idAdjustedUTC标志值?

如何从java.sql.Timestamp中检索毫秒

如何从Firebase TimeStamp [Flutter]中检索时间?

将“ TimeStamp”列截断为pandas DataFrame中的小时精度

从 Oracle 中的 Timestamp 数据类型列中删除数据不起作用

如何在选定的 SharePoint 列表项列中检索值

如何从“带时区的时间戳”列中读取时区?

如何转换熊猫中datetime列的时区?

如何将值插入SQLAlchemy TIMESTAMP列

如何在 Oracle 中使用 current_timestamp 指定时区

Java ResultSet如何在UTC中获取TimeStamp

如何在Android Java中序列化Firestore TimeStamp?

如何在java.sql.Timestamp中添加时间

如何在Oracle中的不同列中修剪文本

使用Scala Slick从SQL db检索所有记录,其中timestamp列中的日期位于上个月或上周

从 Pandas Timestamp 列中提取年/月并将其存储在两个新列中

在MySQL / MariaDB中设置一列以使用unix_timestamp()更新INT(11)列

如何在准备好的stmt中输入变量,以便从列中检索所有变量?

如何在 Python 中检索矩阵中与某个值匹配的元素的行号和列号?

如何通过结合str timedelta / datetime列创建一个timestamp列?蟒蛇熊猫

如何将timestamp和offset列转换为单个timestamptz列?

如何在current_timestamp SQL(Oracle)中增加10秒

如何在 Oracle 中读取很长的 BLOB 列?

如何在Oracle View中更改列数据类型?

如何在Oracle查询的列别名中包含与号

如何在 oracle 表单中增加列的边框厚度

如何在 Oracle SQL 中向嵌套表添加列