I am trying to display date in datePicker
dialog in a format like Sep|29|2016
. you can see in following image.
unfortunately most of the time i am getting 28|M09|2016
rarely i am getting expected output. you can see in following image
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.
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");
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();
}
});
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()));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With