neko12
neko12

Reputation: 33

Android studio firestore timestamp comparing to currentLocalTime

So I want is to get the value of Firestore timestamp and Local device currentTime, and display the different:

Timestamp timestamp = (Timestamp) document.getData().get("createdAt");
Long tsLong = System.currentTimeMillis()/1000;
//I need the result value of (tsLong - timestamp seconds) 

But I'm stuck at the value calculating, how is it possible to get the milliseconds of the timestamp to Long type?

Upvotes: 0

Views: 1219

Answers (3)

Alex Mamo
Alex Mamo

Reputation: 138824

DocumentSnapshot class contains a method called getTimestamp(String field). So a simpler way to get the difference would be:

Timestamp createdAt = document.getTimestamp("createdAt");
Long seconds = System.currentTimeMillis()/1000;
long difference = seconds - createdAt.getSeconds();

Upvotes: 0

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79075

Do not perform the calculation yourself if there is a standard API available to do the same.

import java.time.Instant;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        long seconds = TimeUnit.SECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
        System.out.println(seconds);

        // Alternatively, using java.time API
        seconds = Instant.now().getEpochSecond();
        System.out.println(seconds);
    }
}

ONLINE DEMO

Coming back to your problem:

You can use DocumentSnapshot#getTimestamp to get the Timestamp from which you can get the seconds using Timestamp#getSeconds.

Thus, you can do it like

long diff = TimeUnit.SECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS) - document.getTimestamp("createdAt").getSeconds();

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Solution using java.time, the modern Date-Time API:

Using Timestamp#todate, you can convert Timestamp to java.util.Date which can be converted into java.time.Instant and then you can use java.time.Duration to find the difference in seconds as shown below:

long seconds = Duration.between(Instant.now(), document.getTimestamp("createdAt").toDate().toInstant()).toSeconds();

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Upvotes: 1

Shivam Jamaiwar
Shivam Jamaiwar

Reputation: 544

In firestore timestamp, we have an API getSeconds(). This API return time in second (long data type).

Timestamp timestamp = (Timestamp) document.getData().get("createdAt");
Long tsLong = System.currentTimeMillis()/1000;
long result = tsLong - timestamp.getSeconds();

Upvotes: 3

Related Questions