1
votes

I have datepicker dialog popup on button click event in android. the date format it assigned to text box is d/mm/yyyy.

how to have the date format as dd/MMM/yyyy once user select the date from datepicker dialog below is my dateset code

public void onDateSet(DatePicker view, int yearr, int monthOfYearr,
                int dayOfMonthh) {
            // TODO Auto-generated method stub
            yrr=yearr;
            monn=monthOfYearr +1;

            dayy=dayOfMonthh;

            EditText itt=(EditText)findViewById(R.id.editText2);


            itt.setText(dayy+" / "+monn+" / "+yrr);
1

1 Answers

5
votes

You could add a method like this one

private static String formatDate(int year, int month, int day) {

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(0);
    cal.set(year, month, day);
    Date date = cal.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy");

    return sdf.format(date);
}

And then change one line in your code:

itt.setText(formatDate(yrr, monn, dayy));