繁体   English   中英

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

[英]How to retrieve oracle Timestamp column in expected timezone in Java?

我在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提供日期时间字符串。

我错过了什么?

我认为您的应用程序使用的java.util.Date没有时区信息, toString用法在创建字符串时应用JVM的当前默认时区。

您可以通过调整时区( 使用Joda Library

ZonedDateTime Tokyo = ZonedDateTime.ofInstant (instant,zoneIdTokyo) ;

或实施区域

DateTimeZone zone = DateTimeZone.forID("Asia/Tokyo");

您正在使用LocalDateTime ,它是表示本地日期和时间(不是时区)的不可变类

编辑 - 您可以尝试这个(我还没有测试过)

DateTime udate = new DateTime("2016-05-01T20:10:04", DateTimeZone.UTC);
System.out.println(udate);
DateTime zone = udate.plusMillis(10000)
.withZone(DateTimeZone.forID("Asia/Kolkata"));
System.out.println(zone);

从数据库中获取时间戳时添加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);

暂无
暂无

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

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