Skip to content

Timestamp Converter — Java source

Convert Unix epoch timestamps to human-readable dates and back. See seconds, milliseconds, ISO 8601, UTC, local and relative time at once.

This is the Java implementation — the same logic the interactive tool runs, in a shareable, citable form.

// timestamp — Unix epoch (seconds & milliseconds) <-> ISO 8601 / UTC / local strings and relative time ("3 hr ago" / "in 2 day"). Java (17+) port of src/tools/TimestampConverter.tsx — same logic as this dir's typescript.ts; fromISO parsing omitted for the 80-line budget (see typescript.ts / python.py).
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

final class Timestamp {

    // Fixed formatters: ISO with milliseconds ("...T07:00:00.000Z") and the
    // toUTCString() shape ("Wed, 14 Jun 2017 07:00:00 GMT"), both pinned to
    // English weekday/month names like the browser renders them.
    static final DateTimeFormatter ISO =
        DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH).withZone(ZoneOffset.UTC);
    static final DateTimeFormatter UTC =
        DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.ENGLISH).withZone(ZoneOffset.UTC);
    static final DateTimeFormatter LOCAL = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    // Human relative time: bucket |now - t| into sec/min/hr/day/month/yr
    // with the island's 60s / 60m / 24h / 30d / 365d thresholds, round to
    // the nearest whole unit, then sign it ("ago" past / "in" future).
    static String relativeTime(long ms, long nowMs) {
        long diff = ms - nowMs;
        double a = Math.abs(diff) / 1000.0;
        long n;
        String unit;
        if (a < 60) { n = Math.round(a); unit = "sec"; }
        else if (a < 3600) { n = Math.round(a / 60); unit = "min"; }
        else if (a < 86400) { n = Math.round(a / 3600); unit = "hr"; }
        else if (a < 86400 * 30) { n = Math.round(a / 86400); unit = "day"; }
        else if (a < 86400 * 365) { n = Math.round(a / (86400 * 30)); unit = "month"; }
        else { n = Math.round(a / (86400 * 365)); unit = "yr"; }
        return diff < 0 ? n + " " + unit + " ago" : "in " + n + " " + unit;
    }

    // Render every form the tool exposes from "now" — the island's row
    // table: epoch (s), epoch (ms), ISO 8601, UTC string, local string,
    // relative time.
    public static void main(String[] args) {
        Instant now = Instant.now();
        long ms = now.toEpochMilli();
        System.out.println("Epoch (s):  " + ms / 1000);
        System.out.println("Epoch (ms): " + ms);
        System.out.println("ISO 8601:  " + ISO.format(now));
        System.out.println("UTC:       " + UTC.format(now));
        System.out.println("Local:     " + LOCAL.format(now.atZone(ZoneId.systemDefault())));
        System.out.println("Relative:  " + relativeTime(ms, ms));
        System.out.println("In 3 hr:   " + relativeTime(ms + 3L * 3600 * 1000, ms));
    }
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →