Date and Time sample program in Android

By: Ashley Emailed: 1585 times Printed: 2037 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This sample android program shows you how to use Date Picker and Time Picker in Android. In this program two buttons will be shown. When you click on the first button, the date picker widget is shown and once you select the date, the selected date will be displayed on the textview. When you click on the second button, a Time Picker widget will be shown and the selected time will be shown on the text view.

The DateTimeDemo1.java file is as follows:

package com.javasamples;

import android.app.Activity;
import android.os.Bundle;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.TextView;
import java.text.DateFormat;
import java.util.Calendar;

public class DateTimeDemo1 extends Activity {
	DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();
	TextView lblDateAndTime;
	Calendar myCalendar = Calendar.getInstance();

	DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
	myCalendar.set(Calendar.YEAR, year);
	myCalendar.set(Calendar.MONTH, monthOfYear);
	myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
	updateLabel();
	}
	};

	TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
		myCalendar.set(Calendar.MINUTE, minute);
		updateLabel();
	}
	};

	private void updateLabel() {
		lblDateAndTime.setText(fmtDateAndTime.format(myCalendar.getTime()));
	}

	@Override
	public void onCreate(Bundle icicle) {
	super.onCreate(icicle);
	setContentView(R.layout.main);
	lblDateAndTime = (TextView) findViewById(R.id.lblDateAndTime);
	Button btnDate = (Button) findViewById(R.id.btnDate);
	btnDate.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			new DatePickerDialog(DateTimeDemo1.this, d, myCalendar
					.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
					myCalendar.get(Calendar.DAY_OF_MONTH)).show();
		}
	});

	Button btnTime = (Button) findViewById(R.id.btnTime);
	btnTime.setOnClickListener(new View.OnClickListener() {
		public  void onClick(View v) {
			new TimePickerDialog(DateTimeDemo1.this, t, myCalendar
					.get(Calendar.HOUR_OF_DAY), myCalendar
					.get(Calendar.MINUTE), true).show();
		}
	});

	updateLabel();
	}// onCreate
} // class

The output of this program will be as shown in the android emulator below.

The main.xml file in your res/layout folder is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android:id="@+id/widget28"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<TextView
		android:id="@+id/lblDateAndTime"
		android:layout_width="fill_parent"
		android:layout_height="47px"
		android:background="#ff000099"
		android:textStyle="bold">
	</TextView>
	<Button
		android:id="@+id/btnDate"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="Set the Date">
	</Button>
	<Button
		android:id="@+id/btnTime"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="Set the Time">
	</Button>
</LinearLayout>



Android Home | All Android Tutorials | Latest Android Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

i am getting force closed with this code...
and
How to check reason of force close in android program


View Tutorial          By: kamlapati at 2011-07-16 06:06:31
2. View Comment

I am assuming you are using eclipse for your android development. If you are using eclipse, clcik on the 'DDMS' tab on the right top, and you will see all the Debug errors and reasons there.

View Tutorial          By: Ashley at 2011-07-18 00:34:04
3. View Comment

nice code. code is running perfectly. but my problem is whenever i select date &time comes together. i want to show date and time separately. please help.


thanks


View Tutorial          By: Avinash at 2013-01-28 07:30:19

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ashley
Is autodiscover CNAME a must in Office 365
Directory Synchronization tool in Office 365
Configure Outlook Manually for Office 365
Review: Ruggear - The world's most Rugged Phone in Singapore
Solution to error: unable to open connection to server due to security error
Making your Alias email as 'Send As' in Office 365
Steps to Create Shared Mailbox in office 365
xxxx cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.
The user's Outlook Live mailbox is full.
Outlook Live couldn't connect to the IMAP server.
The user's mailbox has already been migrated.
An Outlook Live mailbox wasn't found for the user.
Outlook Live couldn't sign in to the user account on the IMAP messaging system.
Android Send SMS and Make a phone call in Android Application
Android Preferences - Using Preferences in Android Tutorial

More Tutorials in Android
Getting Started with Android
Solution to error: unable to open connection to server due to security error
Android Preferences - Using Preferences in Android Tutorial
Android Send SMS and Make a phone call in Android Application
Android Emulator
Intent in Android to call one activity from another activity.
Animation sample program in Android
Another Animation sample program in Android
ArrayAdapter sample program in Android
Spinner sample program in Android
GridView sample program in Android
Gallery sample program in Android
Date and Time sample program in Android
Progress bar and downloading a file sample program in Android
Reading a file sample program in Android

More Latest News
Most Viewed Articles (in Android )
Reading and Writing a file to SD card sample program in Android
Progress bar and downloading a file sample program in Android
ArrayAdapter sample program in Android
Intent in Android to call one activity from another activity.
Android Send SMS and Make a phone call in Android Application
Reading a file sample program in Android
Date and Time sample program in Android
Spinner sample program in Android
Animation sample program in Android
Solution to error: unable to open connection to server due to security error
Getting Started with Android
Gallery sample program in Android
Another Animation sample program in Android
GridView sample program in Android
AlertDialog sample program in Android
Most Emailed Articles (in Android)
AlertDialog sample program in Android
Error generating R.java from manifest
Gallery sample program in Android
Progress bar and downloading a file sample program in Android
Reading and Writing a file to SD card sample program in Android
Animation sample program in Android
Spinner sample program in Android
Intent in Android to call one activity from another activity.
Android Emulator
Android Send SMS and Make a phone call in Android Application
Solution to error: unable to open connection to server due to security error
Getting Started with Android
Another Animation sample program in Android
Reading a file sample program in Android
Android Preferences - Using Preferences in Android Tutorial