About
Home > Tutorials Home > Android > DateTime Picker Control in Android

DateTime Picker Control in Android

Published on October 17, 2019 7:58:50 PM

We are an online tutoring company that has laid its foundation in utilizing Multiple Intelligence methodology to understand specific needs and learning styles of a student.So that we patch a particular student to a tutor having a compatible teaching style. We have devised a smart online tutoring system to give you measurable results.Schools, cramped with students, fail to give your child the edge, the motivation and the right teacher guidance to help him find the true potential. We at Eonlinetutors.com make sure that your child gets the perfect learning environment to outperform.




Step 1 : Open the Eclipse and create a new Android project using Blank Activity application template. Give the application name "DateTime Picker Example in Android".

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical">
	<TextView
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="@string/hello_world"
		android:id="@+id/tv" />
		
		<Button
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:id="@+id/date"
			android:text="Set Date">
		</Button>
		
		<Button
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:id="@+id/time"
			android:text="Set Time" />
</LinearLayout>

Step 2 : Copy the Following Code into your .Java File.

package com.example.datatimepickerexample;

import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity {

	private TextView tv;
	private Button date;
	private Button time;
	Calendar c=Calendar.getInstance();
	DatePickerDialog.OnDateSetListener dataset;
	TimePickerDialog.OnTimeSetListener timeset;
	private int cyear;
	private int cmonth;
	private int cdate;
	private int chour;
	private int cminute;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		tv=(TextView)findViewById(R.id.tv);
		date=(Button)findViewById(R.id.date);
		time=(Button)findViewById(R.id.time);

		date.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				new DatePickerDialog(MainActivity.this,dataset, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
			}
		});
		
		time.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				new TimePickerDialog(MainActivity.this,timeset, c.get(Calendar.HOUR), c.get(Calendar.MINUTE),true).show();
			}
		});

		Calendar cc=Calendar.getInstance();
		cyear=Calendar.YEAR;
		cmonth=Calendar.MONTH;
		cdate=Calendar.DAY_OF_MONTH;
		chour=Calendar.HOUR;
		cminute=Calendar.MINUTE;

		dataset=new OnDateSetListener()
		{
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
			{
				cyear=year;
				cmonth=monthOfYear;
				cdate=dayOfMonth;
				updatedata();
			}
		};

		timeset=new OnTimeSetListener()
		{
			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute)
			{
				chour=hourOfDay;
				cminute=minute;
				updatedata();
			}
		};
	}
	
	protected void updatedata()
	{
		StringBuffer sb=new StringBuffer();
		sb.append("Date is:-").append(cyear).append("-").append(cmonth+1).append("-").append(cdate).append("Time is:-").append(chour).append(":").append(cminute);
		tv.setText(sb.toString());
	}
}
ALL COMMENTS (2)
Abhishek Bedre 2017-03-28 17:25:15
I have implemented my own "jarvis"....thanks to this example.        
2019-01-03 13:22:19
It is working fine in desktop but not working in android mobile