android kotlin - Get app first install time

MainActivity.kt

package com.example.jetpack

import android.content.pm.PackageInfo
import android.os.Bundle
import android.text.format.DateUtils
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.text.SimpleDateFormat


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // display app package name in text view
        textView.text = "Package Name $packageName"

        // get the package info instance
        val packageInfo: PackageInfo = packageManager.getPackageInfo(packageName, 0)

        // get app first install time programmatically
        val installedTime: Long = packageInfo.firstInstallTime

        // display app first install time as formatted string
        textView.append("\n\nApp first install time" +
                "\n${installedTime.toFormattedDate}")


        // returns a string describing 'time' as a time relative to 'now'.
        val ago = DateUtils.getRelativeTimeSpanString(
            installedTime, // time
            System.currentTimeMillis(), // now
            DateUtils.MINUTE_IN_MILLIS // minResolution
        )

        // app first install ago
        textView.append("\n\nApp first install : $ago")
    }
}


// extension property to get formatted date string from long value
val Long.toFormattedDate:String
    get() {
        return SimpleDateFormat("dd MMM yyyy hh:mm:ss a").format(this)
    }
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E5E4E2"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="16dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#191970"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android kotlin tutorials