vcmkrtchyan
vcmkrtchyan

Reputation: 2626

How to get time difference in Android?

I am designing a stopwatch, and for my application I need to continuously get the time difference between this moment and the moment that stopwatch started.

Here is what I have

private int timestamp;

private Date startTime;

public void start() {

        if (!working) {

            startTime = new Date();
            working = true;

            tick = new Thread() {
                public void run() {
                    while (true) {
                        try {
                            timestamp = (new Date().getDate() - startTime.getDate())/1000;
                            msg = handler.obtainMessage(0, getTimeStamp());
                            handler.sendMessage(msg);
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                            working = false;
                            timestamp = 0;
                            break;
                        }
                    }
                }
            };

            tick.start();
        }
    }

But I'm getting a message that getDate() method is deprecated. How to refactor it? I have searched for a solution, and found out some similar posts, and tried out the solutions, but my IDE just doesn't recognise DateUtil, or DateTime, or toCalendar Methods, I guess I miss the right libraries? And one more question, could you give me some information about what exactly means that the methods are deprecated? It's not very clear to me. Thanks in advance.

Upvotes: 0

Views: 248

Answers (3)

lucianohgo
lucianohgo

Reputation: 319

One way to refactor this code would be to use SystemClock.uptimeMillis() if your clock doesn't stay on enough time for the device to sleep. Or you could use SystemClock.elapsedRealtime() when it does go over sleep time. Both clocks are monotonic and Google suggest their use for interval timing.

You could try it like this:

startTime = SystemClock.elapsedRealtime(); //Should be a long, not an int
working = true;

tick = new Thread() {
    public void run() {
        while (true) {
            try {
                timestamp = (SystemClock.elapsedRealtime() - startTime)/1000;
                msg = handler.obtainMessage(0, getTimeStamp());
                handler.sendMessage(msg);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
                working = false;
                timestamp = 0L
                break;
            }
        }
    }
};

But remember, as anything that uses time in java and Android, all this values are subject to change if the cellphone user changes his clock, so if you want a more robust solution, I suggest this article: http://julien-millau.fr/articles/Secure-Android-time-based-application.html.

Upvotes: 2

Emanuel0101
Emanuel0101

Reputation: 113

Sometimes deprecated methods could also be removed completely from the API one day, so it's better to use the newer techniques to not run into compatiblility issues.

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93726

For what you're doing, getDate would work fine. But a more Android way of doing it would be to use System.currentTimeMillis() instead of generating a Date object and all the inefficiencies there.

Deprecated basically means its no longer the suggested way of doing things by the author of the library. That doesn't mean it won't work, or that you should never use it- it means that you should understand the author feels there's better ways and you should have a reason to use the deprecated way.

Upvotes: 1

Related Questions