How to format the result of a date?

3

I have developed a code where I put in an EditText the time after selecting it, with this following code the problem I have is when I select a time like 05:08 I get in the EditText 5: 8 (because it is in AM) but If I was in PM, the same time I got bounced: 17: 8. and also it fulfills everything well when the chosen time is 17:54 for example. thanks!

 public void metodo_horaSalida(View v){
    final Calendar c= Calendar.getInstance();
    hora=c.get(Calendar.HOUR_OF_DAY);
    minutos=c.get(Calendar.MINUTE);

    TimePickerDialog timePickerDialog =new TimePickerDialog(this, 3,new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            ethorasalida.setText(hourOfDay+":"+minute);
        }
    },hora,minutos,false);
    timePickerDialog.show();
}

Here is a good example

But when choosing this time in PM

Boot like this: This is in PM  

Boot like this: This is in AM  

    
asked by Pedro Narrea 21.12.2018 в 18:58
source

1 answer

3

Get the values and add them to your TextView or editText in this way assigning a format which would ensure always showing two digits:

ethorasalida.setText(String.format("%02d:%02d", hourOfDay, minute));

in this way, instead of showing:

17:8

would show

17:08
    
answered by 21.12.2018 / 19:07
source