Blog
Wild & Free Tools

Convert Unix Timestamp in Kotlin (Android) and Swift (iOS)

Last updated: April 2026 6 min read

Table of Contents

  1. Kotlin (Android)
  2. Swift (iOS)
  3. Mobile Specific
  4. Frequently Asked Questions

Mobile developers hit Unix timestamps constantly because almost every API returns them. The conversion in Kotlin (Android) and Swift (iOS) is straightforward but each language has its own conventions and a few gotchas around milliseconds vs seconds that bite people repeatedly.

This guide covers both languages with the patterns you need for production mobile code.

Kotlin Unix Timestamp Conversion

Kotlin on Android can use either the legacy java.util.Date (works on all Android versions) or the modern java.time API (Android API 26+ or with desugaring). The modern API is much better.

import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter

fun main() {
    // Current Unix timestamp
    val nowSeconds = Instant.now().epochSecond
    val nowMillis = Instant.now().toEpochMilli()

    // From Unix seconds to a Date string
    val ts = 1711000000L
    val instant = Instant.ofEpochSecond(ts)
    val zoned = instant.atZone(ZoneId.of("America/New_York"))
    val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
    val formatted = zoned.format(formatter)

    // From milliseconds (more common in Android APIs)
    val tsMs = 1711000000123L
    val instant2 = Instant.ofEpochMilli(tsMs)

    // Going back to Unix
    val seconds = instant.epochSecond
    val millis = instant.toEpochMilli()
}

The Android milliseconds gotcha

Android APIs (especially older ones like Calendar and Date) almost always work in milliseconds. If your backend returns seconds, you have to multiply by 1000 before passing to Android Date constructors. The opposite mistake — passing milliseconds where seconds are expected — produces dates in the year 56000.

Kotlin extensions for cleaner code

fun Long.toUtcDateString(): String {
    return Instant.ofEpochSecond(this)
        .atZone(ZoneOffset.UTC)
        .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
}

// Usage
val display = 1711000000L.toUtcDateString()
Sell Custom Apparel — We Handle Printing & Free Shipping

Swift Unix Timestamp Conversion

Swift uses Date from Foundation. Internally, Date stores seconds since January 1, 2001 (Apple's epoch), but the public API exposes Unix time conversion methods so you rarely deal with the internal value.

import Foundation

// Current Unix timestamp
let nowSeconds = Date().timeIntervalSince1970
let nowSecondsInt = Int(nowSeconds)
let nowMillis = Int(nowSeconds * 1000)

// From Unix seconds
let ts: TimeInterval = 1711000000
let date = Date(timeIntervalSince1970: ts)

// Format the date
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
formatter.timeZone = TimeZone(identifier: "America/New_York")
let formatted = formatter.string(from: date)

// From milliseconds
let tsMs: TimeInterval = 1711000000123
let date2 = Date(timeIntervalSince1970: tsMs / 1000)

// Going back
let seconds = date.timeIntervalSince1970
let millis = date.timeIntervalSince1970 * 1000

Swift gotchas

1. Date.timeIntervalSince1970 is a TimeInterval (which is a Double), not an Int. Cast to Int with Int(date.timeIntervalSince1970) before sending over the wire.

2. DateFormatter is expensive to create. Cache instances rather than allocating one per row. The cache is not thread-safe across threads in older iOS versions.

3. The default time zone is the user's device locale. Set it explicitly if you want UTC or a specific zone.

Swift extension for clean code

extension TimeInterval {
    func toUtcString() -> String {
        let date = Date(timeIntervalSince1970: self)
        let f = DateFormatter()
        f.dateFormat = "yyyy-MM-dd HH:mm:ss"
        f.timeZone = TimeZone(identifier: "UTC")
        return f.string(from: date)
    }
}

// Usage
let display = (1711000000 as TimeInterval).toUtcString()

Mobile-Specific Considerations

Battery and performance

Date formatting is surprisingly expensive on mobile, especially in tight loops (like rendering a table view). Cache formatter instances. For RecyclerView in Android or UITableView in iOS, format dates once when binding to the cell, not every redraw.

Locale-aware display

Most mobile users expect dates in their device locale, not your developer locale. DateTimeFormatter.ofLocalizedDate in Java/Kotlin and DateFormatter.dateStyle in Swift respect user preferences. Reserve fixed format strings for storage and machine-readable output, not for user display.

Background tasks and time accuracy

Mobile devices put apps to sleep aggressively. The system clock continues running, but your app may miss seconds or minutes when scheduled timers fire late. For accurate elapsed time measurements, use monotonic clocks (SystemClock.elapsedRealtimeNanos() on Android, CFAbsoluteTimeGetCurrent() with care on iOS) instead of subtracting Unix timestamps.

Server-side vs device clock

Mobile users adjust their clocks all the time — manually, via timezone changes, or by traveling. For anything time-sensitive (auth tokens, anti-cheat, sync conflict resolution), prefer server-provided timestamps over device clocks. The device clock is fine for display but unreliable for security.

For one-off value verification on a phone, the free Unix timestamp converter works in any mobile browser.

Try It Free — No Signup Required

Runs 100% in your browser. No data is collected, stored, or sent anywhere.

Open Free Unix Timestamp Converter

Frequently Asked Questions

How do I convert a Unix timestamp in Kotlin?

Use Instant.ofEpochSecond(timestamp) for seconds or Instant.ofEpochMilli(timestamp) for milliseconds. The Instant class is part of java.time which works on Android API 26+ or with desugaring on older Android versions. Convert to ZonedDateTime when you need to display in a specific zone.

How do I convert a Unix timestamp in Swift?

Date(timeIntervalSince1970: timestamp) creates a Date from Unix seconds. For milliseconds, divide by 1000 first: Date(timeIntervalSince1970: ms / 1000). The result is a Date you can format with DateFormatter.

Why does Android Date use milliseconds?

Android inherits the convention from Java, where the original java.util.Date class was designed in 1995 to support sub-second precision. The decision propagated through the Android APIs, so Calendar.getTimeInMillis(), System.currentTimeMillis(), and most Android timestamp APIs use milliseconds.

How do I get the current Unix timestamp in Kotlin?

Instant.now().epochSecond returns the current Unix timestamp in seconds. Instant.now().toEpochMilli() returns milliseconds. System.currentTimeMillis() also returns milliseconds and is available on every Android version without requiring API 26.

How do I get the current Unix timestamp in Swift?

Date().timeIntervalSince1970 returns the current Unix timestamp as a TimeInterval (Double) in seconds. Cast to Int if you need an integer: Int(Date().timeIntervalSince1970).

Why does my date format look different in different locales?

DateFormatter and DateTimeFormatter use the device's locale by default, which means Spanish users see Spanish month names, German users see different date orders, etc. For machine-readable storage, set the locale explicitly to en_US_POSIX. For user display, the default locale is what you want.

Launch Your Own Clothing Brand — No Inventory, No Risk