有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

jodatime转换java。双击org。乔达。时间瞬间

我到处看了很多。我对Java非常陌生,我正试图将一个双精度转换成一个瞬间。这个用例是针对GoogleDataflowJavaSDK的。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);
    }
}

我得到的错误是:

java.lang.Double cannot be cast to org.joda.time.Instant

问题是,我想将双精度的UNIX时间戳强制转换为Instants,以便将其传递给outputWithTimestamp。我相信这应该是一个微不足道的问题,但我还没有找到解决方案。多谢各位


共 (1) 个答案

  1. # 1 楼答案

    不能将Double强制转换为Instant。您需要将时间戳作为constructor argument传递:

    Instant timestamp = new Instant(((Number)row.getTimestamp("timestamp")).longValue());
    

    这假定“timestamp”值以毫秒为单位。如果是秒,就乘以1000