简体   繁体   中英

How to convert datepicker dialog values to string like (13 January 2021) in Android Studio?

The current values I am getting from datepicker dialog are like this "13/1/2021". How can I convert it to "13 January 2021".

   textVFD.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            calendar = Calendar.getInstance();
            validFromYear = calendar.get(Calendar.YEAR);
            validFromMonth = calendar.get(Calendar.MONTH);
            validFromDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            datePickerDialog = new DatePickerDialog(DocumentUploadActivity.this,
                    new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int month, int day) {
                            textVFD.setText(day + "-" + (month + 1) + "-" + year);
                            validFromDate = textVFD.getText().toString();

                        }
                    }, validFromYear, validFromMonth, validFromDayOfMonth);
            datePickerDialog.show();
        }
    });

Try this,

Date date1 = new SimpleDateFormat("dd-MM-yyyy").parse("day + "-" + (month + 1) + "-" + year");
String formattedTime = new SimpleDateFormat("dd MMMMM yyyy").format(date1);
textVFD.setText(formattedTime );

For more information and different Date formats check https://developer.android.com/reference/java/text/SimpleDateFormat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM