Rauf Abid
Rauf Abid

Reputation: 346

How to Custom date format in android Date Picker

I am trying to capture a date from date picker dialog with the custom format like dd-MMM-yyyy(10-aug-1982). I have used the following code, but that only accept integer value as month not string. Please see my commented code that is i want to send from date picker to Edit Text.

Please anyone guide that how can i return the month as string. Thanks in advance.Following is my code.

XML

    <EditText
        android:id="@+id/date"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#d4d4d4"
        android:inputType="none"
        android:textIsSelectable="true"
        android:hint="Select Date..."
        android:padding="15dp"
        android:textColor="#897"
        android:textColorHint="#090"
        android:textSize="20sp"
        android:textStyle="bold" />
</RelativeLayout>

code

 package com.example.raufabid.mycalendar;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    import android.app.DatePickerDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.DatePicker;
    import android.widget.EditText;

    import java.text.SimpleDateFormat;
    import java.util.Calendar;

    public class MainActivity extends AppCompatActivity {

        EditText date;
        DatePickerDialog datePickerDialog;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            date = (EditText) findViewById(R.id.date);
            date.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    final Calendar c = Calendar.getInstance();
                    int mYear = c.get(Calendar.YEAR); // current year
                    int mMonth = c.get(Calendar.MONTH); // current month
    //                SimpleDateFormat mMonth = new SimpleDateFormat("MMM");
    //                String Month_name = mMonth.format(c);

                    int mDay = c.get(Calendar.DAY_OF_MONTH); // current day


                    // date picker dialog
                    datePickerDialog = new DatePickerDialog(MainActivity.this,
                            new DatePickerDialog.OnDateSetListener() {

                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int monthOfYear, int dayOfMonth) {
                                    // set day of month , month and year value in the edit text
                                    date.setText(dayOfMonth + "/"
                                            + (monthOfYear + 1) + "/" + year);

                                }
                            }, mYear, mMonth, mDay);
                    datePickerDialog.show();
                }
            });

        }
    }

EDIT:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // initiate the date picker and a button
        date = (EditText) findViewById(R.id.date);
        bbc = (EditText) findViewById(R.id.editText2);
        // perform click event on edit text
        date.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // calender class's instance and get current date , month and year from calender
                final Calendar c = Calendar.getInstance();
                int mYear = c.get(Calendar.YEAR); // current year

                try {
                    c.setTime(new SimpleDateFormat("MMM").parse("July"));
                    int mMonth = c.get(Calendar.MONTH) + 1;
                    //String mMonth = c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
                    int mDay = c.get(Calendar.DAY_OF_MONTH); // current day

                    // SimpleDateFormat mMonth = new SimpleDateFormat("MMM");
                    // String Month_name = mMonth.format(c);

                    // date picker dialog
                    datePickerDialog = new DatePickerDialog(MainActivity.this,
                            new DatePickerDialog.OnDateSetListener() {

                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int monthOfYear, int dayOfMonth) {
                                    // set day of month , month and year value in the edit text
                                    date.setText(dayOfMonth + "/"
                                            + (monthOfYear + 1) + "/" + year);

                                }
                            }, mYear, mMonth, mDay);
                    datePickerDialog.show();

                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}

Upvotes: 1

Views: 7099

Answers (2)

M. KHALIL NAZIR
M. KHALIL NAZIR

Reputation: 21

Use "EEEE dd" in place of "dd" to get name of the day. Like Friday 25-Oct-2019.

            // date picker dialog
            datePickerDialog = new DatePickerDialog(MainActivity.this,
                    new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth)
                        {
                            Calendar calendar = Calendar.getInstance();
                            c.set(year , monthOfYear , dayOfMonth);
                            SimpleDateFormat format = new SimpleDateFormat("EEEE dd-MMM-yyyy");
                            String datestring=format.format(calendar.getTime());
                            editText.setText(datestring);
                        }
                    }, mYear, mMonth, mDay);

Upvotes: 1

shine_joseph
shine_joseph

Reputation: 2942

Change to the following

// date picker dialog
datePickerDialog = new DatePickerDialog(MainActivity.this,
        new DatePickerDialog.OnDateSetListener() {
          @Override
             public void onDateSet(DatePicker view, int year,
                                           int monthOfYear, int dayOfMonth){
                                Calendar calendar = Calendar.getInstance();
                                calendar.set(year, monthOfYear, dayOfMonth);
                                SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
                                String dateString = format.format(calendar.getTime());
                                date.setText(dateString);
                            }
                        }, mYear, mMonth, mDay);

Upvotes: 4

Related Questions