如何使用Joda Time,JPA,EclipseLink和Postgres使用指定的时区保存和检索这些时间戳

tes*_*123 5 postgresql timezone jpa jodatime eclipselink

这似乎是一个常见的问题,所以也许我错过了一些明显的东西.使用Joda时间我想用时区保持日期.如果我没有使用JPA,我会想做这样的事情来保存和检索时间戳:

create table test_dates
(
  zone varchar(50),
  ts_with_time_zone timestamp with time zone
);

insert into test_dates values ('UTC', '2012-08-12 12:34:56 UTC');
insert into test_dates values ('MST', '2012-08-12 05:34:56 MST');

select 'UTC in MST', ts_with_time_zone at time zone 'MST' 
    from test_dates where zone = 'UTC';
Run Code Online (Sandbox Code Playgroud)

那么如何使用Joda Time,JPA,EclipseLink和Postgres使用指定的时区保存和检索这些时间戳?

我已经看到了使用转换器的答案,但我不确定您将如何指定或使用时区.当然,我可以获得UTC时间并自己进行转换,但这会破坏"有时区"列的目的.这可能吗?

理想的解决方案是EclipseLink如何处理类似于hibernate的 JodaTime .

Clo*_*eto 0

您不保存时区。您只需保存输入字符串中指定的时区的时间戳。时区将不会被保存。

select '2012-08-12 12:34:56 UTC'::timestamp with time zone;
      timestamptz       
------------------------
 2012-08-12 09:34:56-03

select '2012-08-12 05:34:56 MST'::timestamp with time zone;
      timestamptz       
------------------------
 2012-08-12 09:34:56-03
Run Code Online (Sandbox Code Playgroud)

时区将是数据库时区。

select now()::timestamp with time zone;
              now              
-------------------------------
 2012-12-10 12:26:16.318484-02
Run Code Online (Sandbox Code Playgroud)