如何将long(currentTimeInMillis)转换为UTC时间戳?

时间:2015-08-31 21:43:35

标签: java scala time utc

我的客户正在向我发送Long,可以将其视为

scala> System.currentTimeMillis
res3: Long = 1441056836609

scala> 

如何将其转换为UTC timeStamp?

在服务器上,我们正在使用Java 8

4 个答案:

答案 0 :(得分:10)

您可以使用Instant类方法。

import java.time.Instant;
import java.time.ZoneOffset;

Instant.ofEpochMilli(<yourmillis>).atOffset(ZoneOffset.UTC).toString();

您的示例日期为"2015-08-31T21:33:56.609Z"

答案 1 :(得分:1)

Date dateFromTime = new Date(timeInMillis);

这将获得一个Date对象,然后您可以使用

以适当的UTC格式吐出
DateFormat dateFormatter = SimpleDateFormat(/*UTC Format String*/, Locale./*Your Locale here*/);
System.out.printf("%s\n", dateFormatter.format(dateFromTime));

答案 2 :(得分:1)

由于您使用scala,我建议您使用scala方式,nscala-time是一个非常好的库

scala> import com.github.nscala_time.time.Imports._
import com.github.nscala_time.time.Imports._

scala> DateTimeZone.setDefault(DateTimeZone.UTC)

scala> new DateTime(1441056836609L)
res1: org.joda.time.DateTime = 2015-08-31T21:33:56.609Z

答案 3 :(得分:0)

这就是我正在做的事情

我正在使用Joda-Time并正在执行

DateTimeZone.setDefault(DateTimeZone.UTC);
DateTime.now.toString

client上我将其视为

Wed, 02 Sep 2015 20:57:34 GMT

并在server上我将其视为

2015-09-02T20:24:43.594Z

P.S。不比较值,它们的复制方式不同,格式是我想要分享的内容