Android DatePicker : How to set value from DatePicker in EditText in this format dd-mm-yyyy -
while setting value in edittext datepicker, how set ex 25-03-2014 instead of 25-3-2014.
cos displays 25-3-2014 missing '0' before month(mm) everytime.
thanks.
youredittext = (edittext) findviewbyid(r.id.your_edittext); simpledateformat sdf = new simpledateformat("dd/mm/yyyy"); string date = sdf.format(new date(system.currenttimemillis())); if (date.contains("/")) { date = date.replace("/", "-"); } youredittext.settext(date); add click event on youredittext inside xml :
android:onclick="ontime"
code snippet:
please extend activity fragmentactivity
public void ontime(view v) { dialogfragment newfragment = new fromdatepickerfragment(); newfragment.show(getsupportfragmentmanager(), "from date"); } class fromdatepickerfragment extends dialogfragment implements ondatesetlistener { @override public dialog oncreatedialog(bundle savedinstancestate) { // use current time default values picker final calendar c = calendar.getinstance(); int yr = c.get(calendar.year); int mnth = c.get(calendar.month); int day = c.get(calendar.day_of_month); // create new instance of timepickerdialog , return return new datepickerdialog(getactivity(), this, yr, mnth, day); } @override public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { // todo auto-generated method stub string date; monthofyear++; if (dayofmonth < 10) { date = "0" + dayofmonth + "-"; } else { date = dayofmonth + "-"; } if (monthofyear < 10) { date += "0" + monthofyear + "-"; } else { date += monthofyear + "-"; } date += year; youredittext.settext(date); } }
Comments
Post a Comment