java joda 获取utc时间_JodaTime-如何获取UTC的当前时间

我想获取UTC的当前时间。我到目前为止所做的是(仅出于测试目的):

DateTime dt = new DateTime();

DateTimeZone tz = DateTimeZone.getDefault();

LocalDateTime nowLocal = new LocalDateTime();

DateTime nowUTC = nowLocal.toDateTime(DateTimeZone.UTC);

Date d1 = nowLocal.toDate();

Date d2 = nowUTC.toDate();

L.d("tz: " + tz.toString());

L.d("local: " + d1.toString());

L.d("utc: " + d2.toString());

d1 是我的当地时间,那很好

d2 是我的本地时间+ 1,但应该是本地时间-1 …

我的本地时区是UTC + 1(根据调试输出和此处的列表:https : //www.joda.org/joda-

time/timezones.html)…

如何正确地从一个时区转换为另一个时区(包括毫秒表示)?

编辑

我需要日期/毫秒…这不是正确显示时间…。

编辑2

现在,借助评论和答案,我尝试了以下操作:

DateTimeZone tz = DateTimeZone.getDefault();

DateTime nowLocal = new DateTime();

LocalDateTime nowUTC = nowLocal.withZone(DateTimeZone.UTC).toLocalDateTime();

DateTime nowUTC2 = nowLocal.withZone(DateTimeZone.UTC);

Date dLocal = nowLocal.toDate();

Date dUTC = nowUTC.toDate();

Date dUTC2 = nowUTC2.toDate();

L.d(Temp.class, "------------------------");

L.d(Temp.class, "tz : " + tz.toString());

L.d(Temp.class, "local : " + nowLocal + " | " + dLocal.toString());

L.d(Temp.class, "utc : " + nowUTC + " | " + dUTC.toString()); // <= WORKING SOLUTION

L.d(Temp.class, "utc2 : " + nowUTC2 + " | " + dUTC2.toString());

输出值

tz : Europe/Belgrade

local : 2015-01-02T15:31:38.241+01:00 | Fri Jan 02 15:31:38 MEZ 2015

utc : 2015-01-02T14:31:38.241 | Fri Jan 02 14:31:38 MEZ 2015

utc2 : 2015-01-02T14:31:38.241Z | Fri Jan 02 15:31:38 MEZ 2015

我想要的是,本地日期显示15点,而utc日期显示14点…现在,这似乎可行…

----- EDIT3-最终解决方案-----

希望这是一个很好的解决方案…我想,我尊重我得到的所有小费…

DateTimeZone tz = DateTimeZone.getDefault();

DateTime nowUTC = new DateTime(DateTimeZone.UTC);

DateTime nowLocal = nowUTC.withZone(tz);

// This will generate DIFFERENT Dates!!! As I want it!

Date dLocal = nowLocal.toLocalDateTime().toDate();

Date dUTC = nowUTC.toLocalDateTime().toDate();

L.d("tz : " + tz.toString());

L.d("local : " + nowLocal + " | " + dLocal.toString());

L.d("utc : " + nowUTC + " | " + dUTC.toString());

输出:

tz : Europe/Belgrade

local : 2015-01-03T21:15:35.170+01:00 | Sat Jan 03 21:15:35 MEZ 2015

utc : 2015-01-03T20:15:35.170Z | Sat Jan 03 20:15:35 MEZ 2015

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值