簡體   English   中英

如何將長時間轉換為UTC並將其長時間返回

[英]How to Convert Long time to UTC and return it in long

我有這個方法,我在Long獲得當地時間,我想將其更改為UTC並返回結果Long我做以下操作,似乎改為UTC似乎不起作用。

private Long convertToUtc(Long localTime){

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM