
An Android DatePicker is a best widget to insert or choose a date in valid format. You can select a date by a day, a month and a year. It also save user time to from entering date manually and correctly.
Example of DatePicker:
First of create a new Project and add a DatePicker, a TextView to show selected date, in to your “activity_main.xml” file as shown below:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/text_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/date_Picker" android:layout_marginTop="20sp" android:text="Date is: " android:textStyle="bold" android:textColor="@color/teal_200" android:textSize="20sp" android:gravity="center"/> <DatePicker android:id="@+id/date_Picker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp"/> </RelativeLayout>
Add the below code in MainActivity.java file:
package com.nsc.datepicker; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.os.Build; import android.os.Bundle; import android.widget.DatePicker; import android.widget.TextView; // Download Android Projects From :https://nulledsourcecode.com/ // Contact us for making custom Android App // and School or College Projects. WhatsApp: +91 8307878848 // Telegram: @AndroidStudioCode public class MainActivity extends AppCompatActivity { @RequiresApi(api = Build.VERSION_CODES.O) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView selected_date = (TextView) findViewById(R.id.text_date); DatePicker datePicker = (DatePicker) findViewById(R.id.date_Picker); datePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() { @SuppressLint("SetTextI18n") @Override public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) { selected_date.setText(datePicker.getDayOfMonth()+"/" + datePicker.getMonth()+"/" + datePicker.getYear()); } }); } }
Output as a screenshot is give:

Now output is: 27/3/2021, if you want output as “27 Mar – 2021” follow below tutorial:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/text_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/date_Picker" android:layout_marginTop="20sp" android:text="Date is: " android:textStyle="bold" android:textColor="@color/teal_200" android:textSize="20sp" android:gravity="center"/> <TextView android:id="@+id/text_date2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/text_date" android:layout_marginTop="20sp" android:text="Date is: " android:textStyle="bold" android:textColor="@color/teal_200" android:textSize="20sp" android:gravity="center"/> <DatePicker android:id="@+id/date_Picker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp"/> </RelativeLayout>
add the below code in MainActivity.java:
package com.nsc.datepicker; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.widget.DatePicker; import android.widget.TextView; // Download Android Projects From :https://nulledsourcecode.com/ // Contact us for making custom Android App // and School or College Projects. WhatsApp: +91 8307878848 // Telegram: @AndroidStudioCode public class MainActivity extends AppCompatActivity { // the below string array is used to display the date formate with months name instead of month number public static final String[] MONTHS = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; @RequiresApi(api = Build.VERSION_CODES.O) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView selected_date = (TextView) findViewById(R.id.text_date); TextView selected_date2 = (TextView) findViewById(R.id.text_date2); DatePicker datePicker = (DatePicker) findViewById(R.id.date_Picker); datePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() { @SuppressLint("SetTextI18n") @Override public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) { int month = datePicker.getMonth(); selected_date2.setText(datePicker.getDayOfMonth()+" " + MONTHS[month]+" - " + datePicker.getYear()); selected_date.setText(datePicker.getDayOfMonth()+"/" + (datePicker.getMonth() + 1)+"/" + datePicker.getYear()); } }); } }
The Output of above code is:

Click Here to Download Source Code
Leave a Comment
You must be logged in to post a comment.