How to format the date and month so that they are typed 0 in their formatting (day or month that goes from 1 to 9)?

Asked

Viewed 55 times

-5

I’m wanting to add a zero in the day and month in the date that the editData_devolucao receive. Like, instead of appearing day 05 is just 5 and in the month instead of appearing 06 appears only 6.

Example:

  • How do you return: 2/6/2021
  • How I wanted you to return: 02/06/2021

The line that corresponds to the formatting of my date is

String data_dev = dia + "/" + mes + "/" + ano; // Formatação da data atual

Code

            EditText editData_devolucao;
            
            editData_devolucao = findViewById(R.id.editData_devolucao);
            Calendar calendar = Calendar.getInstance(getDefault(Category.FORMAT));
            dia = calendar.get(Calendar.DAY_OF_MONTH);
            mes = calendar.get(Calendar.MONTH);
            ano = calendar.get(Calendar.YEAR);
            editData_devolucao.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    final DatePickerDialog datePickerDialog = new DatePickerDialog(
                            CadastrarEmprestimo.this, new DatePickerDialog.OnDateSetListener(){
                        @Override
                        public void onDateSet(DatePicker view, int ano, int mes, int dia){
                            mes += 1;
                            String data_dev = dia + "/" + mes + "/" + ano; // Formatação da data atual
                            editData_devolucao.setText(data_dev);
                        }
                    },ano,mes,dia);
                    datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
                    datePickerDialog.show();
                }
            });

2 answers

1


You can use the String.format using the "0nd" parameter, where n is the number of digits you should have.
For example String.format("%02d", dia) will format "day" to 2 digits, completing with zero left.

Would look like this:

String data_dev = String.format("%02d", dia) + "/" + String.format("%02d", mes) + "/" + Integer.toString(ano);

You can see it working here: https://www.mycompiler.io/view/GkfBAqR

EDIT: taking advantage of @hkotsubo’s suggestion, it’s even simpler this way: String.format("%02d/%02d/%04d", dia, mes, ano)

  • String.format possesses date-specific formats (example of use), and then you can use the Calendar direct, without having to pick up the fields separately. Anyway, the best way is on the dup I indicated (use the native date API)

  • yes @hkotsubo, but these formats work if you have the date in a String or a Date object, as in the question each part of the date is a whole value (day, month and year) could not do using these formats cited in the other answer

  • But if you use the separate fields, you could do it all at once: String.format("%02d/%02d/%04d", dia, mes, ano). But I still prefer the native date API :-)

  • "could not do using these formats cited in the other answer" - It would be because in the question it creates a Calendar. Unless the method there only accepts the separate fields

  • I saw the object, but I was guided by the method itself, but your suggestion is very good, I tried to get the closest to the question but it gets much better really, I’ll add in the answer :)

0

I think what you need is this right here:

String strDate= String.format("%02d/%02d/%s", dia, mes, ano);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.