【问题标题】:Array of Instants from Strings来自字符串的瞬间数组
【发布时间】:2019-10-31 18:08:31
【问题描述】:

我有一个 ArrayList,我想输入 Instant,但无论我尝试对数组做什么,它都不允许我将其转换为 Instant 格式。错误在于它尝试将字符串添加到 Instant 的 ArrayList。

@Data
@RequiredArgsConstructor
@AllArgsConstructor
public static class LiveData {
    private final String location;
    private final String metric;
    private Double data = null;
    private Instant timestamp = null;
}

private void onConnectionOpened() {
        try {
            int i = 0;
            final List<Sensor> sensors = clientConnection.querySensors().get();
            final List<String> metric = sensors.stream().map(sensor -> sensor.getLocation()).collect(Collectors.toList());
            final List<String> location = sensors.stream().map(sensor -> sensor.getMetric()).collect(Collectors.toList());
            List<Instant> timestamps = new ArrayList<>();
            List<Instant> times = new ArrayList<>();
            List<Double> datavalues = new ArrayList<>();
            while (i < sensors.size()) {
                final DataPoint data = clientConnection.queryValue(new Sensor(location.get(i), metric.get(i))).get();
                timestamps.add((Util.TIME_FORMAT.format((new Date(data.getTime()).toInstant()))));
                datavalues.add(data.getValue());
                i++;
            }

            i = 0;
            List<LiveData> testcol = new ArrayList<>();
            while (i < sensors.size()) {
                //LiveData temporary = new LiveData(location.get(i), metric.get(i));
                LiveData temporary = new LiveData(location.get(i), metric.get(i), datavalues.get(i), timestamps.get(i));
                testcol.add(temporary);
                i++;
            }
            ObservableList<LiveData> livedata = FXCollections.observableArrayList(testcol);
            Platform.runLater(() ->
                    tvData.setItems(livedata));
            //Thread.sleep(10000);
            //onConnectionOpened();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

我希望能够拥有 Instant 的 ArrayList,而不是像这样使用它:

LiveData temporary = new LiveData(location.get(i), metric.get(i), datavalues.get(i), timestamps.get(i));

供以后在 TableView 中使用。

我正在格式化 Instant 对象。我使用该格式是为了更好地输出实际时间戳,因为现在它看起来像这样:2019-07-18T05:35:00Z。我希望它是 2019-07-18 07:35:00。

【问题讨论】:

  • Util.TIME_FORMAT的类型是什么?你为什么要格式化 Instant 对象呢? Instant 不能有格式。请问create a Minimal, Reproducible Example好吗?
  • 我使用这种格式是为了更好地输出实际时间戳,因为现在它看起来像这样:2019-07-18T05:35:00Z。我希望它是2019-07-18 07:35:00。我提供了更多代码。
  • 对不起,如果我不清楚,我不打算要求 more 代码。一个最小的、可重现的例子更多是关于剥离代码中不直接有助于演示所问问题的部分。 Please read the text in the link。很高兴知道您接下来关于 Stack Overflow 的许多问题。

标签: java datetime arraylist java.time.instant


【解决方案1】:

假设data.getTime() 返回一个long,你只想

    timestamps.add(Instant.ofEpochMilli(data.getTime()));

无需混入陈旧且设计不佳的Date 类,这只会使事情变得不必要地复杂化。尝试格式化也没有用。 Instant 不能有格式。

如果没有完整的上下文,这并不完全清楚,但可能Util.TIME_FORMAT.format() 返回一个String,所以你试图将这个String 添加到你的列表中,这导致了你提到的错误消息:

错误是它试图将一个字符串添加到一个 ArrayList 瞬间。

在你的评论中你说:

我使用这种格式是为了获得更好的实际输出 时间戳,因为现在它看起来像这样:2019-07-18T05:35:00Z。和 我希望它是2019-07-18 07:35:00

对不起,那是错误的。除了最简单的一次性程序之外,您应该将模型和 UI 分开。 Instant 对象属于您的模型。你漂亮的输出——当然你应该有一个漂亮的输出,只有它属于你的用户界面。因此,您想要并且需要做的是在输出之前格式化Instant(而不是在将其放入列表之前)。我再说一遍:Instant 不能有格式。

相关问题,只询问现在已经过时的Date,格式为:

【讨论】:

  • 这不是我想要的 Instant 格式,但它必须是 Instant。我使用该格式是为了更好地输出实际时间戳,因为现在它看起来像这样:2019-07-18T05:35:00Z。我希望它是2019-07-18 07:35:00
  • 我已经编辑以解决这个(不可能,抱歉)要求。
猜你喜欢
  • 2019-05-19
  • 2020-03-06
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2013-01-03
  • 2012-04-24
  • 1970-01-01
相关资源
最近更新 更多