![](/img/trans.png)
[英]How can I convert date time from time zone A to time zone B in Joda time?
[英]In joda time how to convert time zone without changing time
我从数据库获取UTC时间戳,我将其设置为JodaTime DateTime
实例
DateTime dt = new DateTime(timestamp.getTime());
它将时间完美地存储在10:00 AM
但具有当地时区。 例如,我在IST时区,距离UTC +5:30
我已经尝试了很多改变时区的东西,但是每件事都会通过使用+5:30的不同来改变从10:00 AM
到其他地方的时间
有什么办法可以在不影响当前时间的情况下更改TimeZone
编辑:如果我现在的时间是:
2013-09-25 11:27:34 AM UTC
以下是我使用这个new DateTime(timestamp.getTime());
2013-09-25 11:27:34 AM Asia/Kolkata
以下是我使用这个new 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.UTC);
Joda DateTime
以毫秒为单位处理“自1970年以来当前时区的毫升”。 因此,在创建DateTime
实例时,将使用当前时区创建它。
您可以使用DateTime
的withZoneRetainFields()
方法更改时区而不更改日期中的数字。
如果您的时间戳是:2015-01-01T00:00:00.000-0500(这是当地时间[对我而言])
试试这个:
DateTime localDt = new DateTime(timestamp.getTime())
.withZoneRetainFields(DateTimeZone.UTC)
.withZone(DateTimeZone.getDefault());
2014-12-31T19:00:00.000-05:00
分解:这为您提供了与您的时间戳对应的DateTime,指定它是UTC:
new DateTime(timestamp.getTime())
.withZoneRetainFields(DateTimeZone.UTC)
2015-01-01T00:00:00.000Z
这为您提供了DateTime,但时间转换为您当地时间:
new DateTime(timestamp.getTime())
.withZoneRetainFields(DateTimeZone.UTC)
.withZone(DateTimeZone.getDefault());
2014-12-31T19:00:00.000-05:00
我是这样做的:
private DateTime convertLocalToUTC(DateTime eventDateTime) {
// get your local timezone
DateTimeZone localTZ = DateTimeZone.getDefault();
// convert the input local datetime to utc
long eventMillsInUTCTimeZone = localTZ.convertLocalToUTC(eventDateTime.getMillis(), false);
DateTime evenDateTimeInUTCTimeZone = new DateTime(eventMillsInUTCTimeZone);
return evenDateTimeInUTCTimeZone.toDate();
}
我有同样的问题。 在阅读了这组有用的答案,并给出了我的特殊需求和可用对象后,我通过使用另一个DateTime构造函数解决了它:
new DateTime("2012-04-23T18:25:46.511Z", DateTimeZone.UTC)
没有给出的答案实际上解释了问题。 真正的问题是最初的假设是不正确的。 数据库的时间戳是使用本地JVM时区Asia/Kolkata
而不是UTC。 这是JDBC的默认行为,这也是为什么仍然建议将JVM时区设置为UTC的原因。
如果数据库的时间戳实际上是:
2013-09-25 11:27:34 AM UTC
或者采用ISO-8601格式:
2013-09-25T11:27:34Z // The trailing 'Z' means UTC
然后使用new DateTime(timestamp, DateTimeZone.UTC)
构造函数工作正常。 你自己看:
Timestamp timestamp = new Timestamp(1380108454000L);
DateTime dt = new DateTime(timestamp.getTime(), DateTimeZone.UTC);
System.out.println(dt); // => 2013-09-25T11:27:34.000Z
如果你想知道我是如何得到1380108454000L
,我只是使用了Joda解析类:
ISODateTimeFormat.dateTimeParser().parseMillis("2013-09-25T11:27:34Z")
或者,也有在线网站,您可以输入日期,时间和时区,并以毫秒为单位返回纪元值,反之亦然。 它有时候作为一种理智检查是好的。
// https://www.epochconverter.com
Input: 1380108454000 Click: "Timestamp to Human Date"
Assuming that this timestamp is in milliseconds:
GMT: Wednesday, September 25, 2013 11:27:34 AM
另外,请记住java.sql.Timestamp
类与Joda / Java 8+ Instant
类大致相关。 有时,更容易在等效类之间进行转换,以便像之前那样发现错误。
另外,我有另一种对我很有帮助的方法。 我想让我的工作流线程临时更改为特定时区(节省时间),然后当我的代码完成时,我再次设置原始时区。 事实证明,当你使用joda库时,做:
TimeZone.setDefault(TimeZone.getTimeZone(myTempTimeZone));
TimeZone.setDefault(timeZone);
这不够。 我们还需要在DateTimeZone中更改TimeZone,如下所示:
@Before
public void setUp() throws Exception {
timeZone = TimeZone.getDefault();
dateTimeZone = DateTimeZone.getDefault();
}
@After
public void tearDown() throws Exception {
TimeZone.setDefault(timeZone);
DateTimeZone.setDefault(dateTimeZone);
}
@Test
public void myTest() throws Exception {
TimeZone.setDefault(TimeZone.getTimeZone(myTempTimeZone));
DateTimeZone.setDefault(DateTimeZone.forID(myTempTimeZone));
//TODO
// my code with an specific timezone conserving time
}
希望它对其他人也有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.