Date and Time sample program in Android

By: Ashley Printer Friendly Format    

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>




Ask a Question



Most Viewed Articles (in Android )

Latest Articles (in Android)

Comment on this tutorial

Ask a Question

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. i am getting force closed with this code...

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

2. I am assuming you are using eclipse for your andro
View Tutorial          By: Ashley at 2011-07-18 00:34:04

3. Perfect thanks... This <a href="http://www
View Tutorial          By: Meghna at 2013-07-22 20:17:31

4. Nice tutorial! :D Hey I am a real newbie but could
View Tutorial          By: James Bond at 2012-09-23 18:29:35

5. nice code. code is running perfectly. but my probl
View Tutorial          By: Avinash at 2013-01-28 07:30:19

6. very nice tutorial you can also check this one

View Tutorial          By: pavan at 2013-05-10 08:39:56

7. very nice tutorial for date picker in android

View Tutorial          By: pavan deshpande at 2013-06-24 11:33:56