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!

2 responses to “Android Date Picker Example App”
Please check this link http://stackoverflow.com/questions/22578317/textview-button-not-showing-after-add-datepicker-in-android
When i add your code ,Unable to show my xml attributes like Textview & Button