Get current date and time in android programmatically

Dynamically Display android mobile device current date + time in android application using programming coding.

Some of android application app developer or application user needs to know about the current date and time setting up in android mobile phone device. So my in this tutorial i am getting the current device date and time and setting them on TextView on button click. So here is the complete step by step tutorial for Get current date and time in android programmatically.

android-project-download-code-button

How to Get current date and time in android programmatically.

Code for MainActivity.java file.

 package com.currentdatetime_android_examples.com;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 Calendar calander;
 SimpleDateFormat simpledateformat;
 String Date;
 TextView DisplayDateTime;
 Button BTN;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 DisplayDateTime = (TextView)findViewById(R.id.textView1);
 
 BTN = (Button)findViewById(R.id.button1);
 
 calander = Calendar.getInstance();
 simpledateformat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
 Date = simpledateformat.format(calander.getTime());
 
 BTN.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 DisplayDateTime.setText(Date);
 
 }
 }); 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.currentdatetime_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="168dp"
 android:text="Date + Time display here"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="36dp"
 android:text="Get current date and time in android programmatically" />

</RelativeLayout>

Screenshots:

Get current date and time in android programmatically

Click here to download Get current date and time in android programmatically project.

2 Comments

  1. Congratulations, well. But I would like to do somethig as I can get the time dynamically, because like showing the time, it is showing the current time, but it freezes if I do not reload the activity, so how do I get the dynamics of time without updating The view as textclock, but the textclok is only to show the time not to get time dynamically

    Thanks,

  2. Hey LILIANA you can try CountDownTimer for this like :-
    CountDownTimer newtimer = new CountDownTimer(1000000000, 1000) {

    public void onTick(long millisUntilFinished) {
    Calendar c = Calendar.getInstance();
    textView.setText(c.get(Calendar.HOUR)+”:”+c.get(Calendar.MINUTE)+”:”+c.get(Calendar.SECOND));
    }
    public void onFinish() {

    }
    };
    newtimer.start();

Leave a Reply

Your email address will not be published. Required fields are marked *