HOW TO: Get A Date From A DatePicker

If you are writing an Android application, you likely have come across some form of the DatePicker object. This object exposes a UI that allows a phone user to pick a month, day and year. Unfortunately, the object itself does not expose any methods to turn this into a date object. Unlike similar controls across other languages, you must construct the date object yourself. While this does cause burden, you can also create your own DatePicker class and implement this method if you prefer. To do this, you can create the following method to return back a date object:

public Date getDate() {
    	return new Date(this.getYear() + 1900, this.getMonth(), this.getDayOfMonth());
}

Otherwise, you will have to create a new date object every time you want to fetch this information from the DatePicker. Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>