David Boydston
David Boydston

Reputation: 29

Get partial seconds difference from timestamps

I have a code that basically logs data, as of now everything seems to be working fine except I am having issues getting the difference between two time stamps:

System.out.println(readings.size());
    displayLog.appendText("Getting ready to print! Beep Boop! \n" );

            for (int t = 0; t < readings.size(); t++)
    {
        displayLog.appendText("Time: " + readings.get(t).getTimestamp() + " CH1: " + readings.get(t).getValue(0) + " CH2: " + readings.get(t).getValue(1) + " CH3: " + readings.get(t).getValue(2) + " CH4: " + readings.get(t).getValue(3) + " CH5: " + readings.get(t).getValue(4) + " CH6: " + readings.get(t).getValue(5) + " CH7: " + readings.get(t).getValue(6) + " CH8: " + readings.get(t).getValue(7) + " CH9: " + readings.get(t).getValue(8) + "\n");
    }
            int maxReading = readings.size() - 1;
            displayLog.appendText(readings.get(0).getTimestamp() + "\n");
            displayLog.appendText(readings.get(maxReading).getTimestamp() + "\n");

            Duration timeDifference = Duration.between(readings.get(0).getTimestamp(), readings.get(maxReading).getTimestamp());

            displayLog.appendText("Time difference is: " + timeDifference.getSeconds() + "\n");

Whenever I run the code I get an interger, is there a way to get like a double with partial seconds displayed? Say 6.3 seconds or 8.8 seconds instead of 6 and 9. I dont even know if it rounds up or rounds down or truncates or what does it do when it returns this value? or is there a better way to get time difference?

The timestamps get logged by pressing a button using timeStamp LocalDateTime.now()

I tried this code to print and see but I get inconsisten results:

    int maxReading = readings.size() - 1;
    Duration timeDifference = Duration.between(readings.get(0).getTimestamp(), readings.get(maxReading).getTimestamp());
    double secondsDifference = timeDifference.getNano() / 1000000000;
    textLog.appendText("Total time logged is: " + secondsDifference + "\n");
    System.out.println(timeDifference.getSeconds() + "  |  " + secondsDifference + "  |  " + timeDifference.getNano());
    System.out.println("Time logged: " + secondsDifference);

However, this gives out the following output, which does not make sense (IMO):

4  |  0.0  |  270000000
Time logged: 0.0

Like, according to this it logged for 4 seconds? or for 270000000 nanoseconds? or is it actually 4.27 seconds?

Upvotes: 1

Views: 52

Answers (1)

assylias
assylias

Reputation: 328598

I would recommend using Instant.now(), in case you log stuff while a DST change is applied, or your computer changes time zone for whatever reason.

To get a "decimal" number of seconds, you can use:

Duration d = ...;
double seconds = d.toNanos() / 1e9d;

You can then append the result as a string, with the required number of decimals - say you want 2 decimals:

String twoDecimals = String.format("%.2f", seconds);

Upvotes: 1

Related Questions