Package com.example.demo10;
Import Java.util.Calendar;
Import android.support.v7.app.ActionBarActivity;
Import Android.app.DatePickerDialog;
Import Android.app.DatePickerDialog.OnDateSetListener;
Import Android.app.TimePickerDialog;
Import Android.app.TimePickerDialog.OnTimeSetListener;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.DatePicker;
Import Android.widget.DatePicker.OnDateChangedListener;
Import Android.widget.TimePicker;
Import Android.widget.TimePicker.OnTimeChangedListener;
public class Mainactivity extends Actionbaractivity {
Private Timepicker Timepicker;
Private DatePicker DatePicker;
Private Calendar cal;
private int year;
private int month;
private int day;
private int hour;
private int minute;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Get the object for the calendar
Cal=calendar.getinstance ();
Get the corresponding month-day time and minute
Year=cal.get (calendar.year);
Month=cal.get (calendar.month) +1;
Day=cal.get (Calendar.day_of_month);
Hour=cal.get (Calendar.hour_of_day);
Minute=cal.get (Calendar.minute);
Settitle (year+ "-" +month+ "-" +day+ "-" +hour+ ":" +minute ");
Datepicker= (DatePicker) Findviewbyid (R.id.datepicker);
Timepicker= (Timepicker) Findviewbyid (R.id.timepicker);
DatePicker initialization
Datepicker.init (Year,cal.get (calendar.month), Day,new Ondatechangedlistener () {
@Override
public void ondatechanged (DatePicker view, int year, int monthofyear,
int DayOfMonth) {
TODO auto-generated Method Stub
Settitle (year+ "-" + (monthofyear+1) + "-" +dayofmonth);
}
});
Timepicker.setontimechangedlistener (New Ontimechangedlistener () {
@Override
public void ontimechanged (timepicker view, int hourofday, int minute) {
TODO auto-generated Method Stub
Settitle (hourofday+ ":" +minute);
}
});
New Datepickerdialog (This,new Ondatesetlistener () {
@Override
public void Ondateset (DatePicker view, int year, int monthofyear,
int DayOfMonth) {
TODO auto-generated Method Stub
Settitle (year+ "-" + (monthofyear+1) + "-" +dayofmonth);
}
}, year, Cal.get (Calendar.month), day). Show ();
New Timepickerdialog (This,new Ontimesetlistener () {
@Override
public void Ontimeset (timepicker view, int hourofday, int minute) {
TODO auto-generated Method Stub
Settitle (hourofday+ ":" +minute);
}
}, hour, minute,true). Show ();
}
}
Simple use of Timepicker and DatePicker in Android