How to get a timestamp in Android with Kotlin: a complete guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Android Get Current Timestamp in Kotlin: from beginner to professional

\n\n

Have you ever wondered how your smartphone knows the exact time when you send a message, take a photo, or start a timer? At the heart of this magic lies a timestamp — a digital marker that records a moment with millisecond precision. If you are writing an Android app in Kotlin, sooner or later you will face the task: "Android get current timestamp".

\n\n

This question is one of the most popular on Stack Overflow (thousands of views!). And it's no surprise: it seems simple, but in practice, beginners get lost in the confusion between milliseconds, seconds, time zones, and deprecated classes. In this article, we will break down all the ways to get the current timestamp in Android with Kotlin — from basic to advanced. You will learn which method to choose for your task and how to avoid common mistakes.

\n\n

What is a timestamp and why do you need it in Android?

\n\n

A timestamp is a number that represents the amount of time elapsed since a specific reference point. Most commonly, the Unix timestamp is used — the number of milliseconds (or seconds) since January 1, 1970, 00:00:00 UTC. Why is this needed in Android?

\n\n
    \n
  • Event logging — recording user actions.
  • \n
  • Data synchronization — sending the time to a server.
  • \n
  • Caching — checking data staleness.
  • \n
  • Animations and timers — calculating elapsed time.
  • \n
  • Databases — sorting records by time.
  • \n
\n\n

In Android development with Kotlin, you have several tools for getting a timestamp. Let's look at them from simple to complex.

\n\n

Method 1: System.currentTimeMillis() — the classic

\n\n

The simplest and fastest way to get the current timestamp in milliseconds is to use System.currentTimeMillis(). This method exists in Java and is fully available in Kotlin.

\n\n
val currentTimestampMillis = System.currentTimeMillis()\nprintln("Current timestamp (ms): $currentTimestampMillis")
\n\n

Pros:

\n
    \n
  • Lightning fast (native call).
  • \n
  • No additional imports required.
  • \n
  • Works on all Android versions.
  • \n
\n\n

Cons:

\n
    \n
  • Returns only milliseconds.
  • \n
  • Tied to the device's system time (the user can change it).
  • \n
  • Does not account for time zones — it's just a number.
  • \n
\n\n

This method is ideal for measuring intervals (e.g., code execution time) or when you need a quick number for comparison.

\n\n

Method 2: Instant.now() — the modern approach (Java 8+)

\n\n

Starting from API level 26 (Android 8.0) and above, you can use the java.time.Instant class. This is part of the modern time API that solves many problems of the old classes.

\n\n
import java.time.Instant\n\nval instant = Instant.now()\nval timestampMillis = instant.toEpochMilli()\nval timestampSeconds = instant.epochSecond\n\nprintln("Instant: $instant")\nprintln("Timestamp (ms): $timestampMillis")\nprintln("Timestamp (sec): $timestampSeconds")
\n\n

Pros:

\n
    \n
  • Clean, modern API.
  • \n
  • Easily convertible to other formats.
  • \n
  • Works with UTC by default.
  • \n
\n\n

Cons:

\n
    \n
  • Requires API 26+ (for older devices, you need desugaring or the ThreeTenABP library).
  • \n
\n\n

If your app targets modern devices, Instant.now() is the best choice for getting a timestamp.

\n\n

Method 3: Calendar — deprecated but persistent

\n\n

The java.util.Calendar class is an old way that is still

Blogs

Book Recommendations