• Privacy Policy
  • Contact us
  • Register
  • Log In
  • Facebook
  • Pinterest
  • YouTube
  • WhatsUp
Nulled Source Code - NSC
  • Home
  • Mobile Apps
  • PHP Scripts
  • Paid Projects
  • WordPress Theme
Nulled Source Code - NSC
  • Home
  • Mobile Apps
  • PHP Scripts
  • Paid Projects
  • WordPress Theme

Android Widgets

  • Android DatePicker Example And Tutorial
  • Android SeekBar Exmple and Tutorial
  • Android WebView Example and Tutorial
  • Android RatingBar Tutorial and Example
  • Android ListView Tutorial and Example
  • Android Spinner Tutorial and Examples
  • Android Alert Dialog Tutorial and Example
  • Android Radio Button Tutorials
  • Android CheckBox Tutorial
  • Android Toggle Buttons
  • Android Toast Example
  • Types of Android Button and Examples
  • About Android Studio Widgets

About Android

  • How to Setup Different Android Emulators?
  • What is Android Architecture?
  • History of Android Studio and Version
  • What is Android Studio and Installation
  • Home
  • Courses
  • Android Widgets
  • Android DatePicker Example And Tutorial

Android DatePicker Example And Tutorial

Different Date Format using Date Picker - Android Studio tutorials Free - NSC - Nulled Source Code

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:

DatePicker Example Android Studio
DatePicker Example Android Studio

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:

Different Date Format using Date Picker - Android Studio tutorials Free
Different Date Format using Date Picker

Click Here to Download Source Code

DatePicker
What are your Feelings
What are your Feelings
Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest
Updated on April 27, 2021
Android SeekBar Exmple and Tutorial

Leave a Comment X

You must be logged in to post a comment.

Copyright © 2022.Nuled Source Code.

  • Native Android
  • Paid Projects
  • Flutter
  • My Tickets
  • Register
  • Log In
  • Register
 

Loading Comments...
 

You must be logged in to post a comment.