13

I am trying to display date in datePicker dialog in a format like Sep|29|2016. you can see in following image.

Expected output

enter image description here

unfortunately most of the time i am getting 28|M09|2016 rarely i am getting expected output. you can see in following image

current output

enter image description here

I have tried with following code

public Dialog onCreateDialog(Bundle savedInstanceState) {
    Calendar calendar = Calendar.getInstance();

    System.out.println(calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US));    

    calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US);
    int yy = calendar.get(Calendar.YEAR);
    int mm = calendar.get(Calendar.MONTH);   
    int dd = calendar.get(Calendar.DAY_OF_MONTH);      

    System.out.println("yearr==="+yy);
    System.out.println("monthh==="+mm);
    System.out.println("dayy==="+dd);



    DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
             this,yy,mm,dd);
    return datepickerdialog;
}

Thanks in anticipation.

Sujay
  • 2,510
  • 2
  • 27
  • 47

6 Answers6

5

Are you expecting this?

    Button selectDate = (Button) findViewById(R.id.idButton);
    selectDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Calendar currentDate = Calendar.getInstance(Locale.ENGLISH);
            int mYear = currentDate.get(Calendar.YEAR);
            int mMonth = currentDate.get(Calendar.MONTH);
            int mDay = currentDate.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog mDatePicker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int selectedyear, int selectedmonth, int selectedday) {

                    GregorianCalendar gc = new GregorianCalendar();
                    gc.setFirstDayOfWeek(Calendar.MONDAY);
                    gc.set(Calendar.MONTH, view.getMonth());
                    gc.set(Calendar.DAY_OF_MONTH, view.getDayOfMonth());
                    gc.set(Calendar.YEAR, view.getYear());

                    DecimalFormat mFormat = new DecimalFormat("00");
                    String selectedDate = String.format("%s/%S/%s", gc.get(Calendar.YEAR),
                            mFormat.format(gc.get(Calendar.MONTH) + 1),
                            mFormat.format(gc.get(Calendar.DATE)));

                }
            }, mYear, mMonth, mDay);
            mDatePicker.setTitle("Select Date");
            mDatePicker.show();
        }
    });  

output

Mable John
  • 4,518
  • 3
  • 22
  • 35
3
Calendar myCalendar = Calendar.getInstance();

DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
    }

};

   edittext.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new DatePickerDialog(classname.this, date, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });

      private void updateLabel() {

    String myFormat = "MM/dd/yy"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    edittext.setText(sdf.format(myCalendar.getTime()));
    }
Stephen
  • 9,899
  • 16
  • 90
  • 137
2

Might be the problem causing because of Locale.

Set current locale before dialog. Remove the Locale.US this is where the problem occurs.

Locale locale = getResources().getConfiguration().locale;
Locale.setDefault(locale);

After set this locale to dialog.

And then put your dialog stuff.

Android N (Api level 24) update (no warnings):

@TargetApi(Build.VERSION_CODES.N)
public Locale getCurrentLocale(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
        return getResources().getConfiguration().getLocales().get(0);
    } else{
        //noinspection deprecation
        return getResources().getConfiguration().locale;
    }
}
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
2

Did you change your locale configuration? It seems that the datepickerdialog uses that format for default (English) locale when you update the locale configuration.

Instead of this

Locale locale = new Locale(countryCode);
Locale.setDefault(locale);

Do this for countries with "en" language

Locale locale = new Locale(language, countryCode);
Locale.setDefault(locale);

Sample

Locale locale = new Locale("en", "GB");
Dyuben
  • 71
  • 1
0

Could be the OS version on the tablet you're using. It's not the locale because you're using that to get a string, not get the Calendar instance. Use the AppCompatDialogFragment (import android.support.v7.app.AppCompatDialogFragment)

public class DatePickerFragment extends AppCompatDialogFragment
        implements DatePickerDialog.OnDateSetListener {

    private static final String TAG = "DatePickerFormat";

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar c = Calendar.getInstance();

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        Log.d(TAG, String.format("onDateSet: %d %d %d", year, month, dayOfMonth));
    }
}
mWhitley
  • 453
  • 7
  • 14
0

Just pass "Locale.getDefault()" in Calendar.getInstance. Example shown below

public void getDate(EditText editText) {
    Calendar c = Calendar.getInstance(Locale.getDefault());
    int mYear = c.get(Calendar.YEAR);
    int mMonth = c.get(Calendar.MONTH);
    int mDay = c.get(Calendar.DAY_OF_MONTH);
    // date picker dialog
    datePickerDialog = new DatePickerDialog(CustomerProfileActivity.this,
            new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                    selectedDate = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;

                    editText.setText(selectedDate);

                }
            }, mYear, mMonth, mDay);
    datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() - 1000);
    datePickerDialog.setTitle("Select Date");
    datePickerDialog.show();
}
Ashwini
  • 653
  • 6
  • 7