DatePicker and TimePicker

Android DatePicker is a widget to select date. It allows you to select date by day, month and year. Like DatePicker, android also provides TimePicker to select time.

In android, DatePicker is a control that will allow users to select the date by a day, month and year in our application user interface. If we use DatePicker in our application, it will ensure that the users will select a valid date. Let's have DatePicker example in xml

<DatePicker
    android:id="@+id/datePicker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:datePickerMode="spinner"/>

Above xml result will be like:
Common Attribute Description
android:calendarViewShown value either true or false
android:spinnersShown value either true or false
android:datePickerMode values spinner or calender

Let's see and DatePicker example, first create date_picker_activity.xml

date_picker_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <DatePicker
        android:id="@+id/datePicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:calendarViewShown="true"
        android:datePickerMode="spinner"
        android:spinnersShown="false" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btnDate"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"
        android:onClick="showDate"
        android:text="Show Selected Date"/>
</RelativeLayout>

Now create showDate(View view) method in desire activity as below

public void showDate(View view) {
        DatePicker picker = findViewById(R.id.datePicker);
        int year = picker.getYear();
        int month = picker.getMonth() + 1;
        int day = picker.getDayOfMonth();

        Toast.makeText(this, "Your Selected Date: " + year + "/" + month + "/" + day, Toast.LENGTH_SHORT).show();
}

Now we show you example of TimePicker, change in above layout xml android:spinnersShown="true" and android:calendarViewShown="false"

<TimePicker
    android:id="@+id/timePicker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/datePicker"
    android:timePickerMode="clock"/>

Now create method showTime(View view) to show selected time

public void showTime(View view) {
        TimePicker picker = findViewById(R.id.timePicker);
        int hour = picker.getHour();
        int minute = picker.getMinute();

        String amOrPm = picker.is24HourView() ? "" : (hour < 11 && minute < 59 ? "AM" : "PM");

        Toast.makeText(this, "Your Selected Time: " + hour + ":" + minute + " " + amOrPm, Toast.LENGTH_SHORT).show();
}

DatePickerDialog and TimePickerDialog

Android DatePickerDialog is a widget to select date. It allows you to select date by day, month and year. Like DatePickerDialog, android also provides TimePickerDialog to select time.

In android, DatePickerDialog is a control that will allow users to select the date by a day, month and year from dialog. If we use DatePickerDialog in our application.

DatePickerDialog and TimePickerDialog will look like below image


For DatePickerDialog call below method and modify accordingly

public void chooseDate(View view) {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    final int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dialog = new DatePickerDialog(DatePickerActivity.this,
        new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Toast.makeText(DatePickerActivity.this, "Your Selected Date: " + year + "/" + (monthOfYear + 1) + "/" + dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        }, year, month, day);

    dialog.show();
}

For TimePickerDialog call below method and modify accordingly

public void chooseTime(View view) {
    Calendar cal = Calendar.getInstance();
    int hour = cal.get(Calendar.HOUR);
    int minute = cal.get(Calendar.MINUTE);

    final boolean is24TimeFormat = true;
    TimePickerDialog dialog = new TimePickerDialog(DatePickerActivity.this, new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker timePicker, int hour, int minute) {
            String amOrPm = is24TimeFormat ? "" : (hour < 11 && minute < 59 ? "AM" : "PM");
            Toast.makeText(DatePickerActivity.this, "Your Selected Time: " + hour + ":" + minute + " " + amOrPm, Toast.LENGTH_SHORT).show();
        }
    }, hour, minute, is24TimeFormat);

    dialog.show();
}