Home » Android » Use the date selector in Android

Use the date selector in Android


    After
  • hits, a Dialog appears. Click Dialog to select the date and time:

  • select date:


//Calender is to obtain the current system time 
Calendar calendar = Calendar.getInstance ();
Calendar.setTimeInMillis (System.currentTimeMillis ());
int year = calendar.get (Calendar.YEAR);
int monthOfyear = calendar.get (Calendar.MONTH);
int dayOfMonth = calendar.get (Calendar.DAY_OF_MONTH);
// pops up a new dialog box for a new time to select
DatePickerDialog, datePickerDialog = new, DatePickerDialog (MainActivity.this, new, DatePickerDialog.OnDateSetListener () {
@Override
public, void, onDateSet (DatePicker, view, int, year, int, monthOfYear, int, dayOfMonth) {
String, DateTime = (String.format (,%d-%d-%d, , year, monthOfYear+1, dayOfMonth)) // converts the date format to
// sets the selected time to Text of TextView
TextView Date = (TextView) findViewById (R.id.tvDate);
Date.setText (DateTime);
}
}, year, monthOfyear, dayOfMonth);
DatePickerDialog.show ();


  • select time:


Calendar calendar = Calendar.getInstance ();
int hourOfDay = calendar.get (Calendar.HOUR_OF_DAY);
int minute = calendar.get (Calendar.MINUTE);
new TimePickerDialog (MainActivity.this, new, TimePickerDialog.OnTimeSetListener () {
@Override
public, void, onTimeSet (TimePicker, view, int, hourOfDay, int, minute) {
String Datetime = (String.format (,%d:%d, , hourOfDay, minute)) // converts time format to
// sets the selected time to Text of TextView
TextView Time = (TextView) findViewById (R.id.tvTime);
Time.setText (Datetime);
}
}, hourOfDay, minute, true).Show ();

Latest