简体   繁体   English

使用postgres的TimeZone将日期转换为时间戳

[英]Convert Date to TimeStamp with TimeZone of postgres

I have postgresql time column with type TIMESTAMP WITH TIME ZONE having a sample value like 1970-01-01 00:00:00+05:30 I want to compare a value with Date value like: 我有类型为TIMESTAMP WITH TIME ZONE postgresql时间列,其样本值类似于1970-01-01 00:00:00+05:30我想将一个值与Date值进行比较:

Date date = new Date(0L);

ie Tue Jan 19 13:55:24 IST 2016 即星期二1月19日13:55:24 IST 2016

So I used 所以我用

Timestamp timeStampDate = new Timestamp(date.getTime());

The above code gives timeStampDate.getTime() as 0. 上面的代码将timeStampDate.getTime()设置为0。

How to convert it in the above format so that it can be compared, Currently comparing is giving no results. 如何将其转换为上述格式,以便可以进行比较。当前比较没有任何结果。 How to format this date. 如何格式化此日期。

I basically want to convert " Date is Thu Jan 01 05:30:00 IST 1970 " to this format : "1970-01-01 00:00:00+05:30" 我基本上想将“ Date is Thu Jan 01 05:30:00 IST 1970”转换为以下格式:“ 1970-01-01 00:00:00 + 05:30”

java.time java.time

If your database driver supports JDBC 4.2 or later, use Instant to get data from your database where stored in a TIMESTAMP WITH TIME ZONE column. 如果您的数据库驱动程序支持JDBC 4.2或更高版本,请使用Instant从数据库中存储在TIMESTAMP WITH TIME ZONE列中的数据中获取数据。

Instant instant = myResultSet.getObject( … , Instant.class ) ;

For an older JDBC driver, use java.sql.Timestamp to get data from your database by calling getTimestamp on your ResultSet . 对于较旧的JDBC驱动程序,请使用java.sql.Timestamp通过在ResultSet上调用getTimestamp从数据库中获取数据。 That Timestamp object is in UTC. 该时间戳记对象位于UTC中。 Postgres stores your TIMESTAMP WITH TIME ZONE in UTC, by the way, and loses the original time zone or offset-from-UTC that came with the incoming data after using that data to adjust into UTC as part of the storage process. 顺便说一下,Postgres将您的TIMESTAMP WITH TIME ZONE存储在UTC中,并且在使用该数据调整为UTC作为存储过程的一部分之后,会丢失原始时区或传入数据随附的UTC偏移。

You should convert your java.sql.Timestamp/.Date/.Time to java.time types as soon as possible. 您应该尽快将java.sql.Timestamp / .Date / .Time转换为java.time类型。 Then proceed with your business logic. 然后继续您的业务逻辑。

In a nutshell, java.time is… An Instant is a moment on the timeline in UTC. 简而言之,java.time是…… Instant是UTC时间轴上的一个时刻。 Apply a time zone ( ZoneId ) to get a ZonedDateTime . 应用时区( ZoneId )以获取ZonedDateTime

Convert To java.time 转换为java.time

So convert from Timestamp, get an Instant, apply a ZoneId. 因此,从Timestamp转换,获取Instant,应用ZoneId。

java.sql.Timestamp ts = myResultSet.getTimestamp( 1 );
…
Instant instant = ts.toInstant();

Specify your desired/expected time zone. 指定所需/预期的时区。 Apply to the Instant. 申请即时。

ZoneId zoneId = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );

Comparison 比较方式

Get the current moment, for use in comparison. 获取当前时刻,以供比较。

ZonedDateTime now = ZonedDateTime.now( zoneId );

Compare by calling the isEqual , isBefore , or isAfter methods. 通过调用isEqualisBeforeisAfter方法进行比较。

Boolean beforeNow = zdt.isBefore( now );

About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 . 规格为JSR 310

Where to obtain the java.time classes? 在哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

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

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