我的客户正在发送给我一个可以被认为是长整型
的数据。
scala> System.currentTimeMillis
res3: Long = 1441056836609
scala>
如何将其转换为UTC
时间戳?
在服务器上,我们使用的是Java 8
。
你可以使用Instant
类的方法。
import java.time.Instant;
import java.time.ZoneOffset;
Instant.ofEpochMilli(<yourmillis>).atOffset(ZoneOffset.UTC).toString();
你的示例日期将是"2015-08-31T21:33:56.609Z"
。
Date dateFromTime = new Date(timeInMillis);
DateFormat dateFormatter = SimpleDateFormat(/*UTC Format String*/, Locale./*Your Locale here*/);
System.out.printf("%s\n", dateFormatter.format(dateFromTime));
由于您正在使用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
这是我正在做的事情
我正在使用Joda-Time并进行操作
DateTimeZone.setDefault(DateTimeZone.UTC);
DateTime.now.toString
在 client
上我看到它是这样的
Wed, 02 Sep 2015 20:57:34 GMT
而在服务器
上,我看到它是这样的
2015-09-02T20:24:43.594Z
附言:不要比较值,它们被复制的方式不同,我想分享的是格式。