Open In App

How to Get Current Time and Date in Android using Jetpack Compose?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Many times in android applications we have to capture the current date and time within our android application so that we can update our data according to that. In this article, we will look at How to get the Current Time and Date in our android application using Jetpack Compose

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

While choosing the template, select Empty Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with MainActivity.kt file 

Navigate to app > kotlin+java > {package-name} > MainActivity.kt file and add the below code to it. Comments are added in the code to get to know in detail. 

Kotlin
package com.geeksforgeeks.demo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import java.text.SimpleDateFormat
import java.util.*

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            DisplayTxtClock()
        }
    }
}

// composable function to get current date and time
@Composable
fun DisplayTxtClock() {
    // creating a column
    Column(
        Modifier
            .fillMaxSize()
            .fillMaxHeight()
            .fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {

        Text(
            text = "Current Date and\nTime in Android",
            modifier = Modifier.fillMaxWidth(),
            textAlign = TextAlign.Center,
            color = Color.Black,
            fontWeight = FontWeight.Bold,
            fontSize = 20.sp,
        )

        Spacer(modifier = Modifier.height(20.dp))

        // simple date format
        val sdf = SimpleDateFormat("'Date: 'dd-MM-yyyy '\nTime: 'HH:mm:ss z", Locale.getDefault())

        // current date and time and calling a simple date format
        val currentDateAndTime = sdf.format(Date())

        Text(
            text = currentDateAndTime,
            modifier = Modifier.fillMaxWidth(),
            textAlign = TextAlign.Center,
            color = Color.Black,
            fontWeight = FontWeight.Bold,
            fontSize = 16.sp,
        )
    }
}


Output:

date-time-compose



Article Tags :

Similar Reads