如何将长时间转换为UTC并将其长时间返回

时间:2013-10-21 11:28:13

标签: java jodatime

我有这个方法,我在Long获得当地时间,我想将其更改为UTC并返回Long中的结果 我执行以下操作,似乎改为UTC似乎不起作用。

private Long convertToUtc(Long localTime){

  DateTime dt = new DateTime(localTime);
  dt.toDateTime(DateTimeZone.UTC);
  return  dt.getMillis();
 }

1 个答案:

答案 0 :(得分:2)

您可以使用DateTime#withZone方法(注意:它会返回一个新的DateTime对象。因此您需要将其重新分配回新的DateTime引用)

DateTime dt = new DateTime(localTime);
DateTime dtWithZone = dt.withZone(DateTimeZone.UTC);
return dtWithZone.getMillis();