在joda时间如何转换时区而不改变时间 [英] In joda time how to convert time zone without changing time

查看:106
本文介绍了在joda时间如何转换时区而不改变时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据库获取UTC时间戳,我正在设置为JodaTime DateTime 实例

  DateTime dt = new DateTime(timestamp.getTime()); 

它完美地存储时间说 10:00 AM 但与当地时区。例如,我在IST时区是从UTC来的+5:30。

我尝试了很多改变时区的东西,但是随着时间的推移, 10:00 AM 通过使用+5:30差异来进行其他操作



有什么办法可以更改TimeZone而不影响当前时间



编辑:
如果我当前的时间是:

  2013-09-25 11:27:34 AM UTC 

以下是当我使用这个新的DateTime(timestamp.getTime());

  2013-09-25 11:27:34 AM亚洲/加尔各答

以下是结果当我使用这个新的DateTime(timestamp.getTime(),DateTimeZone.UTC);

  2013-09-25 05:57:34 AM UTC 


解决方案

您可以使用类 LocalDateTime

  LocalDateTime dt = new LocalDateTime(t.getTime()); 

并将 LocalDateTime 转换为 DateTime

  DateTime dt = new LocalDateTime(timestamp.getTime())toDateTime(DateTimeZone 。世界标准时间); 

Joda DateTime 随时以毫秒为单位自1970年以来的当前时区中的millis。因此,当您创建 DateTime 实例时,将使用当前时区创建。


I am getting UTC timestamp from database which is I am setting into a JodaTime DateTime instance

DateTime dt = new DateTime(timestamp.getTime());

It stores the time perfectly say 10:00 AM but with local time zone. E.g, I am in IST time zone which is +5:30 from UTC

I have tried lots of things for changing the timezone but with every thing it changes the time from 10:00 AM to something else by using +5:30 difference

Is there any way by which I can change TimeZone without affecting current time

EDIT: If my current time is:

2013-09-25 11:27:34 AM      UTC

Following is the result when I use this new DateTime(timestamp.getTime());

2013-09-25 11:27:34 AM  Asia/Kolkata

And following is the result when I use this new DateTime(timestamp.getTime(), DateTimeZone.UTC);

2013-09-25 05:57:34 AM  UTC

解决方案

You can use class LocalDateTime

LocalDateTime dt = new LocalDateTime(t.getTime()); 

and convert LocalDateTime to DateTime

DateTime dt = new LocalDateTime(timestamp.getTime()).toDateTime(DateTimeZone.UTC);  

Joda DateTime treats any time in millis like "millis since 1970y in current time zone". So, when you create DateTime instance, it is created with current time zone.

这篇关于在joda时间如何转换时区而不改变时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆