2

I have an ArrayList that I want to be type Instant, but no matter what I try to do on the array it just does not let me convert it to Instant format. The error is that it tries to add a String to an ArrayList of Instants.

@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();
        }
    }

I want to be able to have the ArrayList of Instants and than use it like this:

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

For later use in TableView.

I am formatting the Instant objects. I use that format in order to have a nicer output of the actual timestamp because now it looks like this: 2019-07-18T05:35:00Z. And I want it to be 2019-07-18 07:35:00.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Lazar Gugleta
  • 115
  • 1
  • 2
  • 14
  • What is the type of `Util.TIME_FORMAT`? And why are you trying to format the `Instant` objects at all? An `Instant` cannot have a format. Could you [create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), please? – Ole V.V. Oct 31 '19 at 18:19
  • I use that format in order to have a nicer output of the actual timestamp because now it looks like this: ```2019-07-18T05:35:00Z```. And I want it to be ```2019-07-18 07:35:00```. I provided more code. – Lazar Gugleta Oct 31 '19 at 18:30
  • 1
    Sorry if I was unclear, I didn’t intend to ask for *more* code. A minimal, reproducible example is more about stripping away the parts of the code that don’t contribute directly to demonstrating the problem asked about. [Please read the text in the link](https://stackoverflow.com/help/minimal-reproducible-example). It will be nice to know also for your next many questions on Stack Overflow. – Ole V.V. Oct 31 '19 at 19:04

1 Answers1

3

Assuming that data.getTime() returns a long, you just want

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

No need to mix in the old and poorly designed Date class, that will just complicate things needlessly. And no use trying to format. An Instant cannot have a format.

It’s not completely clear without the full context, but probably Util.TIME_FORMAT.format() returns a String, so you were trying to add this String to your list, which caused the error message that you referred to:

The error is that it tries to add a String to an ArrayList of Instants.

In your comment you said:

I use that format in order to have a nicer output of the actual timestamp because now it looks like this: 2019-07-18T05:35:00Z. And I want it to be 2019-07-18 07:35:00.

I am sorry, that’s wrong. In all but the simplest throw-away programs you should keep your model and your UI separate. The Instant objects belong in your model. Your nice output — of course you should have a nice output, only it belongs in your user interface. So what you want and need to do is format the Instant before outputting it (not before putting it into the list). And I repeat: An Instant cannot have a format.

Related questions, only asking about the now outdated Date with a format:

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • That is not the format I want for my Instant, but it has to be an Instant. I use that format in order to have a nicer output of the actual timestamp because now it looks like this: ```2019-07-18T05:35:00Z```. And I want it to be ```2019-07-18 07:35:00``` – Lazar Gugleta Oct 31 '19 at 18:33
  • I have edited to address this (impossible, sorry) requirement. – Ole V.V. Oct 31 '19 at 19:43