Moe
Moe

Reputation: 1557

android local time and timestamp

hi to all how can i get the currant time and date for android and compare it with a timestamp i have

Upvotes: 2

Views: 5447

Answers (2)

mikebabcock
mikebabcock

Reputation: 791

The reference page you want is Android SystemClock. It recommends using either uptimeMillis or elapsedRealtime, depending on whether your app needs to count time during deep sleep or not.

long etime = 0;
final long time1 = uptimeMillis();
/* do something */
final long time2 = uptimeMillis();
if (time2 < time1) {
    etime = Long.MAX_VALUE - time1 + time2;
} else {
    etime = time2 - time1;
}

Note: the value returned can wrap because long isn't infinite; check for a negative value as above.

Upvotes: 1

alibi
alibi

Reputation: 866

Current Timestamp (Milliseconds)

long currentTimestamp = System.currentTimeMillis()

http://developer.android.com/reference/java/lang/System.html#currentTimeMillis()

Upvotes: 2

Related Questions