我有这个方法,我在Long获得当地时间,我想将其更改为UTC并返回Long中的结果 我执行以下操作,似乎改为UTC似乎不起作用。
private Long convertToUtc(Long localTime){
DateTime dt = new DateTime(localTime);
dt.toDateTime(DateTimeZone.UTC);
return dt.getMillis();
}
答案 0 :(得分:2)
您可以使用DateTime#withZone
方法(注意:它会返回一个新的DateTime
对象。因此您需要将其重新分配回新的DateTime
引用)
DateTime dt = new DateTime(localTime);
DateTime dtWithZone = dt.withZone(DateTimeZone.UTC);
return dtWithZone.getMillis();