codeofaninja
website

Android Date Picker Example App

Photo of Mike Dalisay
Modified Monday, August 5, 2013
by - @ninjazhai
I have a friend who often asks: "What day of the week is [insert date here]?" So I decided to create a simple app for him to easily know what day of the week is a certain date. After I made this simple app, I also find it fun to use, haha! Now I know what day of the week I was born, it was Monday (July 30, 1990)!

Video Demo


Here's the final output of our code for today.

Live Demo


In case you want to try it yourself, I uploaded this simple app in my play store account. Download it here: Day Of Week Ninja, or you can just search for "day of week ninja" in your android device.

Let's Code


MainActivity.java - code to run this android date picker example are just in this one file.

package com.example.datepickerlistener;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    DatePicker datePickerBirthday;
    TextView textViewUserDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // create the date picker
        datePickerBirthday = new DatePicker(this);
        
        // hide the whole calendar view (works in api 11 or greater)
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= 11) {
            datePickerBirthday.setCalendarViewShown(false);
        }

        // create the TextView
        textViewUserDate = new TextView(this);
        textViewUserDate.setGravity(Gravity.CENTER);
        
        // initialize the date to current date
        SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        String dateStr = sdfDateTime.format(new Date(System.currentTimeMillis()));
        
        String[] dateSplit = dateStr.split("-");
        int currentYear = Integer.parseInt(dateSplit[0]);
        int currentMonth = Integer.parseInt(dateSplit[1]);
        int currentDay = Integer.parseInt(dateSplit[2]);
        
        // to show date and day of week in the TextView
        setHumanReadableDate(currentYear, currentMonth, currentDay);

        // initialize date picker listener
        // currentMonth - 1, because on the picker, 0 is January
        datePickerBirthday.init(currentYear, currentMonth - 1, currentDay, birthdayListener);

        // add to the container
        LinearLayout linearLayoutCalTvContainer = new LinearLayout(this);
        linearLayoutCalTvContainer.setOrientation(LinearLayout.VERTICAL);
        linearLayoutCalTvContainer.addView(datePickerBirthday);
        linearLayoutCalTvContainer.addView(textViewUserDate);

        // set the views for the activity
        setContentView(linearLayoutCalTvContainer);

    }

    // the date picker listener
    OnDateChangedListener birthdayListener = new OnDateChangedListener() {
        
        @Override
        public void onDateChanged(DatePicker birthDayDatePicker,
                int newYear, int newMonth, int newDay) {

            try{
                
                // currentMonth + 1, to retrieve proper month
                setHumanReadableDate(newYear, newMonth + 1, newDay);
                
            } catch (NullPointerException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    
    // show the user a better date format
    public void setHumanReadableDate(int newYear, int newMonth, int newDay){
        try {
            
            String clickedDate = newYear + "-" + newMonth + "-" + newDay;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = sdf.parse(clickedDate);

            SimpleDateFormat sdfDateTime = new SimpleDateFormat("MMMM dd, yyyy 'is' EEEE", Locale.US);
            String dateStr = sdfDateTime.format(d);
            
            textViewUserDate.setText(dateStr);
            
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}


You can share your thoughts on this Android date picker example code!
For FREE programming tutorials, click the red button below and subscribe! :)
Thanks for the comments!
 
 
Fundamentals
"First do it, then do it right, then do it better."
~ Addy Osmani
"Talk is cheap. Show me the code."
~ Linus Torvalds
Let's Stay Connected!
g+ r
Android app on Google Play
© 2011-2014 The Code Of A Ninja. All rights reserved. Proudly Powered by Google Blogger. Images, logos, marks or names mentioned herein are the property of their respective owners.