Update Time And Date Using Button in Android

Introduction

 
In this article, I explain the time and date classes in Android, how to use the Date and Time Classes in Android and how to work with these classes. You can easily understand it using the following instructions.
 
Step 1
 
As usual, create a new project file as in the following.
 
newTimeUpdater.jpg
 
Step 2
 
Open the "activity_main.xml" file and update it with the following code:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.     <Button  
  11.         android:id="@+id/button1"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_alignParentLeft="true"  
  15.         android:layout_alignParentRight="true"  
  16.         android:text="@string/update_btn" />  
  17. </RelativeLayout> 
Step 3
 
Open the "MainActivity.java" file and update it with the following code:
  1. package com.example.androidthirdapp;  
  2. import java.util.Date;  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. public class MainActivity extends Activity {  
  9.       Button btn;  
  10.       @Override  
  11.       protected void onCreate(Bundle savedInstanceState) {  
  12.             super.onCreate(savedInstanceState);  
  13.             setContentView(R.layout.activity_main);  
  14.             btn = (Button) findViewById(R.id.button1);  
  15.             //Onclick listener  
  16.             btn.setOnClickListener(new View.OnClickListener() {  
  17.             //Onclick method  
  18.             public void onClick(View v) {  
  19.             //Call updateTime() when button is clicked  
  20.             updateTime();  
  21.             }  
  22.             });  
  23.       }  
  24.       //Method definition starts here with no return type  
  25.       public void updateTime() {  
  26.       //Setting button text with current time  
  27.       //java.util.Date() method is used to get the current time  
  28.       btn.setText(new Date().toString());  
  29.       }  
  30.       @Override  
  31.       public boolean onCreateOptionsMenu(Menu menu) {  
  32.             // Inflate the menu; this adds items to the action bar if it is present.  
  33.             getMenuInflater().inflate(R.menu.main, menu);  
  34.             return true;  
  35.       }  

Step 4
 
Now create a new Java file named "CurrentTimeActivity.java" and use the following code in it:
  1. package com.example.androidthirdapp;  
  2. import java.util.Date;  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. public class CurrentTimeActivity extends Activity {  
  8.       Button btn;  
  9.       public void onCreate(Bundle savedInstanceState) {  
  10.             super.onCreate(savedInstanceState);  
  11.             setContentView(R.layout.activity_main);  
  12.             btn = (Button) findViewById(R.id.button1);  
  13.             updateTime();  
  14.             btn.setOnClickListener(new View.OnClickListener() {  
  15.             public void onClick(View v) {  
  16.             updateTime();  
  17.             }  
  18.             });  
  19.             }  
  20.       //Method definition starts here with no return type  
  21.       public void updateTime() {  
  22.       //Setting button text with current time  
  23.       //java.util.Date() method is used to get the current time  
  24.       btn.setText(new Date().toString());  
  25.       }  

Step 5
 
Open the "AndroidManifest.xml" file and update it with the following code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.androidthirdapp"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.     <uses-sdk  
  7.         android:minSdkVersion="8"  
  8.         android:targetSdkVersion="17" />  
  9.     <application  
  10.         android:allowBackup="true"  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name"  
  13.         android:theme="@style/AppTheme" >  
  14.         <activity  
  15.             android:name="com.example.androidthirdapp.MainActivity"  
  16.             android:label="@string/app_name" >  
  17.             <intent-filter>  
  18.                 <action android:name="android.intent.action.MAIN" />  
  19.                 <category android:name="android.intent.category.LAUNCHER" />  
  20.             </intent-filter>  
  21.         </activity>  
  22.     </application>  
  23. </manifest> 
Step 6
 
See the output.
 
Update Button
 
updateTime.jpg
 
Current Time
 
currentTim.jpg