Date and Time sample program in Android
 


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>


 
 
 
 
 
COPYRIGHT © 2017 java-samples.com. ALL RIGHTS RESERVED.