I have looked around a lot. I am very new to Java, and I am trying to cast a Double into an Instant. The use case is for the Google Dataflow Java SDK. The Double is a UNIX timestamp from a file I read using TextIO. When I System.out.println(row.get("timestamp"))
I indeed get UNIX timestamps. When I do System.out.println(row.get("timestamp").getClass().getName())
, then I get java.lang.double
. what I have is as follows:
我环顾四周。我是Java的新手,我正在尝试将Double转换为Instant。该用例适用于Google Dataflow Java SDK。 Double是我使用TextIO读取的文件的UNIX时间戳。当我System.out.println(row.get(“timestamp”))我确实得到UNIX时间戳。当我做System.out.println(row.get(“timestamp”)。getClass()。getName()),然后我得到java.lang.double。我所拥有的如下:
static class ExtractTimestamp extends DoFn<TableRow, TableRow> {
@Override
public void processElement(ProcessContext c) {
TableRow row = c.element();
Instant timestamp = (Instant) row.get("timestamp");
c.outputWithTimestamp(row, timestamp);
}
}
The error I am getting is:
我得到的错误是:
java.lang.Double cannot be cast to org.joda.time.Instant
java.lang.Double无法强制转换为org.joda.time.Instant
The problem is that I want to cast the UNIX timestamps that are in double to Instants so I can pass it to outputWithTimestamp
. I believe this should be a trivial problem, but I wasn't able to find the solution yet. Thank you.
问题是我想将双时的UNIX时间戳转换为Instants,因此我可以将它传递给outputWithTimestamp。我认为这应该是一个微不足道的问题,但我还没有找到解决方案。谢谢。
1 个解决方案
#1
4
You can't "cast" a Double
to an Instant
. You need to pass your timestamp as a constructor argument:
你无法将“双重”投射到瞬间。您需要将时间戳作为构造函数参数传递:
Instant timestamp = new Instant(((Number)row.getTimestamp("timestamp")).longValue());
This assumes the "timestamp" value is in milliseconds. If it's seconds, just multiply by 1000.
这假定“timestamp”值以毫秒为单位。如果它是秒,只需乘以1000。
#1
4
You can't "cast" a Double
to an Instant
. You need to pass your timestamp as a constructor argument:
你无法将“双重”投射到瞬间。您需要将时间戳作为构造函数参数传递:
Instant timestamp = new Instant(((Number)row.getTimestamp("timestamp")).longValue());
This assumes the "timestamp" value is in milliseconds. If it's seconds, just multiply by 1000.
这假定“timestamp”值以毫秒为单位。如果它是秒,只需乘以1000。