How to get current time and date in Android

0 votes
How can I get the current time and date in an Android app?
Nov 22, 2022 in Android by Edureka
• 12,690 points
596 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

 To get the current Time and Date in our android application, follow the below steps:

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio.

Step 2: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the code below. Comments are added in the code to get to know in detail. 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/idRLContainer"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">


<!--on below line we are creating

a text for our app-->

<TextView

android:id="@+id/idTVHeading"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_above="@id/idTVCurrent"

android:layout_centerInParent="true"

android:layout_margin="20dp"

android:gravity="center"

android:padding="10dp"

android:text="Current Date and Time in Android"

android:textAlignment="center"

android:textColor="@color/black"

android:textSize="20sp"

android:textStyle="bold" />


<!--on below line we are creating a text view-->

<TextView

android:id="@+id/idTVCurrent"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:layout_margin="20dp"

android:gravity="center"

android:padding="10dp"

android:text="Time"

android:textAlignment="center"

android:textColor="@color/black"

android:textSize="20sp"

android:textStyle="bold" />


</RelativeLayout>

Step 3: Working with the MainActivity file 

Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail. 

package com.gtappdevelopers.kotlingfgproject;
import android.os.Bundle;

import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.text.SimpleDateFormat;

import java.util.Date;


public class MainActivity extends AppCompatActivity {


// on below line we are creating variables.

private TextView currentTV;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


// on below line we are initializing our variables.

currentTV = findViewById(R.id.idTVCurrent);

// on below line we are creating and initializing

// variable for simple date format.

SimpleDateFormat sdf = new SimpleDateFormat("'Date\n'dd-MM-yyyy '\n\nand\n\nTime\n'HH:mm:ss z");

// on below line we are creating a variable

// for current date and time and calling a simple date format in it.

String currentDateAndTime = sdf.format(new Date());

// on below line we are setting current

// date and time to our text view.

currentTV.setText(currentDateAndTime);

}

}

Try it!!! hope this helps you

answered Nov 23, 2022 by Edureka
• 13,620 points

edited Mar 5

Related Questions In Android

0 votes
0 answers

How to get the device's IMEI/ESN programmatically in android?

I want to use the IMEI to ...READ MORE

Nov 16, 2022 in Android by Edureka
• 12,690 points
1,400 views
0 votes
0 answers

How do I get the current GPS location programmatically in Android?

I need to get my current location ...READ MORE

Nov 23, 2022 in Android by Ashwini
• 5,430 points
785 views
0 votes
0 answers
0 votes
0 answers

How to make grid view scroll horizontally not vertically in android?

I use a flexible Grid View. means ...READ MORE

Sep 21, 2022 in Android by Edureka
• 13,620 points
697 views
0 votes
1 answer

How to turn on front flash light programmatically in Android?

For this problem you should: Check whether the ...READ MORE

answered Nov 8, 2022 in Android by Edureka
• 12,690 points
3,580 views
0 votes
1 answer

How to display Toast in Android?

Use the show() function of the Toast ...READ MORE

answered Nov 8, 2022 in Android by Edureka
• 12,690 points
1,566 views
0 votes
1 answer

How to get current time and date in Android

In order to get the current date ...READ MORE

answered Feb 23, 2022 in Others by Aditya
• 7,680 points
2,276 views
0 votes
1 answer

How can one day be added to a date?

One possible solution could be using calendar ...READ MORE

answered Jun 8, 2018 in Java by Daisy
• 8,140 points
708 views
0 votes
1 answer

Get the Current time in Python

To get the current date and time ...READ MORE

answered Oct 3, 2018 in Python by SDeb
• 13,300 points
779 views
0 votes
1 answer

Python: Get current system/local time

You can use the datetime module for this. ...READ MORE

answered May 8, 2019 in Python by Suman
903 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP